Skip to content

Commit 8be193a

Browse files
perf(mobile): pending draft rows read a per-project has-threads flag
useNewTaskProjectDefaults subscribed to every thread shell to decide whether the project has threads, so each home pending-draft row re-rendered on every shell change while agents worked. A per-project Atom.family over the environment's thread refs by project and its older settled counts now answers that as a boolean; its readers are notified only when it flips. The new-task flow reads the same atom through the hook. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 515081f commit 8be193a

3 files changed

Lines changed: 126 additions & 11 deletions

File tree

‎apps/mobile/src/features/threads/use-new-task-project-defaults.ts‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { DEFAULT_SERVER_SETTINGS, T3_PROJECT_FILE_NAME } from "@t3tools/contract
66
import { resolveNewThreadEnvMode, resolveProjectSettings } from "@t3tools/shared/projectSettings";
77
import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile";
88

9-
import { useEnvironmentServerConfig, useThreadShells } from "../../state/entities";
10-
import { useOlderSettledCount } from "../../state/olderSettledShelf";
9+
import { useEnvironmentServerConfig } from "../../state/entities";
1110
import { useEnvironmentPresentation } from "../../state/presentation";
11+
import { useProjectHasThreads } from "../../state/project-has-threads";
1212
import { projectEnvironment } from "../../state/projects";
1313
import { useEnvironmentQuery } from "../../state/query";
1414
import { environmentSession } from "../../state/session";
@@ -21,7 +21,6 @@ import { environmentSession } from "../../state/session";
2121
*/
2222
export function useNewTaskProjectDefaults(project: EnvironmentProject | null) {
2323
const serverConfig = useEnvironmentServerConfig(project?.environmentId ?? null);
24-
const threads = useThreadShells();
2524
const isScratch =
2625
project !== null && isScratchProject(project, serverConfig?.scratchWorkspaceRoot);
2726
const hasWorkspace = project !== null && project.workspaceRoot !== "";
@@ -59,14 +58,7 @@ export function useNewTaskProjectDefaults(project: EnvironmentProject | null) {
5958
project,
6059
t3ProjectFile,
6160
);
62-
const olderSettledCount = useOlderSettledCount(project);
63-
const projectHasThreads =
64-
project !== null &&
65-
(olderSettledCount > 0 ||
66-
threads.some(
67-
(thread) =>
68-
thread.environmentId === project.environmentId && thread.projectId === project.id,
69-
));
61+
const projectHasThreads = useProjectHasThreads(project);
7062
const defaultWorkspaceMode = isScratch
7163
? "local"
7264
: resolveNewThreadEnvMode({
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { EnvironmentId, ProjectId } from "@t3tools/contracts";
2+
import { Atom, AtomRegistry } from "effect/reactivity";
3+
import { describe, expect, it, vi } from "vite-plus/test";
4+
5+
// The live sources pull in the connection runtime; the factory gets its own.
6+
vi.mock("./threads", () => ({
7+
environmentThreadShells: { environmentThreadRefsByProjectAtom: () => null },
8+
olderSettledThreads: { olderSettledAtom: () => null },
9+
}));
10+
11+
import { makeProjectHasThreadsAtom } from "./project-has-threads";
12+
13+
const environmentId = EnvironmentId.make("env-1");
14+
const mine = ProjectId.make("project-mine");
15+
const other = ProjectId.make("project-other");
16+
17+
function setup() {
18+
const refs = Atom.make<ReadonlyMap<ProjectId, ReadonlyArray<string>>>(new Map());
19+
const older = Atom.make<{ readonly counts: ReadonlyMap<ProjectId, number> } | null>(null);
20+
const family = makeProjectHasThreadsAtom({
21+
threadRefsByProject: () => refs,
22+
olderSettled: () => older,
23+
});
24+
const registry = AtomRegistry.make();
25+
const onChange = vi.fn();
26+
const atom = family(environmentId)(mine);
27+
// Immediate, as useAtomValue reads the value when it subscribes.
28+
const unsubscribe = registry.subscribe(atom, onChange, { immediate: true });
29+
onChange.mockClear();
30+
return { registry, refs, older, atom, onChange, unsubscribe };
31+
}
32+
33+
describe("project has threads", () => {
34+
it("notifies its reader only when its own project's answer flips", () => {
35+
const { registry, refs, atom, onChange, unsubscribe } = setup();
36+
expect(registry.get(atom)).toBe(false);
37+
38+
// Another project's threads come and go (streaming shells, new threads).
39+
const broad = vi.fn();
40+
const unsubscribeBroad = registry.subscribe(refs, broad);
41+
registry.set(refs, new Map([[other, ["a"]]]));
42+
registry.set(refs, new Map([[other, ["a", "b"]]]));
43+
// A thread-list reader sees both changes; this project's reader sees none.
44+
expect(broad).toHaveBeenCalledTimes(2);
45+
expect(onChange).not.toHaveBeenCalled();
46+
unsubscribeBroad();
47+
48+
registry.set(
49+
refs,
50+
new Map([
51+
[other, ["a", "b"]],
52+
[mine, ["c"]],
53+
]),
54+
);
55+
expect(registry.get(atom)).toBe(true);
56+
expect(onChange).toHaveBeenCalledTimes(1);
57+
58+
registry.set(
59+
refs,
60+
new Map([
61+
[other, ["a"]],
62+
[mine, ["c", "d"]],
63+
]),
64+
);
65+
expect(onChange).toHaveBeenCalledTimes(1);
66+
unsubscribe();
67+
});
68+
69+
it("counts older settled threads the client does not hold", () => {
70+
const { registry, older, atom, unsubscribe } = setup();
71+
registry.set(older, { counts: new Map([[mine, 2]]) });
72+
expect(registry.get(atom)).toBe(true);
73+
unsubscribe();
74+
});
75+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { useAtomValue } from "@effect/atom-react";
2+
import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
3+
import { Atom } from "effect/reactivity";
4+
5+
import { environmentThreadShells, olderSettledThreads } from "./threads";
6+
7+
/**
8+
* Whether a project has any thread, loaded or only counted as older settled.
9+
* A per-project boolean, so readers re-render when it flips, not on every
10+
* thread-shell change (which is frequent while agents work).
11+
*/
12+
export function makeProjectHasThreadsAtom(sources: {
13+
readonly threadRefsByProject: (
14+
environmentId: EnvironmentId,
15+
) => Atom.Atom<ReadonlyMap<ProjectId, ReadonlyArray<unknown>>>;
16+
readonly olderSettled: (
17+
environmentId: EnvironmentId,
18+
) => Atom.Atom<{ readonly counts: ReadonlyMap<ProjectId, number> } | null>;
19+
}) {
20+
return Atom.family((environmentId: EnvironmentId) =>
21+
Atom.family((projectId: ProjectId) =>
22+
Atom.make(
23+
(get) =>
24+
(get(sources.olderSettled(environmentId))?.counts.get(projectId) ?? 0) > 0 ||
25+
(get(sources.threadRefsByProject(environmentId)).get(projectId)?.length ?? 0) > 0,
26+
).pipe(Atom.withLabel(`mobile:project-has-threads:${environmentId}:${projectId}`)),
27+
),
28+
);
29+
}
30+
31+
const projectHasThreadsAtom = makeProjectHasThreadsAtom({
32+
threadRefsByProject: environmentThreadShells.environmentThreadRefsByProjectAtom,
33+
olderSettled: olderSettledThreads.olderSettledAtom,
34+
});
35+
36+
const NO_PROJECT_HAS_THREADS = Atom.make(false).pipe(
37+
Atom.withLabel("mobile:project-has-threads:none"),
38+
);
39+
40+
export function useProjectHasThreads(
41+
project: { readonly environmentId: EnvironmentId; readonly id: ProjectId } | null,
42+
): boolean {
43+
return useAtomValue(
44+
project === null
45+
? NO_PROJECT_HAS_THREADS
46+
: projectHasThreadsAtom(project.environmentId)(project.id),
47+
);
48+
}

0 commit comments

Comments
 (0)