@devries-cameron20 / t3code-1 / commits / c8667fd5f4
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(mobile): connecting an environment no longer crashes the header (#22521)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 20, 2026 at 15:07 UTC c8667fd5f4cd635b82b70d9e082c98c06eb78dcc
2 files changed +80 -3
apps/mobile/src/native/NativeHeaderToolbar.test.tsx
+50 -1
@@ -1,5 +1,5 @@
1 import type { NativeStackHeaderItem } from "@react-navigation/native-stack";
2 -import { act } from "react";
2 +import { act, createContext, type ReactElement } from "react";
3 import { createRoot, type Root } from "react-dom/client";
4 import { afterEach, expect, it, vi } from "vite-plus/test";
5
@@ -56,6 +56,43 @@ function renderDocument(path: string, placement: Placement) {
56 act(() => root!.render(<DocumentToolbar path={path} placement={placement} />));
57 }
58
59 +// Like React Navigation's NavigationStateContext, whose default value throws when
60 +// read. React restores a context's default outside render, so layout effects see it.
61 +const ContainerStateContext = createContext<object>({
62 + get getIsInitial(): never {
63 + throw new Error("Couldn't find a navigation context.");
64 + },
65 +});
66 +const containerState = {};
67 +
68 +function ConnectingToolbar(props: { readonly label: string }) {
69 + return (
70 + <NativeHeaderToolbar placement="right">
71 + <NativeHeaderToolbar.Custom>
72 + <span aria-label={props.label} />
73 + </NativeHeaderToolbar.Custom>
74 + </NativeHeaderToolbar>
75 + );
76 +}
77 +
78 +function renderConnecting(label: string) {
79 + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
80 + root ??= createRoot(document.createElement("div"));
81 + act(() =>
82 + root!.render(
83 + <ContainerStateContext value={containerState}>
84 + <ConnectingToolbar label={label} />
85 + </ContainerStateContext>,
86 + ),
87 + );
88 +}
89 +
90 +function renderedCustomLabel() {
91 + const item = harness.renderedItems[0];
92 + if (item?.type !== "custom") throw new Error("Expected the custom header element");
93 + return (item.element as ReactElement<{ "aria-label": string }>).props["aria-label"];
94 +}
95 +
96 function openRenderedDocument() {
97 const menu = harness.renderedItems[0];
98 if (menu?.type !== "menu") throw new Error("Expected the native file menu");
@@ -98,3 +135,15 @@ it.each(["left", "right", "bottom"] as const)(
135 expect(harness.openedDocuments).toEqual(["first/guide.pdf"]);
136 },
137 );
138 +
139 +it("applies a custom header element rendered inside a navigation container", () => {
140 + renderConnecting("Connecting");
141 + expect(renderedCustomLabel()).toBe("Connecting");
142 +
143 + const applied = harness.setOptionsCalls;
144 + renderConnecting("Connecting");
145 + expect(harness.setOptionsCalls).toBe(applied);
146 +
147 + renderConnecting("Reconnecting");
148 + expect(renderedCustomLabel()).toBe("Reconnecting");
149 +});
apps/mobile/src/native/StackHeader.tsx
+30 -2
@@ -4,7 +4,7 @@ import type {
4 NativeStackNavigationOptions,
5 NativeStackNavigationProp,
6 } from "@react-navigation/native-stack";
7 -import { use, useEffect, useLayoutEffect, useMemo, useRef } from "react";
7 +import { isValidElement, use, useEffect, useLayoutEffect, useMemo, useRef } from "react";
8 import { NativeWorkspaceTabActivityContext } from "./v5-workspace-context";
9 import type { ColorValue } from "react-native";
10
@@ -55,6 +55,29 @@ function normalizeScreenOptions(
55 return normalized as NativeStackNavigationOptions;
56 }
57
58 +// React tags elements, component types and contexts with `$$typeof`; their other
59 +// fields are React internals. In development an element's `_owner` is a fiber that
60 +// reaches the whole tree, including contexts that hold their default value outside
61 +// render, and React Navigation's NavigationStateContext default throws when read.
62 +function isReactObject(value: object): boolean {
63 + return "$$typeof" in value;
64 +}
65 +
66 +const identityIds = new WeakMap<object, number>();
67 +let nextIdentityId = 0;
68 +
69 +function identitySignature(value: unknown): string {
70 + if (value === null || (typeof value !== "object" && typeof value !== "function")) {
71 + return String(value);
72 + }
73 + let id = identityIds.get(value);
74 + if (id === undefined) {
75 + id = nextIdentityId++;
76 + identityIds.set(value, id);
77 + }
78 + return `identity:${id}`;
79 +}
80 +
81 function optionsSignature(value: unknown, seen = new WeakSet<object>()): string {
82 if (value === null) return "null";
83 switch (typeof value) {
@@ -75,6 +98,11 @@ function optionsSignature(value: unknown, seen = new WeakSet<object>()): string
98 return `bigint:${String(value)}`;
99 case "object": {
100 const object = value as object;
101 + if (isValidElement(object)) {
102 + // Elements are recreated on every render, so compare what they render.
103 + return `element:${identitySignature(object.type)}:${String(object.key)}:${optionsSignature(object.props, seen)}`;
104 + }
105 + if (isReactObject(object)) return identitySignature(object);
106 if (seen.has(object)) return "[circular]";
107 seen.add(object);
108 if (Array.isArray(value)) {
@@ -121,7 +149,7 @@ function stabilizeOptionFunctions(
149 );
150 }
151 if (value !== null && typeof value === "object") {
124 - if (seen.has(value) || "current" in value) return value;
152 + if (seen.has(value) || "current" in value || isReactObject(value)) return value;
153 seen.add(value);
154 return Object.fromEntries(
155 Object.entries(value as Record<string, unknown>).map(([key, entry]) => [