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.

g1t.sh
131 linesCodeBlameRaw
1import * as Notifications from "expo-notifications";
2import { Tabs, router, useGlobalSearchParams, useSegments } from "expo-router";
3import { Bell, Code2, House, 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 { hasCodeAccess } from "@g1t/contracts";
14import { useColors } from "@/theme";
15
16/**
17 * The signed-in app. Its bar is the site's phone bar
18 * (apps/web/app/components/mobile.tsx): Home, Chat, Notifications, Code,
19 * and More. Chat and Code have their screens; the others say so and open
20 * the site. Your account is under More.
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 const code = !workspace || hasCodeAccess(workspace);
42 // A conversation or a thread takes the whole screen, as on the site: the bar steps aside.
43 const segments = useSegments() as string[];
44 const inConversation = segments[1] === "chat" && (segments[2] === "[channel]" || segments[2] === "thread");
45 const badge = mentions > 0 ? mentions : unread > 0 ? unread : undefined;
46
47 return (
48 <Tabs
49 initialRouteName="chat"
50 screenOptions={{
51 headerShown: false,
52 // The site's phone bar: flat along the bottom on the rail's colour, the
53 // app you are in marked as the rail marks it (apps/web components/mobile.tsx).
54 tabBarActiveTintColor: colors.fg,
55 tabBarInactiveTintColor: colors.faint,
56 tabBarStyle: inConversation
57 ? { display: "none" }
58 : { backgroundColor: colors.rail, borderTopColor: colors.line, borderTopWidth: StyleSheet.hairlineWidth, elevation: 0 },
59 tabBarHideOnKeyboard: true,
60 tabBarLabelStyle: { fontSize: 11, fontWeight: "500" },
61 tabBarIconStyle: { width: 44, height: 28 },
62 }}
63 >
64 <Tabs.Screen name="index" options={{ href: null }} />
65 <Tabs.Screen name="home" options={{ title: "Home", tabBarIcon: (props) => <BarIcon icon={House} {...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 <Tabs.Screen name="code" options={{ title: "Code", href: code ? undefined : null, tabBarIcon: (props) => <BarIcon icon={Code2} {...props} /> }} />
77 <Tabs.Screen name="more" options={{ title: "More", tabBarIcon: (props) => <BarIcon icon={Menu} {...props} /> }} />
78 </Tabs>
79 );
80}
81
82/** A tab's icon: on the rail's accent in a 10px-rounded square when it is the tab you are in. */
83function BarIcon({ icon: Icon, focused }: { icon: LucideIcon | ComponentType<{ color: string; size: number }>; focused: boolean }) {
84 const colors = useColors();
85 return (
86 <View style={{ width: 44, height: 28, borderRadius: 10, alignItems: "center", justifyContent: "center", backgroundColor: focused ? colors.railAccent : "transparent" }}>
87 <Icon color={focused ? colors.fg : colors.muted} size={20} />
88 </View>
89 );
90}
91
92/**
93 * Registers the phone for notifications once signed in (asking the first
94 * time), and opens what a tapped notification is about: the thread or
95 * conversation, in its workspace.
96 */
97function PushSetup() {
98 const { client, selectWorkspace } = useSignedIn();
99 const handled = useRef<string | null>(null);
100 const response = Notifications.useLastNotificationResponse();
101
102 useEffect(() => {
103 void registerForPush(client, true).catch((error: unknown) => console.warn("g1t: registering for notifications failed", error));
104 // The system can give the app a new push token: register that one too.
105 const changed = Notifications.addPushTokenListener(() => {
106 void registerForPush(client, false).catch(() => undefined);
107 });
108 return () => changed.remove();
109 }, [client]);
110
111 useEffect(() => {
112 if (!response || response.actionIdentifier !== Notifications.DEFAULT_ACTION_IDENTIFIER) return;
113 const id = response.notification.request.identifier;
114 if (handled.current === id) return;
115 handled.current = id;
116 const data = (response.notification.request.content.data ?? {}) as PushData;
117 void (async () => {
118 if (data.workspace) await selectWorkspace(data.workspace);
119 if (data.channel_id && data.thread_root) {
120 router.push({ pathname: "/chat/thread", params: { channel: data.channel_id, root: data.thread_root } });
121 } else if (data.channel_id) {
122 router.push({ pathname: "/chat/[channel]", params: { channel: data.channel_id } });
123 } else if (data.href) {
124 const route = routeFor(data.href);
125 router.push((route ?? `/open?path=${encodeURIComponent(data.href)}`) as never);
126 }
127 })();
128 }, [response, selectWorkspace]);
129
130 return null;
131}