The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { NavigationState } from "@react-navigation/native"; |
| 2 | import { isSettingsRoute } from "../features/settings/settings-navigation"; |
| 3 | import { resetRootStack, type RootStackNavigation } from "./root-stack-reset"; |
| 4 | |
| 5 | // Back follows history; Git confirmations can stack copies of the current thread, so skip those too. |
| 6 | function androidWorkspaceBackTarget(state: NavigationState) { |
| 7 | let index = state.index; |
| 8 | const params = state.routes[index]?.params as ReactNavigation.RootParamList["Thread"] | undefined; |
| 9 | if (state.routes[index]?.name === "Thread" && params) { |
| 10 | while (parentThreadIsBelow({ ...state, index }, params)) index -= 1; |
| 11 | } |
| 12 | return index - 1; |
| 13 | } |
| 14 | |
| 15 | /** The root stack with only the workspace list. */ |
| 16 | export function androidWorkspaceNavigationState(state: NavigationState) { |
| 17 | const home = state.routes.find((route) => route.name === "Home") ?? { name: "Home" }; |
| 18 | return { ...state, index: 0, routes: [home] }; |
| 19 | } |
| 20 | |
| 21 | export function resetAndroidWorkspace(navigation: RootStackNavigation) { |
| 22 | resetRootStack(navigation, androidWorkspaceNavigationState); |
| 23 | } |
| 24 | |
| 25 | export function parentThreadIsBelow( |
| 26 | state: NavigationState, |
| 27 | parent: ReactNavigation.RootParamList["Thread"], |
| 28 | ) { |
| 29 | if (!parent?.environmentId || !parent.threadId) return false; |
| 30 | const previous = state.routes[state.index - 1]; |
| 31 | const params = previous?.params as ReactNavigation.RootParamList["Thread"] | undefined; |
| 32 | return ( |
| 33 | previous?.name === "Thread" && |
| 34 | params?.environmentId === parent.environmentId && |
| 35 | params?.threadId === parent.threadId |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * How a thread link opened from inside a thread reaches its target. `navigate` would reuse the |
| 41 | * current Thread route, so Back would skip the thread the link was in and land on the list. |
| 42 | */ |
| 43 | export function linkedThreadStep( |
| 44 | state: NavigationState, |
| 45 | target: ReactNavigation.RootParamList["Thread"], |
| 46 | ): "stay" | "pop" | "push" { |
| 47 | const params = state.routes[state.index]?.params as |
| 48 | | ReactNavigation.RootParamList["Thread"] |
| 49 | | undefined; |
| 50 | if ( |
| 51 | state.routes[state.index]?.name === "Thread" && |
| 52 | params?.environmentId === target?.environmentId && |
| 53 | params?.threadId === target?.threadId |
| 54 | ) { |
| 55 | return "stay"; |
| 56 | } |
| 57 | return parentThreadIsBelow(state, target) ? "pop" : "push"; |
| 58 | } |
| 59 | |
| 60 | export function returnToAndroidWorkspaceList( |
| 61 | navigation: RootStackNavigation & { readonly goBack: () => void }, |
| 62 | ) { |
| 63 | const state = navigation.getState(); |
| 64 | const index = state ? androidWorkspaceBackTarget(state) : -1; |
| 65 | if (!state || index < 0) return resetAndroidWorkspace(navigation); |
| 66 | if (index === state.index - 1) return navigation.goBack(); |
| 67 | resetRootStack(navigation, () => ({ ...state, index, routes: state.routes.slice(0, index + 1) })); |
| 68 | } |
| 69 | |
| 70 | // Split view: Thread, PullRequest, and a Settings page picked from the list swap the detail pane |
| 71 | // rather than stack, so they don't animate. Pages opened from a Settings page crossfade inside the |
| 72 | // pane's card; the default push edge-extends the incoming page, bleeding a stripe beside the list. |
| 73 | export function androidWorkspaceDestinationAnimation( |
| 74 | route: { readonly key: string; readonly name: string }, |
| 75 | routes: readonly { readonly key: string; readonly name: string }[], |
| 76 | usesSplitView: boolean, |
| 77 | ) { |
| 78 | if (!usesSplitView) return undefined; |
| 79 | if (route.name === "Thread" || route.name === "PullRequest" || route.name === "Issue") |
| 80 | return "none" as const; |
| 81 | if (route.name === "Settings" || !isSettingsRoute(route.name)) return undefined; |
| 82 | const below = routes[routes.findIndex((candidate) => candidate.key === route.key) - 1]; |
| 83 | return below?.name === "Settings" ? ("none" as const) : ("fade" as const); |
| 84 | } |