Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

workspace-stack-projection.ts

152 lines5.2 KB

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