| 1 | import * as Notifications from "expo-notifications"; |
| 2 | import { Tabs, router, useGlobalSearchParams, useSegments } from "expo-router"; |
| 3 | import { Bell, Bot, Code2, House, Menu, MessagesSquare } from "lucide-react-native"; |
| 4 | import type { LucideIcon } from "lucide-react-native"; |
| 5 | import { type ComponentType, useEffect, useRef } from "react"; |
| 6 | import { StyleSheet, View } from "react-native"; |
| 7 | |
| 8 | import { Banner } from "@/components/chat/banner"; |
| 9 | import { FeedProvider, useFeed } from "@/lib/feed"; |
| 10 | import { routeFor } from "@/lib/links"; |
| 11 | import { type PushData, registerForPush } from "@/lib/push"; |
| 12 | import { useSignedIn } from "@/lib/session"; |
| 13 | import { hasCodeAccess } from "@g1t/contracts"; |
| 14 | import { 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, Agents, |
| 19 | * Code, and More. Chat and Code have their screens; the others say so and |
| 20 | * open the site. Customize and your account are under More. |
| 21 | */ |
| 22 | export default function AppLayout() { |
| 23 | return ( |
| 24 | <FeedProvider> |
| 25 | <PushSetup /> |
| 26 | <AppTabs /> |
| 27 | <Banner /> |
| 28 | </FeedProvider> |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | function 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="agents" options={{ title: "Agents", tabBarIcon: (props) => <BarIcon icon={Bot} {...props} /> }} /> |
| 77 | <Tabs.Screen name="code" options={{ title: "Code", href: code ? undefined : null, tabBarIcon: (props) => <BarIcon icon={Code2} {...props} /> }} /> |
| 78 | <Tabs.Screen name="customize" options={{ href: null }} /> |
| 79 | <Tabs.Screen name="more" options={{ title: "More", tabBarIcon: (props) => <BarIcon icon={Menu} {...props} /> }} /> |
| 80 | </Tabs> |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | /** A tab's icon: on the rail's accent in a 10px-rounded square when it is the tab you are in. */ |
| 85 | function BarIcon({ icon: Icon, focused }: { icon: LucideIcon | ComponentType<{ color: string; size: number }>; focused: boolean }) { |
| 86 | const colors = useColors(); |
| 87 | return ( |
| 88 | <View style={{ width: 44, height: 28, borderRadius: 10, alignItems: "center", justifyContent: "center", backgroundColor: focused ? colors.railAccent : "transparent" }}> |
| 89 | <Icon color={focused ? colors.fg : colors.muted} size={20} /> |
| 90 | </View> |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Registers the phone for notifications once signed in (asking the first |
| 96 | * time), and opens what a tapped notification is about: the thread or |
| 97 | * conversation, in its workspace. |
| 98 | */ |
| 99 | function PushSetup() { |
| 100 | const { client, selectWorkspace } = useSignedIn(); |
| 101 | const handled = useRef<string | null>(null); |
| 102 | const response = Notifications.useLastNotificationResponse(); |
| 103 | |
| 104 | useEffect(() => { |
| 105 | void registerForPush(client, true).catch((error: unknown) => console.warn("g1t: registering for notifications failed", error)); |
| 106 | // The system can give the app a new push token: register that one too. |
| 107 | const changed = Notifications.addPushTokenListener(() => { |
| 108 | void registerForPush(client, false).catch(() => undefined); |
| 109 | }); |
| 110 | return () => changed.remove(); |
| 111 | }, [client]); |
| 112 | |
| 113 | useEffect(() => { |
| 114 | if (!response || response.actionIdentifier !== Notifications.DEFAULT_ACTION_IDENTIFIER) return; |
| 115 | const id = response.notification.request.identifier; |
| 116 | if (handled.current === id) return; |
| 117 | handled.current = id; |
| 118 | const data = (response.notification.request.content.data ?? {}) as PushData; |
| 119 | void (async () => { |
| 120 | if (data.workspace) await selectWorkspace(data.workspace); |
| 121 | if (data.channel_id && data.thread_root) { |
| 122 | router.push({ pathname: "/chat/thread", params: { channel: data.channel_id, root: data.thread_root } }); |
| 123 | } else if (data.channel_id) { |
| 124 | router.push({ pathname: "/chat/[channel]", params: { channel: data.channel_id } }); |
| 125 | } else if (data.href) { |
| 126 | const route = routeFor(data.href); |
| 127 | router.push((route ?? `/open?path=${encodeURIComponent(data.href)}`) as never); |
| 128 | } |
| 129 | })(); |
| 130 | }, [response, selectWorkspace]); |
| 131 | |
| 132 | return null; |
| 133 | } |