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
| 1 | import { |
| 2 | NavigationContext, |
| 3 | NavigationRouteContext, |
| 4 | type ParamListBase, |
| 5 | } from "@react-navigation/native"; |
| 6 | import type { |
| 7 | NativeStackNavigationOptions, |
| 8 | NativeStackNavigationProp, |
| 9 | } from "@react-navigation/native-stack"; |
| 10 | import { isValidElement, use, useEffect, useLayoutEffect, useMemo, useRef } from "react"; |
| 11 | import { |
| 12 | NativeWorkspaceTabActivityContext, |
| 13 | WorkspaceSlotOptionsContext, |
| 14 | } from "./v5-workspace-context"; |
| 15 | import type { ColorValue } from "react-native"; |
| 16 | |
| 17 | export { |
| 18 | nativeHeaderScrollEdgeEffects, |
| 19 | type NativeHeaderScrollEdgeEffects, |
| 20 | type NativeTopScrollEdgeEffect, |
| 21 | } from "./scrollEdgeEffects"; |
| 22 | |
| 23 | export type AppNativeStackNavigationOptions = Omit< |
| 24 | NativeStackNavigationOptions, |
| 25 | "headerTintColor" | "unstable_headerLeftItems" | "unstable_headerRightItems" |
| 26 | > & { |
| 27 | readonly nativeContentInsetHorizontally?: boolean; |
| 28 | readonly headerTintColor?: string | ColorValue; |
| 29 | readonly unstable_headerCenterItems?: unknown; |
| 30 | readonly unstable_headerLeftItems?: unknown; |
| 31 | readonly unstable_headerRightItems?: unknown; |
| 32 | readonly unstable_headerSubtitle?: unknown; |
| 33 | readonly unstable_headerToolbarItems?: unknown; |
| 34 | readonly unstable_navigationItemStyle?: unknown; |
| 35 | readonly unstable_headerMinimizationBehavior?: unknown; |
| 36 | }; |
| 37 | |
| 38 | function useNativeStackNavigation(): NativeStackNavigationProp<ParamListBase> | null { |
| 39 | const navigation = use(NavigationContext); |
| 40 | return navigation ? (navigation as NativeStackNavigationProp<ParamListBase>) : null; |
| 41 | } |
| 42 | |
| 43 | function normalizeScreenOptions( |
| 44 | options: AppNativeStackNavigationOptions | undefined, |
| 45 | ): NativeStackNavigationOptions | undefined { |
| 46 | if (!options) { |
| 47 | return options; |
| 48 | } |
| 49 | |
| 50 | const normalized = { ...options } as NativeStackNavigationOptions & { |
| 51 | unstable_navigationItemStyle?: unknown; |
| 52 | unstable_headerCenterItems?: unknown; |
| 53 | unstable_headerSubtitle?: unknown; |
| 54 | unstable_headerToolbarItems?: unknown; |
| 55 | }; |
| 56 | |
| 57 | if (normalized.headerTintColor !== undefined) { |
| 58 | normalized.headerTintColor = String(normalized.headerTintColor); |
| 59 | } |
| 60 | |
| 61 | return normalized as NativeStackNavigationOptions; |
| 62 | } |
| 63 | |
| 64 | // React tags elements, component types and contexts with `$$typeof`; their other |
| 65 | // fields are React internals. In development an element's `_owner` is a fiber that |
| 66 | // reaches the whole tree, including contexts that hold their default value outside |
| 67 | // render, and React Navigation's NavigationStateContext default throws when read. |
| 68 | function isReactObject(value: object): boolean { |
| 69 | return "$$typeof" in value; |
| 70 | } |
| 71 | |
| 72 | const identityIds = new WeakMap<object, number>(); |
| 73 | let nextIdentityId = 0; |
| 74 | |
| 75 | function identitySignature(value: unknown): string { |
| 76 | if (value === null || (typeof value !== "object" && typeof value !== "function")) { |
| 77 | return String(value); |
| 78 | } |
| 79 | let id = identityIds.get(value); |
| 80 | if (id === undefined) { |
| 81 | id = nextIdentityId++; |
| 82 | identityIds.set(value, id); |
| 83 | } |
| 84 | return `identity:${id}`; |
| 85 | } |
| 86 | |
| 87 | function optionsSignature(value: unknown, seen = new WeakSet<object>()): string { |
| 88 | if (value === null) return "null"; |
| 89 | switch (typeof value) { |
| 90 | case "boolean": |
| 91 | case "number": |
| 92 | case "string": |
| 93 | return JSON.stringify(value); |
| 94 | case "undefined": |
| 95 | return "undefined"; |
| 96 | case "function": |
| 97 | // Compare factory source; inline references change every render and would loop setOptions. |
| 98 | return `function:${Function.prototype.toString.call(value)}`; |
| 99 | case "symbol": |
| 100 | return `symbol:${String(value)}`; |
| 101 | case "bigint": |
| 102 | return `bigint:${String(value)}`; |
| 103 | case "object": { |
| 104 | const object = value as object; |
| 105 | if (isValidElement(object)) { |
| 106 | // Elements are recreated on every render, so compare what they render. |
| 107 | return `element:${identitySignature(object.type)}:${String(object.key)}:${optionsSignature(object.props, seen)}`; |
| 108 | } |
| 109 | if (isReactObject(object)) return identitySignature(object); |
| 110 | if (seen.has(object)) return "[circular]"; |
| 111 | seen.add(object); |
| 112 | if (Array.isArray(value)) { |
| 113 | return `[${value.map((entry) => optionsSignature(entry, seen)).join(",")}]`; |
| 114 | } |
| 115 | // Ignore mutable native refs when comparing static screen options. |
| 116 | if ("current" in object) return "[ref]"; |
| 117 | return `{${Object.keys(value as Record<string, unknown>) |
| 118 | .sort() |
| 119 | .map( |
| 120 | (key) => |
| 121 | `${JSON.stringify(key)}:${optionsSignature((value as Record<string, unknown>)[key], seen)}`, |
| 122 | ) |
| 123 | .join(",")}}`; |
| 124 | } |
| 125 | } |
| 126 | return String(value); |
| 127 | } |
| 128 | |
| 129 | function stabilizeOptionFunctions( |
| 130 | value: unknown, |
| 131 | path: string, |
| 132 | latestFunctions: Map<string, (...args: unknown[]) => unknown>, |
| 133 | wrappers: Map<string, (...args: unknown[]) => unknown>, |
| 134 | seen = new WeakSet<object>(), |
| 135 | ): unknown { |
| 136 | if (typeof value === "function") { |
| 137 | latestFunctions.set(path, value as (...args: unknown[]) => unknown); |
| 138 | let wrapper = wrappers.get(path); |
| 139 | if (!wrapper) { |
| 140 | wrapper = (...args: unknown[]) => { |
| 141 | return latestFunctions.get(path)?.(...args); |
| 142 | }; |
| 143 | wrappers.set(path, wrapper); |
| 144 | } |
| 145 | return wrapper; |
| 146 | } |
| 147 | if (Array.isArray(value)) { |
| 148 | if (seen.has(value)) return value; |
| 149 | seen.add(value); |
| 150 | return value.map((entry, index) => |
| 151 | stabilizeOptionFunctions(entry, `${path}[${index}]`, latestFunctions, wrappers, seen), |
| 152 | ); |
| 153 | } |
| 154 | if (value !== null && typeof value === "object") { |
| 155 | if (seen.has(value) || "current" in value || isReactObject(value)) return value; |
| 156 | seen.add(value); |
| 157 | return Object.fromEntries( |
| 158 | Object.entries(value as Record<string, unknown>).map(([key, entry]) => [ |
| 159 | key, |
| 160 | stabilizeOptionFunctions(entry, `${path}.${key}`, latestFunctions, wrappers, seen), |
| 161 | ]), |
| 162 | ); |
| 163 | } |
| 164 | return value; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Sets the current screen's options. Inside a workspace tab slot they're kept for that slot, so |
| 169 | * slots rendering the same route never overwrite each other's header. |
| 170 | */ |
| 171 | export function useScreenOptionsWriter() { |
| 172 | const navigation = useNativeStackNavigation(); |
| 173 | const setSlotOptions = use(WorkspaceSlotOptionsContext)?.set; |
| 174 | const routeKey = use(NavigationRouteContext)?.key; |
| 175 | return useMemo(() => { |
| 176 | if (setSlotOptions && routeKey !== undefined) |
| 177 | return (options: NativeStackNavigationOptions) => setSlotOptions(routeKey, options); |
| 178 | return navigation |
| 179 | ? (options: NativeStackNavigationOptions) => navigation.setOptions(options) |
| 180 | : null; |
| 181 | }, [navigation, routeKey, setSlotOptions]); |
| 182 | } |
| 183 | |
| 184 | export function NativeStackScreenOptions(props: { |
| 185 | readonly options?: AppNativeStackNavigationOptions; |
| 186 | /** Reapply factories when captured data changes; source equality misses asynchronous menu content. */ |
| 187 | readonly optionsVersion?: unknown; |
| 188 | readonly listeners?: Record<string, (event: never) => void>; |
| 189 | readonly name?: string; |
| 190 | }) { |
| 191 | const navigation = useNativeStackNavigation(); |
| 192 | const setOptions = useScreenOptionsWriter(); |
| 193 | const tabActivity = use(NativeWorkspaceTabActivityContext); |
| 194 | const tabActive = tabActivity ? tabActivity.active || tabActivity.headerOnly === true : true; |
| 195 | const lastAppliedOptionsSignatureRef = useRef<string | undefined>(undefined); |
| 196 | const latestOptionFunctionsRef = useRef(new Map<string, (...args: unknown[]) => unknown>()); |
| 197 | const optionFunctionWrappersRef = useRef(new Map<string, (...args: unknown[]) => unknown>()); |
| 198 | |
| 199 | // Stabilizing records each factory's latest closure, so it runs in the commit |
| 200 | // phase: a render React discards must not leak its closures into the header. |
| 201 | // It runs before the tab check so wrappers already installed in an inactive |
| 202 | // tab's header still call the newest closures. |
| 203 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- writes stabilized header options to the navigator after commit so discarded renders leave no closures |
| 204 | useLayoutEffect(() => { |
| 205 | const normalizedOptions = normalizeScreenOptions(props.options); |
| 206 | const stableOptions = normalizedOptions |
| 207 | ? stabilizeOptionFunctions( |
| 208 | normalizedOptions, |
| 209 | "options", |
| 210 | latestOptionFunctionsRef.current, |
| 211 | optionFunctionWrappersRef.current, |
| 212 | ) |
| 213 | : undefined; |
| 214 | if (!tabActive) { |
| 215 | lastAppliedOptionsSignatureRef.current = undefined; |
| 216 | return; |
| 217 | } |
| 218 | if (!setOptions || !normalizedOptions) { |
| 219 | return; |
| 220 | } |
| 221 | const signature = optionsSignature([stableOptions, props.optionsVersion]); |
| 222 | if (lastAppliedOptionsSignatureRef.current === signature) { |
| 223 | return; |
| 224 | } |
| 225 | lastAppliedOptionsSignatureRef.current = signature; |
| 226 | // React Navigation keeps a screen's descriptor while its options are shallowly equal. The |
| 227 | // stabilized factories keep their identity, so one whose output changed needs a new wrapper |
| 228 | // to reach the header. |
| 229 | optionFunctionWrappersRef.current = new Map(); |
| 230 | setOptions( |
| 231 | stabilizeOptionFunctions( |
| 232 | normalizedOptions, |
| 233 | "options", |
| 234 | latestOptionFunctionsRef.current, |
| 235 | optionFunctionWrappersRef.current, |
| 236 | ) as NativeStackNavigationOptions, |
| 237 | ); |
| 238 | }, [setOptions, props.options, props.optionsVersion, tabActive]); |
| 239 | |
| 240 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes the given listeners to navigator events for the screen's lifetime |
| 241 | useEffect(() => { |
| 242 | if (!navigation || !props.listeners) { |
| 243 | return; |
| 244 | } |
| 245 | const subscriptions = Object.entries(props.listeners).map(([eventName, listener]) => |
| 246 | navigation.addListener(eventName as never, listener as never), |
| 247 | ); |
| 248 | return () => { |
| 249 | for (const unsubscribe of subscriptions) { |
| 250 | unsubscribe(); |
| 251 | } |
| 252 | }; |
| 253 | }, [navigation, props.listeners]); |
| 254 | |
| 255 | return null; |
| 256 | } |