Skip to content

Commit 86b780b

Browse files
maria-rcksflamboh
andcommitted
fix(mobile): keep split-view inspectors registered across renders
Co-authored-by: oliver <[email protected]>
1 parent 122c5f8 commit 86b780b

3 files changed

Lines changed: 142 additions & 17 deletions

File tree

‎apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ function useThreadFilesWorkspace(params: {
361361
readonly threadId?: string | string[];
362362
readonly cwd?: string | string[];
363363
readonly projectName?: string | string[];
364+
readonly path?: string | string[];
364365
}) {
365366
const routeEnvironmentId = firstRouteParam(params.environmentId);
366367
const routeThreadId = firstRouteParam(params.threadId);
@@ -382,6 +383,7 @@ function useThreadFilesWorkspace(params: {
382383
cwd: routeCwd ?? selectedThreadCwd ?? project?.workspaceRoot ?? null,
383384
environmentId,
384385
projectName: routeProjectName ?? project?.title ?? "Files",
386+
relativePath: normalizeRoutePath(params.path),
385387
selectedThread,
386388
threadId,
387389
};
@@ -594,11 +596,9 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
594596
const { appearance, setCodeWordBreak } = useAppearancePreferences();
595597
const iconColor = useUniwindTheme()["--color-icon"];
596598
const params = props.route.params;
597-
const relativePath = normalizeRoutePath(params.path);
598599
const targetLine = normalizeRouteLine(firstRouteParam(params.line));
599-
const { cwd, environmentId, projectName, selectedThread, threadId } = useThreadFilesWorkspace(
600-
props.route.params,
601-
);
600+
const { cwd, environmentId, projectName, relativePath, selectedThread, threadId } =
601+
useThreadFilesWorkspace(props.route.params);
602602
const fileAccessSession = useEnvironmentQuery(
603603
environmentId !== null ? environmentSession.sessionStateAtom(environmentId) : null,
604604
);

‎apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,28 +135,34 @@ export function useRegisterWorkspaceInspector(render: (() => ReactNode) | undefi
135135
deactivateRef.current = registerWorkspaceInspector(wrappedRender);
136136
});
137137

138-
useFocusEffect(() => {
139-
focusedRef.current = true;
140-
syncRegistration();
141-
return () => {
138+
useEffect(() => {
139+
if (navigation === undefined) return;
140+
const focus = () => {
141+
if (focusedRef.current) return;
142+
focusedRef.current = true;
143+
syncRegistration();
144+
};
145+
const blur = () => {
142146
focusedRef.current = false;
143147
syncRegistration();
144148
};
145-
});
149+
if (navigation.isFocused()) focus();
150+
const unsubscribeFocus = navigation.addListener("focus", focus);
151+
const unsubscribeBlur = navigation.addListener("blur", blur);
152+
return () => {
153+
unsubscribeFocus();
154+
unsubscribeBlur();
155+
focusedRef.current = false;
156+
deactivateRef.current?.();
157+
deactivateRef.current = null;
158+
};
159+
}, [navigation]);
146160

147161
useEffect(() => {
148162
if (focusedRef.current) {
149163
syncRegistration();
150164
}
151165
}, [wrappedRender]);
152-
153-
useEffect(
154-
() => () => {
155-
deactivateRef.current?.();
156-
deactivateRef.current = null;
157-
},
158-
[],
159-
);
160166
}
161167

162168
export function AdaptiveWorkspaceLayout(props: {

‎apps/mobile/src/features/threads/thread-inspector-navigation.test.tsx‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import * as NodePath from "node:path";
12
import * as NodeModule from "node:module";
3+
import * as React from "react";
24
import { act, useLayoutEffect } from "react";
35
import { createRoot } from "react-dom/client";
46
import { expect, it, vi } from "vite-plus/test";
@@ -167,3 +169,120 @@ it("keeps the parent when the maximized child's Agents sheet opens another child
167169
expect(screen.open({ type: "thread", ...child, threadId: "grandchild" }, child)).toBe(false);
168170
expect(screen.state).toEqual(originalState);
169171
});
172+
173+
it("keeps a focused inspector registered across renders and updates its content", () => {
174+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
175+
const requireMobile = NodeModule.createRequire(import.meta.url);
176+
const listeners = { focus: new Set<() => void>(), blur: new Set<() => void>() };
177+
let focused = true;
178+
let activeRender: (() => React.ReactNode) | undefined;
179+
const deactivate = vi.fn(() => {
180+
activeRender = undefined;
181+
});
182+
const registerWorkspaceInspector = vi.fn((render: () => React.ReactNode) => {
183+
activeRender = render;
184+
return deactivate;
185+
});
186+
const navigation = {
187+
isFocused: () => focused,
188+
addListener(event: "focus" | "blur", listener: () => void) {
189+
listeners[event].add(listener);
190+
return () => {
191+
listeners[event].delete(listener);
192+
};
193+
},
194+
};
195+
const navigationContext = React.createContext(navigation);
196+
const routeContext = React.createContext({ key: "file" });
197+
const babel = requireMobile("@babel/core") as {
198+
transformFileSync(file: string, options: Record<string, unknown>): { code: string };
199+
};
200+
const compiled = babel.transformFileSync(
201+
NodePath.resolve(__dirname, "../layout/AdaptiveWorkspaceLayout.tsx"),
202+
{
203+
babelrc: false,
204+
configFile: false,
205+
presets: [
206+
[
207+
requireMobile.resolve("babel-preset-expo"),
208+
{ unstable_transformImportMeta: true, enableBabelRuntime: false },
209+
],
210+
],
211+
caller: {
212+
name: "metro",
213+
bundler: "metro",
214+
platform: "ios",
215+
isDev: false,
216+
isServer: false,
217+
isNodeModule: false,
218+
supportsStaticESM: false,
219+
supportsReactCompiler: true,
220+
},
221+
},
222+
);
223+
const exports: {
224+
useRegisterWorkspaceInspector?: (render: (() => React.ReactNode) | undefined) => void;
225+
} = {};
226+
const load = (name: string) => {
227+
if (name === "react" || name.startsWith("react/")) return requireMobile(name);
228+
if (name === "./adaptive-workspace-context")
229+
return {
230+
useAdaptiveWorkspaceLayout: () => ({ registerWorkspaceInspector }),
231+
};
232+
if (name === "@react-navigation/native")
233+
return {
234+
NavigationContext: navigationContext,
235+
NavigationRouteContext: routeContext,
236+
useFocusEffect: (callback: () => (() => void) | undefined) =>
237+
React.useEffect(callback, [callback]),
238+
};
239+
return {};
240+
};
241+
new Function("require", "exports", compiled.code)(load, exports);
242+
const useRegisterWorkspaceInspector = exports.useRegisterWorkspaceInspector!;
243+
function Screen({ render }: { render: (() => React.ReactNode) | undefined }) {
244+
useRegisterWorkspaceInspector(render);
245+
return null;
246+
}
247+
const root = createRoot(document.createElement("div"));
248+
using _root = { [Symbol.dispose]: () => act(() => root.unmount()) };
249+
const render = (content: (() => React.ReactNode) | undefined) =>
250+
act(() => root.render(<Screen render={content} />));
251+
const first = () => "first";
252+
render(first);
253+
registerWorkspaceInspector.mockClear();
254+
deactivate.mockClear();
255+
render(first);
256+
render(first);
257+
expect(registerWorkspaceInspector).not.toHaveBeenCalled();
258+
expect(deactivate).not.toHaveBeenCalled();
259+
const second = () => "second";
260+
render(second);
261+
expect(registerWorkspaceInspector).toHaveBeenCalledTimes(1);
262+
const paneContainer = document.createElement("div");
263+
const paneRoot = createRoot(paneContainer);
264+
using _pane = { [Symbol.dispose]: () => act(() => paneRoot.unmount()) };
265+
act(() => paneRoot.render(activeRender?.()));
266+
expect(paneContainer.textContent).toBe("second");
267+
const emit = (event: "focus" | "blur") =>
268+
act(() => {
269+
focused = event === "focus";
270+
for (const listener of listeners[event]) listener();
271+
});
272+
emit("blur");
273+
expect(deactivate).toHaveBeenCalledTimes(1);
274+
registerWorkspaceInspector.mockClear();
275+
render(first);
276+
expect(registerWorkspaceInspector).not.toHaveBeenCalled();
277+
emit("focus");
278+
expect(registerWorkspaceInspector).toHaveBeenCalledTimes(1);
279+
emit("focus");
280+
expect(registerWorkspaceInspector).toHaveBeenCalledTimes(1);
281+
render(undefined);
282+
expect(activeRender).toBeUndefined();
283+
render(second);
284+
expect(activeRender).toBeDefined();
285+
act(() => root.unmount());
286+
expect(activeRender).toBeUndefined();
287+
expect(listeners.focus.size + listeners.blur.size).toBe(0);
288+
});

0 commit comments

Comments
 (0)