GeezerVison is showing github.com — refactor(mobile): notifications, quick actions and Connect onboarding… · pingdotgg/t3code@0b02b21
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/App.tsx +50-10Lines changed: 50 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,9 +9,12 @@ import { GestureHandlerRootView } from "react-native-gesture-handler"; | 9 | 9 | import { KeyboardProvider } from "react-native-keyboard-controller"; | 10 | 10 | import { SafeAreaProvider } from "react-native-safe-area-context"; | 11 | 11 | import { | | 12 | + CommonActions, | | 13 | + createPathConfigForStaticNavigation, | 12 | 14 | createStaticNavigation, | 13 | 15 | getActionFromState, | 14 | 16 | getStateFromPath, | | 17 | + type NavigationAction, | 15 | 18 | type NavigationState, | 16 | 19 | } from "@react-navigation/native"; | 17 | 20 |
| @@ -25,12 +28,20 @@ import { | 25 | 28 | presentPendingIncomingShare, | 26 | 29 | startIncomingShareInbox, | 27 | 30 | } from "./features/sharing/IncomingShareProvider"; | | 31 | +import { | | 32 | + installAgentNotificationNavigation, | | 33 | + setAgentNotificationPathname, | | 34 | +} from "./features/agent-awareness/notificationNavigation"; | | 35 | +import { | | 36 | + installAppShortcuts, | | 37 | + recordAppShortcutNavigationState, | | 38 | +} from "./features/shortcuts/appShortcutsRuntime"; | 28 | 39 | import { | 29 | 40 | AppearancePreferencesProvider, | 30 | 41 | installAppearanceRuntime, | 31 | 42 | useAppearancePreferences, | 32 | 43 | } from "./features/settings/appearance/AppearancePreferencesProvider"; | 33 | | -import { RootStack } from "./Stack"; | | 44 | +import { RootStack, workspaceLocationFromState } from "./Stack"; | 34 | 45 | import { appAtomRegistry } from "./state/atom-registry"; | 35 | 46 | import { subscribeComposerDraftModelReconciliation } from "./state/use-composer-drafts"; | 36 | 47 | import { environmentThreadShells } from "./state/threads"; | @@ -57,7 +68,12 @@ import { | 57 | 68 | } from "./features/workspace/workspace-selection-store"; | 58 | 69 | import { workspaceDetailLinkAction } from "./features/workspace/workspace-detail-selection"; | 59 | 70 | import { NativeLayoutMetricsProvider } from "./native/native-layout-metrics"; | 60 | | -import { attachRootNavigation, rootNavigationRef } from "./lib/rootNavigation"; | | 71 | +import { | | 72 | + attachRootNavigation, | | 73 | + flushRootNavigationQueue, | | 74 | + linkToRootPath, | | 75 | + rootNavigationRef, | | 76 | +} from "./lib/rootNavigation"; | 61 | 77 | import { pruneNewTaskBranchCheckouts } from "./features/threads/NewTaskDraftRouteScreen"; | 62 | 78 |
| 63 | 79 | import "../global.css"; | @@ -69,6 +85,20 @@ if (process.env.EXPO_PUBLIC_SHOWCASE === "1") { | 69 | 85 | void SplashScreen.preventAutoHideAsync().catch(() => {}); | 70 | 86 | void hydrateWorkspaceSelections(); | 71 | 87 |
| | 88 | +const linkingConfig = { | | 89 | + initialRouteName: "Home", | | 90 | + screens: createPathConfigForStaticNavigation(RootStack, { initialRouteName: "Home" }) ?? {}, | | 91 | +} as const; | | 92 | + | | 93 | +function getAppStateFromPath(path: string, options: Parameters<typeof getStateFromPath>[1]) { | | 94 | + return takeScheduledTaskNewLink( | | 95 | + restoreSettingsLinkState( | | 96 | + restoreDraftLinkState(getStateFromPath(path, options), path), | | 97 | + settingsLinkPage(), | | 98 | + ), | | 99 | + ); | | 100 | +} | | 101 | + | 72 | 102 | const appLinking = { | 73 | 103 | prefixes: [Linking.createURL("/"), "t3code://", "t3code-dev://", "t3code-preview://"], | 74 | 104 | config: { initialRouteName: "Home" }, | @@ -81,25 +111,33 @@ const appLinking = { | 81 | 111 | // Our router handles the selection; React Navigation's type only lists its own actions. | 82 | 112 | (workspaceDetailLinkAction(state) as ReturnType<typeof getActionFromState>) ?? | 83 | 113 | getActionFromState(state, options), | 84 | | - getStateFromPath: (path: string, options: Parameters<typeof getStateFromPath>[1]) => | 85 | | - takeScheduledTaskNewLink( | 86 | | - restoreSettingsLinkState( | 87 | | - restoreDraftLinkState(getStateFromPath(path, options), path), | 88 | | - settingsLinkPage(), | 89 | | - ), | 90 | | - ), | | 114 | + getStateFromPath: getAppStateFromPath, | 91 | 115 | }; | 92 | 116 |
| | 117 | +function appLinkAction(href: string): NavigationAction { | | 118 | + const state = getAppStateFromPath(href, linkingConfig); | | 119 | + if (!state) throw new Error(`Failed to parse the href to a navigation state (${href}).`); | | 120 | + return appLinking.getActionFromState(state, linkingConfig) ?? CommonActions.reset(state); | | 121 | +} | | 122 | + | | 123 | +function linkToAppPath(href: string): void { | | 124 | + linkToRootPath(href, appLinkAction); | | 125 | +} | | 126 | + | 93 | 127 | const Navigation = createStaticNavigation(RootStack); | 94 | 128 |
| 95 | 129 | function handleNavigationStateChange(state: NavigationState | undefined): void { | 96 | 130 | recordWorkspaceSelections(state); | 97 | | - if (state) pruneNewTaskBranchCheckouts(state); | 98 | 131 | presentPendingIncomingShare(); | | 132 | + if (!state) return; | | 133 | + setAgentNotificationPathname(workspaceLocationFromState(state).pathname); | | 134 | + recordAppShortcutNavigationState(state); | | 135 | + pruneNewTaskBranchCheckouts(state); | 99 | 136 | } | 100 | 137 |
| 101 | 138 | function handleNavigationReady(): void { | 102 | 139 | handleNavigationStateChange(rootNavigationRef.getRootState()); | | 140 | + flushRootNavigationQueue(); | 103 | 141 | } | 104 | 142 |
| 105 | 143 | declare const module: { readonly hot?: FoundationHotModule } | undefined; | @@ -118,6 +156,8 @@ const appServiceDisposers = [ | 118 | 156 | installThreadOutboxDelivery(), | 119 | 157 | installComposerAttachmentUploads(), | 120 | 158 | startIncomingShareInbox(), | | 159 | + installAgentNotificationNavigation(linkToAppPath), | | 160 | + installAppShortcuts(linkToAppPath), | 121 | 161 | ]; | 122 | 162 | disposeOnFoundationReplace(typeof module === "undefined" ? undefined : module.hot, () => { | 123 | 163 | for (const dispose of appServiceDisposers) dispose(); | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/Stack.tsx +11-7Lines changed: 11 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,11 @@ import { androidWorkspaceDestinationAnimation } from "./lib/android-workspace-na | 24 | 24 | import { getCompactBrandHeaderOptions } from "./components/CompactBrandTitle"; | 25 | 25 | import { WorkspaceConnectionStatusProvider } from "./features/home/WorkspaceConnectionTitle"; | 26 | 26 | import { WorkspaceRootScreen } from "./features/workspace/WorkspaceRootScreen"; | | 27 | +import { | | 28 | + recordHomeFocus, | | 29 | + recordHomeTransitionEnd, | | 30 | + recordHomeTransitionStart, | | 31 | +} from "./features/home/home-route-visibility"; | 27 | 32 | import { AndroidWorkspaceTabsProvider } from "./features/workspace/AndroidWorkspaceTabsProvider"; | 28 | 33 | import { | 29 | 34 | PullRequestRouteScreen, | @@ -41,9 +46,7 @@ import { | 41 | 46 | type RenderFailureProps, | 42 | 47 | } from "./components/RenderErrorBoundary"; | 43 | 48 | import { ArchivedThreadsRouteScreen } from "./features/archive/ArchivedThreadsRouteScreen"; | 44 | | -import { useAgentNotificationNavigation } from "./features/agent-awareness/notificationNavigation"; | 45 | 49 | import { ConnectOnboardingRouteScreen } from "./features/cloud/ConnectOnboardingRouteScreen"; | 46 | | -import { useConnectOnboardingNavigation } from "./features/cloud/connectOnboardingNavigation"; | 47 | 50 | import { AttachmentFileScreen } from "./features/files/AttachmentFileScreen"; | 48 | 51 | import { ThreadFilesTreeScreen, ThreadFileScreen } from "./features/files/ThreadFilesRouteScreen"; | 49 | 52 | import { AdaptiveWorkspaceLayout } from "./features/layout/AdaptiveWorkspaceLayout"; | @@ -138,7 +141,6 @@ import { | 138 | 141 | SettingsLegalDocumentCloseHeaderButton, | 139 | 142 | SettingsLegalDocumentExternalHeaderButton, | 140 | 143 | } from "./features/settings/components/SettingsLegalDocumentRouteScreen"; | 141 | | -import { useAppShortcuts } from "./features/shortcuts/useAppShortcuts"; | 142 | 144 | import { NATIVE_LIQUID_GLASS_SUPPORTED } from "./native/native-glass"; | 143 | 145 | import { deriveLayout } from "./lib/layout"; | 144 | 146 | import { useNativeLayoutMetrics } from "./native/native-layout-metrics"; | @@ -473,7 +475,7 @@ const SETTINGS_SCREENS = { | 473 | 475 | // iOS 26 header morphing requires one UINavigationBar; nested stacks make the whole screen slide. | 474 | 476 | const THREAD_LINKING_PREFIX = "threads/:environmentId/:threadId"; | 475 | 477 |
| 476 | | -function workspaceLocationFromState(state: NavigationState) { | | 478 | +export function workspaceLocationFromState(state: NavigationState) { | 477 | 479 | const routes = state.routes.filter((route) => !isWorkspaceOverlayRoute(route.name)); | 478 | 480 | const effectiveState = | 479 | 481 | routes.length > 0 && routes.length !== state.routes.length | @@ -505,9 +507,6 @@ function RootStackLayout(props: { | 505 | 507 | const routeDraftId = Array.isArray(draftParams?.draftId) | 506 | 508 | ? draftParams.draftId[0] | 507 | 509 | : draftParams?.draftId; | 508 | | - useAgentNotificationNavigation(workspaceLocation.pathname); | 509 | | - useConnectOnboardingNavigation(); | 510 | | - useAppShortcuts(props.state); | 511 | 510 |
| 512 | 511 | return ( | 513 | 512 | <NewTaskFlowProvider active={isDraftThreadRoute(draftRoute)} routeDraftId={routeDraftId}> | @@ -592,6 +591,11 @@ const RootStackConfig = createWorkspaceStackNavigator({ | 592 | 591 | Home: createNativeStackScreen({ | 593 | 592 | screen: WorkspaceRootScreen, | 594 | 593 | linking: "", | | 594 | + listeners: { | | 595 | + transitionStart: ({ data }) => recordHomeTransitionStart(data.closing), | | 596 | + transitionEnd: ({ data }) => recordHomeTransitionEnd(data.closing), | | 597 | + focus: recordHomeFocus, | | 598 | + }, | 595 | 599 | options: { | 596 | 600 | ...GLASS_HEADER_OPTIONS, | 597 | 601 | ...FIXED_DUO_HEADER, | |
Collapse file Copy file name to clipboard +30-38Lines changed: 30 additions & 38 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { useEffect, useLayoutEffect, useRef } from "react"; | 2 | 1 | import * as Notifications from "expo-notifications"; | 3 | | -import { useLinkTo } from "@react-navigation/native"; | 4 | 2 |
| 5 | 3 | import { setAndroidThreadOnScreen } from "./androidNotifications"; | 6 | 4 | import { foregroundNotificationBehavior } from "./foregroundNotificationBehavior"; | 7 | 5 | import { routeAgentNotificationResponseOnce, threadDeepLinkOnScreen } from "./notificationPayload"; | 8 | 6 | import { consumeLastAgentNotificationResponse } from "./notificationResponseConsumer"; | 9 | 7 |
| 10 | | -export function useAgentNotificationNavigation(pathname: string): void { | 11 | | - const linkTo = useLinkTo(); | 12 | | - const handledResponseIds = useRef(new Set<string>()); | 13 | | - const deepLinkOnScreen = useRef<string | null>(null); | 14 | | - useLayoutEffect(() => { | 15 | | - const thread = threadDeepLinkOnScreen(pathname); | 16 | | - deepLinkOnScreen.current = thread; | 17 | | - setAndroidThreadOnScreen(thread); | 18 | | - }, [pathname]); | | 8 | +let deepLinkOnScreen: string | null = null; | 19 | 9 |
| 20 | | - useEffect(() => { | 21 | | - Notifications.setNotificationHandler({ | 22 | | - handleNotification: (notification) => | 23 | | - Promise.resolve(foregroundNotificationBehavior(notification, deepLinkOnScreen.current)), | 24 | | - }); | 25 | | - return () => { | 26 | | - Notifications.setNotificationHandler(null); | 27 | | - }; | 28 | | - }, []); | | 10 | +/** Called with the workspace pathname on every root navigation state change. */ | | 11 | +export function setAgentNotificationPathname(pathname: string): void { | | 12 | + deepLinkOnScreen = threadDeepLinkOnScreen(pathname); | | 13 | + setAndroidThreadOnScreen(deepLinkOnScreen); | | 14 | +} | 29 | 15 |
| 30 | | - useEffect(() => { | 31 | | - const handleResponse = (response: Notifications.NotificationResponse): void => { | 32 | | - routeAgentNotificationResponseOnce({ | 33 | | - handledResponseIds: handledResponseIds.current, | 34 | | - response, | 35 | | - navigate: linkTo, | 36 | | - }); | 37 | | - }; | | 16 | +/** | | 17 | + * App-lifetime notification routing: foreground presentation suppresses the | | 18 | + * thread already on screen, and taps (including the cold-start tap) open the | | 19 | + * notification's deep link through `navigate`. | | 20 | + */ | | 21 | +export function installAgentNotificationNavigation(navigate: (href: string) => void): () => void { | | 22 | + const handledResponseIds = new Set<string>(); | | 23 | + Notifications.setNotificationHandler({ | | 24 | + handleNotification: (notification) => | | 25 | + Promise.resolve(foregroundNotificationBehavior(notification, deepLinkOnScreen)), | | 26 | + }); | 38 | 27 |
| 39 | | - const subscription = Notifications.addNotificationResponseReceivedListener(handleResponse); | 40 | | - void consumeLastAgentNotificationResponse({ | 41 | | - getLastResponse: () => Notifications.getLastNotificationResponseAsync(), | 42 | | - clearLastResponse: () => Notifications.clearLastNotificationResponseAsync(), | 43 | | - handleResponse, | 44 | | - }); | | 28 | + const handleResponse = (response: Notifications.NotificationResponse): void => { | | 29 | + routeAgentNotificationResponseOnce({ handledResponseIds, response, navigate }); | | 30 | + }; | | 31 | + const subscription = Notifications.addNotificationResponseReceivedListener(handleResponse); | | 32 | + void consumeLastAgentNotificationResponse({ | | 33 | + getLastResponse: () => Notifications.getLastNotificationResponseAsync(), | | 34 | + clearLastResponse: () => Notifications.clearLastNotificationResponseAsync(), | | 35 | + handleResponse, | | 36 | + }); | 45 | 37 |
| 46 | | - return () => { | 47 | | - subscription.remove(); | 48 | | - }; | 49 | | - }, [linkTo]); | | 38 | + return () => { | | 39 | + subscription.remove(); | | 40 | + Notifications.setNotificationHandler(null); | | 41 | + }; | 50 | 42 | } |
Collapse file Copy file name to clipboard +74Lines changed: 74 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { afterEach, describe, expect, it, vi } from "vite-plus/test"; | | 2 | + | | 3 | +const navigation = vi.hoisted(() => ({ | | 4 | + navigate: vi.fn(), | | 5 | + ready: true, | | 6 | + queue: [] as Array<() => void>, | | 7 | +})); | | 8 | +const optOut = vi.hoisted(() => ({ optedOut: new Set<string>() })); | | 9 | + | | 10 | +vi.mock("../../lib/rootNavigation", () => ({ | | 11 | + rootNavigationRef: navigation, | | 12 | + whenRootNavigationReady: (navigate: () => void) => | | 13 | + navigation.ready ? navigate() : navigation.queue.push(navigate), | | 14 | +})); | | 15 | +vi.mock("./connectOnboardingOptOut", () => ({ | | 16 | + isConnectOnboardingOptedOut: (accountId: string) => | | 17 | + Promise.resolve(optOut.optedOut.has(accountId)), | | 18 | +})); | | 19 | + | | 20 | +import { clearConnectOnboardingRequest, requestConnectOnboarding } from "./connectOnboarding"; | | 21 | + | | 22 | +afterEach(() => { | | 23 | + clearConnectOnboardingRequest(); | | 24 | + optOut.optedOut.clear(); | | 25 | + navigation.navigate.mockReset(); | | 26 | + navigation.ready = true; | | 27 | + navigation.queue.length = 0; | | 28 | + vi.useRealTimers(); | | 29 | +}); | | 30 | + | | 31 | +describe("requestConnectOnboarding", () => { | | 32 | + it("presents the sheet after the settle delay", async () => { | | 33 | + vi.useFakeTimers(); | | 34 | + requestConnectOnboarding("account-1"); | | 35 | + await vi.advanceTimersByTimeAsync(599); | | 36 | + expect(navigation.navigate).not.toHaveBeenCalled(); | | 37 | + await vi.advanceTimersByTimeAsync(1); | | 38 | + expect(navigation.navigate).toHaveBeenCalledExactlyOnceWith("ConnectOnboarding"); | | 39 | + }); | | 40 | + | | 41 | + it("drops a request cleared by sign-out before it fires", async () => { | | 42 | + vi.useFakeTimers(); | | 43 | + requestConnectOnboarding("account-1"); | | 44 | + clearConnectOnboardingRequest(); | | 45 | + await vi.runAllTimersAsync(); | | 46 | + expect(navigation.navigate).not.toHaveBeenCalled(); | | 47 | + }); | | 48 | + | | 49 | + it("skips accounts that opted out", async () => { | | 50 | + vi.useFakeTimers(); | | 51 | + optOut.optedOut.add("account-1"); | | 52 | + requestConnectOnboarding("account-1"); | | 53 | + await vi.runAllTimersAsync(); | | 54 | + expect(navigation.navigate).not.toHaveBeenCalled(); | | 55 | + }); | | 56 | + | | 57 | + it("presents once when a newer request replaces a pending one", async () => { | | 58 | + vi.useFakeTimers(); | | 59 | + requestConnectOnboarding("account-1"); | | 60 | + requestConnectOnboarding("account-2"); | | 61 | + await vi.runAllTimersAsync(); | | 62 | + expect(navigation.navigate).toHaveBeenCalledOnce(); | | 63 | + }); | | 64 | + | | 65 | + it("drops a request cleared while it waits for navigation to be ready", async () => { | | 66 | + vi.useFakeTimers(); | | 67 | + navigation.ready = false; | | 68 | + requestConnectOnboarding("account-1"); | | 69 | + await vi.runAllTimersAsync(); | | 70 | + clearConnectOnboardingRequest(); | | 71 | + for (const navigate of navigation.queue.splice(0)) navigate(); | | 72 | + expect(navigation.navigate).not.toHaveBeenCalled(); | | 73 | + }); | | 74 | +}); |
Collapse file Copy file name to clipboard +37-8Lines changed: 37 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { Atom } from "effect/reactivity"; | | 1 | +import { rootNavigationRef, whenRootNavigationReady } from "../../lib/rootNavigation"; | | 2 | +import { isConnectOnboardingOptedOut } from "./connectOnboardingOptOut"; | 2 | 3 |
| 3 | | -import { appAtomRegistry } from "../../state/atom-registry"; | | 4 | +// Sign-in happens in its own modal; give its session-state transition a beat to | | 5 | +// settle before presenting another formSheet on top. | | 6 | +const PRESENT_ONBOARDING_DELAY_MS = 600; | | 7 | + | | 8 | +interface PendingRequest { | | 9 | + readonly accountId: string; | | 10 | + readonly timer: ReturnType<typeof setTimeout>; | | 11 | +} | 4 | 12 |
| 5 | 13 | // Retain the account id so sign-out cannot present onboarding for the wrong account. | 6 | | -export const connectOnboardingRequestAtom = Atom.make<string | null>(null).pipe( | 7 | | - Atom.keepAlive, | 8 | | - Atom.withLabel("mobile:connect-onboarding-request"), | 9 | | -); | | 14 | +let pending: PendingRequest | null = null; | 10 | 15 |
| 11 | 16 | export function requestConnectOnboarding(accountId: string): void { | 12 | | - appAtomRegistry.set(connectOnboardingRequestAtom, accountId); | | 17 | + clearConnectOnboardingRequest(); | | 18 | + const request: PendingRequest = { | | 19 | + accountId, | | 20 | + timer: setTimeout(() => { | | 21 | + void (async () => { | | 22 | + const optedOut = await isConnectOnboardingOptedOut(accountId).catch(() => false); | | 23 | + // A sign-out or a newer sign-in may have replaced this request while | | 24 | + // the opt-out read was in flight. | | 25 | + if (pending !== request) return; | | 26 | + if (optedOut) { | | 27 | + pending = null; | | 28 | + return; | | 29 | + } | | 30 | + // Stays pending until navigation is ready, so a sign-out meanwhile still cancels it. | | 31 | + whenRootNavigationReady(() => { | | 32 | + if (pending !== request) return; | | 33 | + pending = null; | | 34 | + rootNavigationRef.navigate("ConnectOnboarding"); | | 35 | + }); | | 36 | + })(); | | 37 | + }, PRESENT_ONBOARDING_DELAY_MS), | | 38 | + }; | | 39 | + pending = request; | 13 | 40 | } | 14 | 41 |
| 15 | 42 | export function clearConnectOnboardingRequest(): void { | 16 | | - appAtomRegistry.set(connectOnboardingRequestAtom, null); | | 43 | + if (pending === null) return; | | 44 | + clearTimeout(pending.timer); | | 45 | + pending = null; | 17 | 46 | } |
|