GeezerVison is showing github.com — fix(mobile): custom header views no longer crash with "Couldn't find … · pingdotgg/t3code@0ff8d38
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/native/NativeHeaderToolbar.test.tsx +27-1Lines changed: 27 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | // @vitest-environment jsdom | 2 | 2 | import type { NativeStackHeaderItem } from "@react-navigation/native-stack"; | 3 | | -import { act } from "react"; | | 3 | +import { act, createContext, use } from "react"; | 4 | 4 | import { createRoot, type Root } from "react-dom/client"; | 5 | 5 | import { afterEach, expect, it, vi } from "vite-plus/test"; | 6 | 6 |
| @@ -99,3 +99,29 @@ it.each(["left", "right", "bottom"] as const)( | 99 | 99 | expect(harness.openedDocuments).toEqual(["first/guide.pdf"]); | 100 | 100 | }, | 101 | 101 | ); | | 102 | + | | 103 | +// React Navigation's context defaults throw from getters, and dev elements keep their owner fiber. | | 104 | +const ThrowingDefaultContext = createContext({ | | 105 | + get value(): never { | | 106 | + throw new Error("Couldn't find a navigation context."); | | 107 | + }, | | 108 | +}); | | 109 | + | | 110 | +function CustomViewToolbar() { | | 111 | + use(ThrowingDefaultContext); | | 112 | + return ( | | 113 | + <NativeHeaderToolbar placement="right"> | | 114 | + <NativeHeaderToolbar.Custom> | | 115 | + <span>Status</span> | | 116 | + </NativeHeaderToolbar.Custom> | | 117 | + </NativeHeaderToolbar> | | 118 | + ); | | 119 | +} | | 120 | + | | 121 | +it("applies custom header views created under contexts whose defaults throw", () => { | | 122 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); | | 123 | + root ??= createRoot(document.createElement("div")); | | 124 | + act(() => root!.render(<CustomViewToolbar />)); | | 125 | + | | 126 | + expect(harness.renderedItems.map((item) => item.type)).toEqual(["custom"]); | | 127 | +}); |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/native/StackHeader.tsx +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,7 @@ import type { | 3 | 3 | NativeStackNavigationOptions, | 4 | 4 | NativeStackNavigationProp, | 5 | 5 | } from "@react-navigation/native-stack"; | 6 | | -import { use, useEffect, useLayoutEffect, useMemo, useRef } from "react"; | | 6 | +import { isValidElement, use, useEffect, useLayoutEffect, useMemo, useRef } from "react"; | 7 | 7 | import { NativeWorkspaceTabActivityContext } from "./v5-workspace-context"; | 8 | 8 | import type { ColorValue } from "react-native"; | 9 | 9 |
| @@ -81,6 +81,13 @@ function optionsSignature(value: unknown, seen = new WeakSet<object>()): string | 81 | 81 | // React refs carry mutable native instances that must not make static | 82 | 82 | // screen options appear different after every render. | 83 | 83 | if ("current" in object) return "[ref]"; | | 84 | + // Custom header views are elements. Only their type, key, and props describe them; | | 85 | + // dev-only internals such as the owner fiber reach context defaults that throw. | | 86 | + if (isValidElement(object)) { | | 87 | + const type = object.type as { displayName?: string; name?: string } | string; | | 88 | + const typeName = typeof type === "string" ? type : (type.displayName ?? type.name); | | 89 | + return `element:${typeName}:${String(object.key)}:${optionsSignature(object.props, seen)}`; | | 90 | + } | 84 | 91 | return `{${Object.keys(value as Record<string, unknown>) | 85 | 92 | .sort() | 86 | 93 | .map( | |
|