Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

132 linesCodeBlameRaw
1import * as Notifications from "expo-notifications";
2import { Tabs, router, useGlobalSearchParams, useSegments } from "expo-router";
3import { Bell, House, ListTodo, Menu, MessagesSquare } from "lucide-react-native";
4import type { LucideIcon } from "lucide-react-native";
5import { type ComponentType, useEffect, useRef } from "react";
6import { StyleSheet, View } from "react-native";
7
8import { Banner } from "@/components/chat/banner";
9import { FeedProvider, useFeed } from "@/lib/feed";
10import { routeFor } from "@/lib/links";
11import { type PushData, registerForPush } from "@/lib/push";
12import { useSignedIn } from "@/lib/session";
13import { useColors } from "@/theme";
14
15/**
16 * The signed-in app. Its bar is the site's phone bar
17 * (apps/web/app/components/mobile.tsx): Home, Work, Chat, Notifications
18 * and More. Work and Chat have their screens; Home and Notifications say
19 * so and open the site. Code is at the top of More, for those who use it,
20 * and your account is under it.
21 */
22export default function AppLayout() {
23 return (
24 <FeedProvider>
25 <PushSetup />
26 <AppTabs />
27 <Banner />
28 </FeedProvider>
29 );
30}
31
32function AppTabs() {
33 const colors = useColors();
34 const { workspace, selectWorkspace } = useSignedIn();
35 const { unread, mentions } = useFeed();
36 // A link names its workspace (`/chat?workspace=acme`, lib/links.ts): switch to it.
37 const { workspace: linked } = useGlobalSearchParams<{ workspace?: string }>();
38 useEffect(() => {
39 if (linked && linked.toLowerCase() !== workspace?.slug.toLowerCase()) void selectWorkspace(linked);
40 }, [linked, workspace?.slug, selectWorkspace]);
41 // A conversation or a thread takes the whole screen, as on the site: the bar steps aside.
42 const segments = useSegments() as string[];
43 const inConversation = segments[1] === "chat" && (segments[2] === "[channel]" || segments[2] === "thread");
44 const badge = mentions > 0 ? mentions : unread > 0 ? unread : undefined;
45
46 return (
47 <Tabs
48 initialRouteName="chat"
49 screenOptions={{
50 headerShown: false,
51 // The site's phone bar: flat along the bottom on the rail's colour, the
52 // app you are in marked as the rail marks it (apps/web components/mobile.tsx).
53 tabBarActiveTintColor: colors.fg,
54 tabBarInactiveTintColor: colors.faint,
55 tabBarStyle: inConversation
56 ? { display: "none" }
57 : { backgroundColor: colors.rail, borderTopColor: colors.line, borderTopWidth: StyleSheet.hairlineWidth, elevation: 0 },
58 tabBarHideOnKeyboard: true,
59 tabBarLabelStyle: { fontSize: 11, fontWeight: "500" },
60 tabBarIconStyle: { width: 44, height: 28 },
61 }}
62 >
63 <Tabs.Screen name="index" options={{ href: null }} />
64 <Tabs.Screen name="home" options={{ title: "Home", tabBarIcon: (props) => <BarIcon icon={House} {...props} /> }} />
65 <Tabs.Screen name="work" options={{ title: "Work", tabBarIcon: (props) => <BarIcon icon={ListTodo} {...props} /> }} />
66 <Tabs.Screen
67 name="chat"
68 options={{
69 title: "Chat",
70 tabBarIcon: (props) => <BarIcon icon={MessagesSquare} {...props} />,
71 tabBarBadge: badge,
72 tabBarBadgeStyle: { backgroundColor: mentions > 0 ? colors.accent : colors.lineStrong, color: mentions > 0 ? colors.onAccent : colors.fg, fontSize: 11 },
73 }}
74 />
75 <Tabs.Screen name="notifications" options={{ title: "Notifications", tabBarIcon: (props) => <BarIcon icon={Bell} {...props} /> }} />
76 {/* Code opens from More, so it has no place in the bar. */}
77 <Tabs.Screen name="code" options={{ title: "Code", href: null }} />
78 <Tabs.Screen name="more" options={{ title: "More", tabBarIcon: (props) => <BarIcon icon={Menu} {...props} /> }} />
79 </Tabs>
80 );
81}
82
83/** A tab's icon: on the rail's accent in a 10px-rounded square when it is the tab you are in. */
84function BarIcon({ icon: Icon, focused }: { icon: LucideIcon | ComponentType<{ color: string; size: number }>; focused: boolean }) {
85 const colors = useColors();
86 return (
87 <View style={{ width: 44, height: 28, borderRadius: 10, alignItems: "center", justifyContent: "center", backgroundColor: focused ? colors.railAccent : "transparent" }}>
88 <Icon color={focused ? colors.fg : colors.muted} size={20} />
89 </View>
90 );
91}
92
93/**
94 * Registers the phone for notifications once signed in (asking the first
95 * time), and opens what a tapped notification is about: the thread or
96 * conversation, in its workspace.
97 */
98function PushSetup() {
99 const { client, selectWorkspace } = useSignedIn();
100 const handled = useRef<string | null>(null);
101 const response = Notifications.useLastNotificationResponse();
102
103 useEffect(() => {
104 void registerForPush(client, true).catch((error: unknown) => console.warn("g1t: registering for notifications failed", error));
105 // The system can give the app a new push token: register that one too.
106 const changed = Notifications.addPushTokenListener(() => {
107 void registerForPush(client, false).catch(() => undefined);
108 });
109 return () => changed.remove();
110 }, [client]);
111
112 useEffect(() => {
113 if (!response || response.actionIdentifier !== Notifications.DEFAULT_ACTION_IDENTIFIER) return;
114 const id = response.notification.request.identifier;
115 if (handled.current === id) return;
116 handled.current = id;
117 const data = (response.notification.request.content.data ?? {}) as PushData;
118 void (async () => {
119 if (data.workspace) await selectWorkspace(data.workspace);
120 if (data.channel_id && data.thread_root) {
121 router.push({ pathname: "/chat/thread", params: { channel: data.channel_id, root: data.thread_root } });
122 } else if (data.channel_id) {
123 router.push({ pathname: "/chat/[channel]", params: { channel: data.channel_id } });
124 } else if (data.href) {
125 const route = routeFor(data.href);
126 router.push((route ?? `/open?path=${encodeURIComponent(data.href)}`) as never);
127 }
128 })();
129 }, [response, selectWorkspace]);
130
131 return null;
132}