Skip to content

Commit 6c5876e

Browse files
fix(web): retain project scope until live snapshots arrive
1 parent dd27c81 commit 6c5876e

4 files changed

Lines changed: 141 additions & 8 deletions

File tree

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ import { useLocalStorage } from "../hooks/useLocalStorage";
112112
import { useNowMinute } from "../hooks/useNowMinute";
113113
import { useEnvironments, usePrimaryEnvironmentId } from "../state/environments";
114114
import {
115-
useAllEnvironmentShellsBootstrapped,
115+
useAllEnvironmentProjectSnapshotsReady,
116116
useProjects,
117117
useThreadShells,
118118
} from "../state/entities";
@@ -2171,15 +2171,14 @@ export default function Sidebar() {
21712171
[scopedProjectGroup],
21722172
);
21732173
// A persisted scope whose project is gone falls back to all projects, but
2174-
// only after every catalog environment has delivered its project snapshot:
2175-
// environments populate independently, so a partial list must not clear a
2176-
// scope whose project lives in an environment that is still loading.
2177-
const allShellsBootstrapped = useAllEnvironmentShellsBootstrapped();
2174+
// only after every catalog environment has a live project snapshot. Cached
2175+
// or disconnected environments cannot establish that the project is gone.
2176+
const allProjectSnapshotsReady = useAllEnvironmentProjectSnapshotsReady();
21782177
useEffect(() => {
2179-
if (projectScopeKey !== null && allShellsBootstrapped && scopedProjectGroup === null) {
2178+
if (projectScopeKey !== null && allProjectSnapshotsReady && scopedProjectGroup === null) {
21802179
setProjectScopeKey(null);
21812180
}
2182-
}, [allShellsBootstrapped, projectScopeKey, scopedProjectGroup, setProjectScopeKey]);
2181+
}, [allProjectSnapshotsReady, projectScopeKey, scopedProjectGroup, setProjectScopeKey]);
21832182
// Count-only subscription: the parent needs "are there draft rows" for the
21842183
// empty state, while SidebarDraftBlock owns the per-keystroke content
21852184
// subscription. Selecting a number keeps typing in a draft composer from

‎apps/web/src/state/entities.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import { useMemo } from "react";
1515
import { appAtomRegistry } from "../rpc/atomRegistry";
1616
import { environmentProjects } from "./projects";
1717
import { environmentServerConfigsAtom } from "./server";
18-
import { allEnvironmentShellsBootstrappedAtom } from "./shell";
18+
import {
19+
allEnvironmentProjectSnapshotsReadyAtom,
20+
allEnvironmentShellsBootstrappedAtom,
21+
} from "./shell";
1922
import { environmentThreadDetails, environmentThreadShells } from "./threads";
2023

2124
const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
@@ -79,6 +82,10 @@ export function useAllEnvironmentShellsBootstrapped(): boolean {
7982
return useAtomValue(allEnvironmentShellsBootstrappedAtom);
8083
}
8184

85+
export function useAllEnvironmentProjectSnapshotsReady(): boolean {
86+
return useAtomValue(allEnvironmentProjectSnapshotsReadyAtom);
87+
}
88+
8289
export function useThreadShellsForProjectRefs(
8390
refs: ReadonlyArray<ScopedProjectRef>,
8491
): ReadonlyArray<EnvironmentThreadShell> {

‎apps/web/src/state/shell.test.ts‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection";
2+
import type { EnvironmentCatalogState } from "@t3tools/client-runtime/state/connections";
3+
import type { EnvironmentShellState } from "@t3tools/client-runtime/state/shell";
4+
import { EnvironmentId } from "@t3tools/contracts";
5+
import * as Option from "effect/Option";
6+
import { Atom, AtomRegistry } from "effect/unstable/reactivity";
7+
import { describe, expect, it } from "vite-plus/test";
8+
9+
import { createAllEnvironmentProjectSnapshotsReadyAtom } from "./shell";
10+
11+
const LOCAL = EnvironmentId.make("local");
12+
const REMOTE = EnvironmentId.make("remote");
13+
14+
function shellState(status: EnvironmentShellState["status"]): EnvironmentShellState {
15+
return {
16+
status,
17+
snapshot:
18+
status === "empty"
19+
? Option.none()
20+
: Option.some({
21+
snapshotSequence: 1,
22+
updatedAt: "2026-09-04T00:00:00.000Z",
23+
projects: [],
24+
threads: [],
25+
}),
26+
error: Option.none(),
27+
};
28+
}
29+
30+
function catalogState(environmentIds: readonly EnvironmentId[]): EnvironmentCatalogState {
31+
return {
32+
isReady: true,
33+
entries: new Map(
34+
environmentIds.map((environmentId) => [
35+
environmentId,
36+
{
37+
target: new PrimaryConnectionTarget({
38+
environmentId,
39+
label: environmentId,
40+
httpBaseUrl: `https://${environmentId}.example.test`,
41+
wsBaseUrl: `wss://${environmentId}.example.test`,
42+
}),
43+
profile: Option.none(),
44+
},
45+
]),
46+
),
47+
};
48+
}
49+
50+
function makeHarness() {
51+
const catalog = Atom.make<EnvironmentCatalogState>({ isReady: false, entries: new Map() });
52+
const shells = Atom.family((_environmentId: EnvironmentId) =>
53+
Atom.make<EnvironmentShellState>(shellState("empty")),
54+
);
55+
const ready = createAllEnvironmentProjectSnapshotsReadyAtom({
56+
catalogValueAtom: catalog,
57+
shellStateValueAtom: shells,
58+
});
59+
const registry = AtomRegistry.make();
60+
return { catalog, shells, ready, registry };
61+
}
62+
63+
describe("project snapshot readiness", () => {
64+
it("does not clear a saved scope before catalog loading finishes", () => {
65+
const { catalog, ready, registry } = makeHarness();
66+
expect(registry.get(ready)).toBe(false);
67+
registry.set(catalog, catalogState([]));
68+
expect(registry.get(ready)).toBe(true);
69+
registry.dispose();
70+
});
71+
72+
it("waits for live snapshots through offline startup and reconnect", () => {
73+
const { catalog, shells, ready, registry } = makeHarness();
74+
registry.set(catalog, catalogState([LOCAL, REMOTE]));
75+
registry.set(shells(LOCAL), shellState("live"));
76+
expect(registry.get(ready)).toBe(false);
77+
78+
// An old cache and a reconnect in progress can both omit a real project.
79+
registry.set(shells(REMOTE), shellState("cached"));
80+
expect(registry.get(ready)).toBe(false);
81+
registry.set(shells(REMOTE), shellState("synchronizing"));
82+
expect(registry.get(ready)).toBe(false);
83+
registry.set(shells(REMOTE), shellState("live"));
84+
expect(registry.get(ready)).toBe(true);
85+
86+
registry.set(shells(REMOTE), shellState("cached"));
87+
expect(registry.get(ready)).toBe(false);
88+
registry.set(shells(REMOTE), shellState("live"));
89+
expect(registry.get(ready)).toBe(true);
90+
registry.dispose();
91+
});
92+
93+
it("stops waiting for an environment only when it leaves the catalog", () => {
94+
const { catalog, shells, ready, registry } = makeHarness();
95+
registry.set(catalog, catalogState([LOCAL, REMOTE]));
96+
registry.set(shells(LOCAL), shellState("live"));
97+
expect(registry.get(ready)).toBe(false);
98+
registry.set(catalog, catalogState([LOCAL]));
99+
expect(registry.get(ready)).toBe(true);
100+
registry.dispose();
101+
});
102+
});

‎apps/web/src/state/shell.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
createEnvironmentShellSummaryAtom,
88
createEnvironmentSnapshotAtom,
99
createShellEnvironmentAtoms,
10+
type EnvironmentShellState,
1011
} from "@t3tools/client-runtime/state/shell";
12+
import type { EnvironmentCatalogState } from "@t3tools/client-runtime/state/connections";
13+
import type { EnvironmentId } from "@t3tools/contracts";
1114
import * as Option from "effect/Option";
1215
import { AsyncResult, Atom } from "effect/unstable/reactivity";
1316

@@ -46,3 +49,25 @@ export const allEnvironmentShellsBootstrappedAtom = Atom.make((get) => {
4649
}
4750
return true;
4851
}).pipe(Atom.withLabel("web-all-environment-shells-bootstrapped"));
52+
53+
/** Cached or missing snapshots cannot establish that a saved project no longer exists. */
54+
export function createAllEnvironmentProjectSnapshotsReadyAtom(input: {
55+
readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
56+
readonly shellStateValueAtom: (environmentId: EnvironmentId) => Atom.Atom<EnvironmentShellState>;
57+
}) {
58+
return Atom.make((get) => {
59+
const catalog = get(input.catalogValueAtom);
60+
if (!catalog.isReady) return false;
61+
for (const environmentId of catalog.entries.keys()) {
62+
const shell = get(input.shellStateValueAtom(environmentId));
63+
if (shell.status !== "live" || Option.isNone(shell.snapshot)) return false;
64+
}
65+
return true;
66+
}).pipe(Atom.withLabel("web-all-environment-project-snapshots-ready"));
67+
}
68+
69+
export const allEnvironmentProjectSnapshotsReadyAtom =
70+
createAllEnvironmentProjectSnapshotsReadyAtom({
71+
catalogValueAtom: environmentCatalog.catalogValueAtom,
72+
shellStateValueAtom: environmentShell.stateValueAtom,
73+
});

0 commit comments

Comments
 (0)