Skip to content

Commit d6aa32c

Browse files
fix(mobile): new thread on branch no longer loops after the checkout
The route screen built the draft screen's project ref inline. The React Compiler does not memoize values that cross the screen's hooks, so the ref was a new object on every render; NewTaskDraftScreen re-applies the route project (and rewrites the draft's workspace) whenever that identity changes, and the write re-rendered the route into another write until React threw "Maximum update depth exceeded". Main memoized it by hand. The ref is now memoized on its fields, with a test that it keeps its identity across re-renders. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d2a23ac commit d6aa32c

2 files changed

Lines changed: 61 additions & 7 deletions

File tree

‎apps/mobile/src/features/threads/NewTaskDraftRouteScreen.test.tsx‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const harness = vi.hoisted(() => {
3434
route: { key: "thread-route", name: "Thread", params: {} as object },
3535
reset: () => undefined,
3636
checkout: vi.fn(),
37+
draftProjectRefs: [] as unknown[],
3738
// Hook results stay stable across renders, as the real hooks' are. Only
3839
// main's screen reads these (see the main-comparison mocks below).
3940
layout: { deferThreadSelectionUntil: () => undefined },
@@ -58,9 +59,10 @@ vi.mock("./ThreadScreenHost", () => ({
5859
ThreadScreenFallback: ({ children }: { children: ReactNode }) => <>{children}</>,
5960
}));
6061
vi.mock("./NewTaskDraftScreen", () => ({
61-
NewTaskDraftScreen: (props: { initialProjectRef: { branch?: string | null } }) => (
62-
<p>draft on {props.initialProjectRef.branch}</p>
63-
),
62+
NewTaskDraftScreen: (props: { initialProjectRef: { branch?: string | null } }) => {
63+
harness.draftProjectRefs.push(props.initialProjectRef);
64+
return <p>draft on {props.initialProjectRef.branch}</p>;
65+
},
6466
}));
6567
vi.mock("./new-task-context-presentation", () => ({
6668
resolveNewTaskBranchWorktreePath: () => null,
@@ -142,3 +144,34 @@ it("checks the branch out once when a reconnect drops and restores the project",
142144
expect(harness.checkout).toHaveBeenCalledTimes(1);
143145
expect(container.textContent).toBe("draft on feature");
144146
});
147+
148+
it("hands the draft screen the same project ref across re-renders", async () => {
149+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
150+
harness.checkout.mockReset();
151+
harness.draftProjectRefs.length = 0;
152+
harness.checkout.mockResolvedValue(
153+
AsyncResult.success({ name: "other", current: true, isDefault: false, worktreePath: null }),
154+
);
155+
harness.set({ projects: [project], catalog: connected });
156+
const draft = { environmentId: "env-1", projectId: "project-1", branch: "other" };
157+
harness.route = { key: "stable-route", name: "Thread", params: { draft } };
158+
159+
const container = document.createElement("div");
160+
const root = createRoot(container);
161+
using _root = { [Symbol.dispose]: () => act(() => root.unmount()) };
162+
act(() =>
163+
root.render(
164+
<NewTaskDraftRouteScreen
165+
route={{ key: "stable-route", name: "Thread", params: draft } as never}
166+
/>,
167+
),
168+
);
169+
await vi.waitFor(() => expect(container.textContent).toBe("draft on other"));
170+
171+
// The draft screen re-applies its route project when this identity changes,
172+
// and that write re-renders the route; an unstable ref loops forever.
173+
act(() => harness.set({ projects: [{ ...project }], catalog: { ...connected } }));
174+
const refs = new Set(harness.draftProjectRefs);
175+
expect(harness.draftProjectRefs.length).toBeGreaterThan(1);
176+
expect(refs.size).toBe(1);
177+
});

‎apps/mobile/src/features/threads/NewTaskDraftRouteScreen.tsx‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
usePreventRemoveContext,
88
type StaticScreenProps,
99
} from "@react-navigation/native";
10-
import { useEffect, useLayoutEffect, useState } from "react";
10+
import { useEffect, useLayoutEffect, useMemo, useState } from "react";
1111
import { View } from "react-native";
1212
import { useAtomValue } from "@effect/atom-react";
1313
import {
@@ -157,18 +157,39 @@ export function NewTaskDraftRouteScreen({ route }: StaticScreenProps<NewTaskDraf
157157
branchCheckouts.release(checkoutKey);
158158
}, [checkoutKey, checkoutPending, removalBlocked]);
159159

160-
const preparedProjectRef =
160+
const prepared =
161161
result?._tag === "Success" && workspaceRoot
162162
? {
163-
...initialProjectRef,
164163
branch: result.value.name,
165164
worktreePath: resolveNewTaskBranchWorktreePath({
166165
workspaceMode: "local",
167166
projectCwd: workspaceRoot,
168167
branchWorktreePath: result.value.worktreePath,
169168
}),
170169
}
171-
: initialProjectRef;
170+
: null;
171+
const preparedBranch = prepared ? prepared.branch : initialProjectRef.branch;
172+
const preparedWorktreePath = prepared ? prepared.worktreePath : initialProjectRef.worktreePath;
173+
// NewTaskDraftScreen re-applies the route project (and rewrites the draft's
174+
// workspace) whenever this object's identity changes. The compiler does not
175+
// memoize values that cross the hooks above, so key it on its fields;
176+
// otherwise every draft write re-renders into another write.
177+
const preparedProjectRef = useMemo(
178+
() => ({
179+
environmentId: initialProjectRef.environmentId,
180+
projectId: initialProjectRef.projectId,
181+
branch: preparedBranch,
182+
worktreePath: preparedWorktreePath,
183+
cloning: initialProjectRef.cloning,
184+
}),
185+
[
186+
initialProjectRef.environmentId,
187+
initialProjectRef.projectId,
188+
preparedBranch,
189+
preparedWorktreePath,
190+
initialProjectRef.cloning,
191+
],
192+
);
172193
const preparingBranch = checkoutPending || (needsPreparation && result?._tag !== "Success");
173194

174195
if (!initialized) return null;

0 commit comments

Comments
 (0)