GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(mobile): connecting an environment no longer crashes the header (… · pingdotgg/t3code@c8667fd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/mobile/src/native
    • NativeHeaderToolbar.test.tsx
    • StackHeader.tsx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/mobile/src/native/NativeHeaderToolbar.test.tsx‎

Copy file name to clipboardExpand all lines: apps/mobile/src/native/NativeHeaderToolbar.test.tsx
+50-1Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { NativeStackHeaderItem } from "@react-navigation/native-stack";
2-
import { act } from "react";
2+
import { act, createContext, type ReactElement } from "react";
33
import { createRoot, type Root } from "react-dom/client";
44
import { afterEach, expect, it, vi } from "vite-plus/test";
55

@@ -56,6 +56,43 @@ function renderDocument(path: string, placement: Placement) {
5656
act(() => root!.render(<DocumentToolbar path={path} placement={placement} />));
5757
}
5858

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+
5996
function openRenderedDocument() {
6097
const menu = harness.renderedItems[0];
6198
if (menu?.type !== "menu") throw new Error("Expected the native file menu");
@@ -98,3 +135,15 @@ it.each(["left", "right", "bottom"] as const)(
98135
expect(harness.openedDocuments).toEqual(["first/guide.pdf"]);
99136
},
100137
);
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+
});
Collapse file

‎apps/mobile/src/native/StackHeader.tsx‎

Copy file name to clipboardExpand all lines: apps/mobile/src/native/StackHeader.tsx
+30-2Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
NativeStackNavigationOptions,
55
NativeStackNavigationProp,
66
} from "@react-navigation/native-stack";
7-
import { use, useEffect, useLayoutEffect, useMemo, useRef } from "react";
7+
import { isValidElement, use, useEffect, useLayoutEffect, useMemo, useRef } from "react";
88
import { NativeWorkspaceTabActivityContext } from "./v5-workspace-context";
99
import type { ColorValue } from "react-native";
1010

@@ -55,6 +55,29 @@ function normalizeScreenOptions(
5555
return normalized as NativeStackNavigationOptions;
5656
}
5757

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+
5881
function optionsSignature(value: unknown, seen = new WeakSet<object>()): string {
5982
if (value === null) return "null";
6083
switch (typeof value) {
@@ -75,6 +98,11 @@ function optionsSignature(value: unknown, seen = new WeakSet<object>()): string
7598
return `bigint:${String(value)}`;
7699
case "object": {
77100
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);
78106
if (seen.has(object)) return "[circular]";
79107
seen.add(object);
80108
if (Array.isArray(value)) {
@@ -121,7 +149,7 @@ function stabilizeOptionFunctions(
121149
);
122150
}
123151
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;
125153
seen.add(value);
126154
return Object.fromEntries(
127155
Object.entries(value as Record<string, unknown>).map(([key, entry]) => [

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.