@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 03:37 UTC

main
ts 178 lines 6.33 KB
Raw
1 import type {
2 NavigationState,
3 ParamListBase,
4 StackNavigationState,
5 } from "@react-navigation/native";
6
7 type Route = StackNavigationState<ParamListBase>["routes"][number];
8
9 /** Keep the presenting history mounted while modal routes cover the whole workspace. */
10 export function projectStackPresentation(
11 state: StackNavigationState<ParamListBase>,
12 isOverlay: (route: Route) => boolean,
13 ) {
14 const firstOverlay = state.routes.findIndex(isOverlay);
15 return {
16 workspace: state.routes.slice(0, firstOverlay < 0 ? undefined : firstOverlay),
17 overlays: firstOverlay < 0 ? [] : state.routes.slice(firstOverlay),
18 };
19 }
20
21 /** Project one router history into native columns and a modal presentation stack. */
22 export function projectWorkspaceStack(
23 state: StackNavigationState<ParamListBase>,
24 isOverlay: (route: Route) => boolean,
25 ) {
26 const { workspace: workspaceRoutes, overlays } = projectStackPresentation(state, isOverlay);
27 const primary = workspaceRoutes.find((route) => route.name === "Home");
28 // The Settings tab's route only selects what Home shows; its pages are the detail.
29 const detail = workspaceRoutes.filter(
30 (route) => route.name !== "Home" && route.name !== "Settings",
31 );
32 return { primary, detail, overlays };
33 }
34
35 /** Native callbacks can arrive after a JS pop or replace; those must not pop the next screen. */
36 export function nativeWorkspacePopCount(
37 state: Pick<NavigationState, "index" | "routes">,
38 dismissedKey: string,
39 ): number {
40 const index = state.routes.findIndex((route) => route.key === dismissedKey);
41 return index <= 0 || index > state.index ? 0 : state.index - index + 1;
42 }
43
44 /** POP counts backwards from its source, so dismiss a group from the active descendant. */
45 export function nativeWorkspacePopAction(
46 state: Pick<NavigationState, "key" | "index" | "routes">,
47 dismissedKey: string,
48 ) {
49 const count = nativeWorkspacePopCount(state, dismissedKey);
50 return count
51 ? {
52 type: "POP" as const,
53 payload: { count },
54 source: state.routes[state.index]!.key,
55 target: state.key,
56 }
57 : null;
58 }
59
60 /** A native card pop must preserve pushes made before its completion event reaches JS. */
61 export function nativeStackPopAction(
62 state: Pick<NavigationState, "key" | "index" | "routes">,
63 dismissedKey: string,
64 destinationKey?: string,
65 ) {
66 const sourceIndex = state.routes.findIndex((route) => route.key === dismissedKey);
67 let destinationIndex =
68 destinationKey === undefined
69 ? sourceIndex - 1
70 : state.routes.findIndex((route) => route.key === destinationKey);
71 // Home's screen draws the Settings list, so going back to it from a Settings page stops there.
72 if (destinationIndex >= 0 && state.routes[destinationIndex + 1]?.name === "Settings")
73 destinationIndex += 1;
74 if (
75 sourceIndex <= 0 ||
76 sourceIndex > state.index ||
77 destinationIndex < 0 ||
78 destinationIndex >= sourceIndex
79 ) {
80 return null;
81 }
82 return {
83 type: "POP" as const,
84 payload: { count: sourceIndex - destinationIndex },
85 source: dismissedKey,
86 target: state.key,
87 };
88 }
89
90 /** The column UIKit shows while a split is collapsed, and the active detail it began removing. */
91 export interface CollapsedSplitColumn {
92 readonly visible: "primary" | "secondary";
93 readonly leaving: string | null;
94 /** Back reveals another screen; a full-screen presentation covers the detail without one. */
95 readonly revealed?: boolean;
96 }
97
98 /**
99 * A collapsed split nests the detail column's navigation controller in the list column's. Back
100 * from the detail root pops that controller whole, so no screen leaves its own stack or reports a
101 * dismissal: the list appearing after the active detail began leaving is the only sign of it.
102 */
103 export function collapsedSplitTransition(
104 column: CollapsedSplitColumn,
105 event: {
106 readonly type: "willAppear" | "didAppear" | "willDisappear" | "didDisappear";
107 readonly key: string;
108 },
109 state: Pick<NavigationState, "key" | "index" | "routes">,
110 primaryKey: string,
111 ): {
112 readonly column: CollapsedSplitColumn;
113 readonly pop: ReturnType<typeof nativeStackPopAction>;
114 } {
115 switch (event.type) {
116 case "willDisappear":
117 // JS pushes, pops, and presentations have already moved the router past this screen.
118 return {
119 column:
120 state.index > 0 && state.routes[state.index]?.key === event.key
121 ? { ...column, leaving: event.key, revealed: false }
122 : column,
123 pop: null,
124 };
125 case "willAppear":
126 // A cancelled swipe, or an expansion that keeps the detail on screen.
127 if (column.leaving === event.key) return { column: { ...column, leaving: null }, pop: null };
128 return { column: column.leaving ? { ...column, revealed: true } : column, pop: null };
129 case "didDisappear":
130 return {
131 column:
132 column.leaving === event.key && !column.revealed ? { ...column, leaving: null } : column,
133 pop: null,
134 };
135 case "didAppear":
136 return {
137 column: { visible: event.key === primaryKey ? "primary" : "secondary", leaving: null },
138 pop:
139 event.key === primaryKey && column.leaving
140 ? nativeStackPopAction(state, column.leaving, primaryKey)
141 : null,
142 };
143 }
144 }
145
146 export function partitionStackPresentations<T>(
147 routes: readonly T[],
148 isModal: (route: T) => boolean,
149 ): T[][] {
150 const groups: T[][] = [];
151 for (const route of routes) {
152 if (groups.length === 0 || isModal(route)) groups.push([]);
153 groups.at(-1)!.push(route);
154 }
155 return groups;
156 }
157
158 /** Keep outgoing screens in place until dismissal; Fabric moves enqueue native pop/push operations. */
159 export function reconcileStackScreens<T extends { readonly key: string }>(
160 previous: readonly T[],
161 current: readonly T[],
162 completedNativeDismissals: ReadonlySet<string> = new Set(),
163 ): T[] {
164 const active = new Set(current.map((route) => route.key));
165 const screens = [...current];
166 previous.forEach((route, index) => {
167 if (!active.has(route.key) && !completedNativeDismissals.has(route.key)) {
168 screens.splice(Math.min(index, screens.length), 0, route);
169 }
170 });
171 return screens;
172 }
173
174 /** Whether a route is presented over the stack (a sheet or modal) rather than pushed. */
175 export function isPresentedRoute(options: { readonly presentation?: string } | undefined) {
176 const presentation = options?.presentation;
177 return presentation !== undefined && presentation !== "card";
178 }