Skip to content

Commit 95d13c0

Browse files
fix(mobile): the draft route's project ref is memoized by the compiler, not by hand
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d6aa32c commit 95d13c0

2 files changed

Lines changed: 15 additions & 23 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ it("hands the draft screen the same project ref across re-renders", async () =>
171171
// The draft screen re-applies its route project when this identity changes,
172172
// and that write re-renders the route; an unstable ref loops forever.
173173
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);
174+
// A stable ref lets the compiled screen skip re-rendering the draft screen at all; an unstable
175+
// one re-renders it with a second ref.
176+
expect(new Set(harness.draftProjectRefs).size).toBe(1);
177177
});

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

Lines changed: 12 additions & 20 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, useMemo, useState } from "react";
10+
import { useEffect, useLayoutEffect, useState } from "react";
1111
import { View } from "react-native";
1212
import { useAtomValue } from "@effect/atom-react";
1313
import {
@@ -171,25 +171,17 @@ export function NewTaskDraftRouteScreen({ route }: StaticScreenProps<NewTaskDraf
171171
const preparedBranch = prepared ? prepared.branch : initialProjectRef.branch;
172172
const preparedWorktreePath = prepared ? prepared.worktreePath : initialProjectRef.worktreePath;
173173
// 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-
);
174+
// workspace) whenever this object's identity changes. initialProjectRef is new
175+
// every render, so read its fields first: the compiler then memoizes this
176+
// object on them, instead of on initialProjectRef.
177+
const { environmentId, projectId, cloning } = initialProjectRef;
178+
const preparedProjectRef = {
179+
environmentId,
180+
projectId,
181+
branch: preparedBranch,
182+
worktreePath: preparedWorktreePath,
183+
cloning,
184+
};
193185
const preparingBranch = checkoutPending || (needsPreparation && result?._tag !== "Success");
194186

195187
if (!initialized) return null;

0 commit comments

Comments
 (0)