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

workspace-stack-projection.ts

99 lines3.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 const detail = workspaceRoutes.filter((route) => route.name !== "Home");
17 const overlays = firstOverlay < 0 ? [] : state.routes.slice(firstOverlay);
18 return { primary, detail, overlays };
20​
21/** Native callbacks can arrive after a JS pop or replace; those must not pop the next screen. */
22export function nativeWorkspacePopCount(
23 state: Pick<NavigationState, "index" | "routes">,
24 dismissedKey: string,
25): number {
26 const index = state.routes.findIndex((route) => route.key === dismissedKey);
27 return index <= 0 || index > state.index ? 0 : state.index - index + 1;
29​
30/** POP counts backwards from its source, so dismiss a group from the active descendant. */
31export function nativeWorkspacePopAction(
32 state: Pick<NavigationState, "key" | "index" | "routes">,
33 dismissedKey: string,
34) {
35 const count = nativeWorkspacePopCount(state, dismissedKey);
36 return count
37 ? {
38 type: "POP" as const,
39 payload: { count },
40 source: state.routes[state.index]!.key,
41 target: state.key,
42 }
43 : null;
45​
46/** A native card pop must preserve pushes made before its completion event reaches JS. */
47export function nativeStackPopAction(
48 state: Pick<NavigationState, "key" | "index" | "routes">,
49 dismissedKey: string,
50 destinationKey?: string,
51) {
52 const sourceIndex = state.routes.findIndex((route) => route.key === dismissedKey);
53 const destinationIndex =
54 destinationKey === undefined
55 ? sourceIndex - 1
56 : state.routes.findIndex((route) => route.key === destinationKey);
57 if (
58 sourceIndex <= 0 ||
59 sourceIndex > state.index ||
60 destinationIndex < 0 ||
61 destinationIndex >= sourceIndex
62 ) {
63 return null;
64 }
65 return {
66 type: "POP" as const,
67 payload: { count: sourceIndex - destinationIndex },
68 source: dismissedKey,
69 target: state.key,
70 };
72​
73export function partitionStackPresentations<T>(
74 routes: readonly T[],
75 isModal: (route: T) => boolean,
76): T[][] {
77 const groups: T[][] = [];
78 for (const route of routes) {
79 if (groups.length === 0 || isModal(route)) groups.push([]);
80 groups.at(-1)!.push(route);
81 }
82 return groups;
84​
85/** Keep outgoing screens in place until dismissal; Fabric moves enqueue native pop/push operations. */
86export function reconcileStackScreens<T extends { readonly key: string }>(
87 previous: readonly T[],
88 current: readonly T[],
89 completedNativeDismissals: ReadonlySet<string> = new Set(),
90): T[] {
91 const active = new Set(current.map((route) => route.key));
92 const screens = [...current];
93 previous.forEach((route, index) => {
94 if (!active.has(route.key) && !completedNativeDismissals.has(route.key)) {
95 screens.splice(Math.min(index, screens.length), 0, route);
96 }
97 });
98 return screens;