Skip to content

Commit b238f41

Browse files
committed
fix(mobile): preserve chat caches while tabs are hidden
1 parent cbe1a08 commit b238f41

4 files changed

Lines changed: 112 additions & 9 deletions

File tree

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search";
1313
import { type EnvironmentId, type SidebarProjectGroupingMode } from "@t3tools/contracts";
1414
import { useAtomValue } from "@effect/atom-react";
15-
import { use, useEffect, useRef, useState, type ComponentProps } from "react";
15+
import { use, useEffect, useMemo, useRef, useState, type ComponentProps } from "react";
1616
import {
1717
Platform,
1818
ScrollView,
@@ -361,11 +361,16 @@ export function HomeScreen(props: HomeScreenProps) {
361361
onScrollBeginDrag: handleScrollBeginDrag,
362362
});
363363

364-
const projectScopes = buildHomeProjectScopes({
365-
projects: props.projects,
366-
environmentId: props.selectedEnvironmentId,
367-
projectGroupingMode: props.projectGroupingMode,
368-
});
364+
const { projects, selectedEnvironmentId, projectGroupingMode } = props;
365+
const projectScopes = useMemo(
366+
() =>
367+
buildHomeProjectScopes({
368+
projects,
369+
environmentId: selectedEnvironmentId,
370+
projectGroupingMode,
371+
}),
372+
[projects, selectedEnvironmentId, projectGroupingMode],
373+
);
369374
const hasSearchQuery = props.searchQuery.trim().length > 0;
370375
const projectByKey = (() => {
371376
const map = new Map<string, EnvironmentProject>();

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,26 @@ it("reuses a large thread list across tab renders and updates when its inputs ch
113113
);
114114
expect(seen.at(-1)!.layout.items.map((item) => item.thread.title)).toEqual(["Updated title"]);
115115

116+
const clockThreads = threads.map((thread) =>
117+
thread.id === "thread-0" ? { ...thread, snoozedUntil: "2026-01-02T00:00:30.000Z" } : thread,
118+
);
119+
const clockInput = { ...input, threads: clockThreads, snoozedShelfExpanded: true };
120+
await act(async () => root.render(<Probe input={clockInput} tab="chats" />));
121+
const beforeClock = seen.at(-1)!;
122+
expect(beforeClock.layout.snoozedCount).toBe(1);
123+
expect(beforeClock.layout.items.find((item) => item.thread.id === "thread-0")?.snoozed).toBe(
124+
true,
125+
);
126+
await act(async () => root.render(<Probe input={clockInput} tab="settings" />));
127+
expect(seen.at(-1)!.items).toBe(beforeClock.items);
128+
116129
await act(async () =>
117-
root.render(<Probe input={{ ...input, now: "2026-01-02T00:01:00.000Z" }} tab="chats" />),
130+
root.render(<Probe input={{ ...clockInput, now: "2026-01-02T00:01:00.000Z" }} tab="chats" />),
131+
);
132+
const afterClock = seen.at(-1)!;
133+
expect(afterClock.items).not.toBe(beforeClock.items);
134+
expect(afterClock.layout.snoozedCount).toBe(0);
135+
expect(afterClock.layout.items.find((item) => item.thread.id === "thread-0")?.snoozed).toBe(
136+
false,
118137
);
119-
expect(seen.at(-1)!.items).not.toBe(initial.items);
120138
});
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { CommandId, EnvironmentId, MessageId, ProjectId, ThreadId } from "@t3tools/contracts";
2+
import { AtomRegistry } from "effect/reactivity";
3+
import { expect, it, vi } from "vite-plus/test";
4+
5+
import type { QueuedThreadMessage } from "./thread-outbox-model";
6+
7+
vi.mock("./thread-outbox", async () => {
8+
const { Atom } = await import("effect/reactivity");
9+
return {
10+
threadOutboxManager: {
11+
queuedMessagesByThreadKeyAtom: Atom.make<
12+
Readonly<Record<string, ReadonlyArray<QueuedThreadMessage>>>
13+
>({}).pipe(Atom.keepAlive),
14+
},
15+
};
16+
});
17+
vi.mock("./atom-registry", () => ({ appAtomRegistry: undefined }));
18+
vi.mock("./shell", () => ({ environmentShell: {} }));
19+
20+
import { threadOutboxManager } from "./thread-outbox";
21+
import { queuedThreadKeysAtom } from "./use-thread-outbox";
22+
23+
it("retains queue membership while chat lists are hidden and follows queued messages", () => {
24+
const scheduled = new Set<() => void>();
25+
const registry = AtomRegistry.make({
26+
scheduleTask: (task) => {
27+
scheduled.add(task);
28+
return () => {
29+
scheduled.delete(task);
30+
};
31+
},
32+
});
33+
using _registry = { [Symbol.dispose]: () => registry.dispose() };
34+
const flush = () => {
35+
for (const task of scheduled) {
36+
scheduled.delete(task);
37+
task();
38+
}
39+
};
40+
const release = registry.mount(queuedThreadKeysAtom);
41+
const initial = registry.get(queuedThreadKeysAtom);
42+
expect([...initial]).toEqual([]);
43+
release();
44+
flush();
45+
expect(registry.get(queuedThreadKeysAtom)).toBe(initial);
46+
47+
const message: QueuedThreadMessage = {
48+
environmentId: EnvironmentId.make("environment-1"),
49+
threadId: ThreadId.make("thread-1"),
50+
messageId: MessageId.make("message-1"),
51+
commandId: CommandId.make("command-1"),
52+
text: "Follow up",
53+
attachments: [],
54+
createdAt: "2026-10-29T12:00:00.000Z",
55+
};
56+
registry.set(threadOutboxManager.queuedMessagesByThreadKeyAtom, {
57+
"environment-1:thread-1": [message],
58+
"environment-1:new-thread": [
59+
{
60+
...message,
61+
threadId: ThreadId.make("new-thread"),
62+
creation: {
63+
projectId: ProjectId.make("project-1"),
64+
workspaceMode: "local",
65+
branch: null,
66+
worktreePath: null,
67+
},
68+
},
69+
],
70+
});
71+
const queued = registry.get(queuedThreadKeysAtom);
72+
expect([...queued]).toEqual(["environment-1:thread-1"]);
73+
const releaseAgain = registry.mount(queuedThreadKeysAtom);
74+
releaseAgain();
75+
flush();
76+
expect(registry.get(queuedThreadKeysAtom)).toBe(queued);
77+
78+
registry.set(threadOutboxManager.queuedMessagesByThreadKeyAtom, {});
79+
expect([...registry.get(queuedThreadKeysAtom)]).toEqual([]);
80+
});

‎apps/mobile/src/state/use-thread-outbox.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const queuedThreadKeysAtom = Atom.make((get): ReadonlySet<string> => {
6363
}
6464
}
6565
return keys;
66-
}).pipe(Atom.withLabel("mobile:thread-outbox:queued-thread-keys"));
66+
}).pipe(Atom.keepAlive, Atom.withLabel("mobile:thread-outbox:queued-thread-keys"));
6767

6868
export function useQueuedThreadKeys(): ReadonlySet<string> {
6969
return useAtomValue(queuedThreadKeysAtom);

0 commit comments

Comments
 (0)