Skip to content

Commit 2984931

Browse files
committed
fix(mobile): preserve newline-containing thread list keys
1 parent a619829 commit 2984931

4 files changed

Lines changed: 42 additions & 16 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { act, useLayoutEffect } from "react";
2+
import { createRoot } from "react-dom/client";
3+
import { expect, it } from "vite-plus/test";
4+
5+
import { useKeySet } from "./useKeySet";
6+
7+
it("preserves embedded newlines without changing identity for reordered keys", async () => {
8+
const seen: ReadonlySet<string>[] = [];
9+
function Probe({ keys }: { readonly keys: ReadonlyArray<string> }) {
10+
const value = useKeySet(keys);
11+
useLayoutEffect(() => {
12+
seen.push(value);
13+
});
14+
return null;
15+
}
16+
const root = createRoot(document.createElement("div"));
17+
try {
18+
await act(async () => root.render(<Probe keys={["a\nb", "c"]} />));
19+
expect(seen.at(-1)).toEqual(new Set(["a\nb", "c"]));
20+
const original = seen.at(-1);
21+
await act(async () => root.render(<Probe keys={["c", "a\nb"]} />));
22+
expect(seen.at(-1)).toBe(original);
23+
await act(async () => root.render(<Probe keys={["a", "b", "c"]} />));
24+
expect(seen.at(-1)).toEqual(new Set(["a", "b", "c"]));
25+
expect(seen.at(-1)).not.toBe(original);
26+
await act(async () => root.render(<Probe keys={[]} />));
27+
expect(seen.at(-1)?.size).toBe(0);
28+
} finally {
29+
act(() => root.unmount());
30+
}
31+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { useMemo } from "react";
2+
3+
// Stable key sets keep thread row selection handlers stable across list rebuilds.
4+
export function useKeySet(keys: ReadonlyArray<string>): ReadonlySet<string> {
5+
const serializedKeys = JSON.stringify([...keys].sort());
6+
return useMemo(() => new Set(JSON.parse(serializedKeys) as string[]), [serializedKeys]);
7+
}

‎apps/mobile/src/features/threads/useThreadListV2.ts‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,7 @@ import {
1414
threadListInboxReturns,
1515
} from "./threadListV2";
1616
import { computeThreadMoveAvailability } from "./threadOrder";
17-
18-
/**
19-
* Screens' select handlers read the content-only key set; a fresh one per list rebuild gave
20-
* every row a new handler whenever any thread shell changed. Keyed by a string, the set keeps
21-
* its identity until its keys change.
22-
*/
23-
function useKeySet(newlineSeparatedKeys: string): ReadonlySet<string> {
24-
return useMemo(
25-
() => new Set(newlineSeparatedKeys.length === 0 ? [] : newlineSeparatedKeys.split("\n")),
26-
[newlineSeparatedKeys],
27-
);
28-
}
17+
import { useKeySet } from "./useKeySet";
2918

3019
type LayoutInput = Parameters<typeof buildThreadListV2Items>[0];
3120

@@ -71,9 +60,7 @@ export function useThreadListV2(input: {
7160
const disconnectedEnvironmentIds = useKeySet(
7261
useWorkspaceEnvironments()
7362
.filter((environment) => isThreadListV2EnvironmentDisconnected(environment.connectionState))
74-
.map((environment) => environment.environmentId)
75-
.sort()
76-
.join("\n"),
63+
.map((environment) => environment.environmentId),
7764
);
7865
// Up/down menu availability for every card, computed once per section per
7966
// rebuild (see computeThreadMoveAvailability): per-thread planner calls made
@@ -141,7 +128,7 @@ export function useThreadListV2(input: {
141128
),
142129
});
143130
useOlderSettledShelfGrowth(olderSettledShelf, layout.listedSettledThreads);
144-
const contentOnlyThreadKeys = useKeySet([...layout.contentOnlyThreadKeys].sort().join("\n"));
131+
const contentOnlyThreadKeys = useKeySet([...layout.contentOnlyThreadKeys]);
145132
const scopedProjectKeys =
146133
input.projectRefs == null
147134
? null

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const reactTests = [
3535
"src/state/use-selected-thread-git-state.test.tsx",
3636
"src/features/terminal/useTerminalExitNavigation.test.tsx",
3737
"src/features/threads/use-thread-list-now.test.tsx",
38+
"src/features/threads/useKeySet.test.tsx",
3839
"src/features/home/home-list-options.test.tsx",
3940
"src/features/connection/ConnectionsNewRouteScreen.test.tsx",
4041
];

0 commit comments

Comments
 (0)