Skip to content

Commit aa1203b

Browse files
fix(web): wait for primary discovery before clearing sidebar scope
1 parent 6c5876e commit aa1203b

2 files changed

Lines changed: 53 additions & 10 deletions

File tree

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

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection";
1+
import { BearerConnectionTarget, PrimaryConnectionTarget } from "@t3tools/client-runtime/connection";
22
import type { EnvironmentCatalogState } from "@t3tools/client-runtime/state/connections";
33
import type { EnvironmentShellState } from "@t3tools/client-runtime/state/shell";
44
import { EnvironmentId } from "@t3tools/contracts";
@@ -34,37 +34,67 @@ function catalogState(environmentIds: readonly EnvironmentId[]): EnvironmentCata
3434
environmentIds.map((environmentId) => [
3535
environmentId,
3636
{
37-
target: new PrimaryConnectionTarget({
38-
environmentId,
39-
label: environmentId,
40-
httpBaseUrl: `https://${environmentId}.example.test`,
41-
wsBaseUrl: `wss://${environmentId}.example.test`,
42-
}),
37+
target:
38+
environmentId === LOCAL
39+
? new PrimaryConnectionTarget({
40+
environmentId,
41+
label: environmentId,
42+
httpBaseUrl: `https://${environmentId}.example.test`,
43+
wsBaseUrl: `wss://${environmentId}.example.test`,
44+
})
45+
: new BearerConnectionTarget({
46+
environmentId,
47+
connectionId: environmentId,
48+
label: environmentId,
49+
}),
4350
profile: Option.none(),
4451
},
4552
]),
4653
),
4754
};
4855
}
4956

50-
function makeHarness() {
57+
function makeHarness(requiresPrimaryEnvironment = true) {
5158
const catalog = Atom.make<EnvironmentCatalogState>({ isReady: false, entries: new Map() });
5259
const shells = Atom.family((_environmentId: EnvironmentId) =>
5360
Atom.make<EnvironmentShellState>(shellState("empty")),
5461
);
5562
const ready = createAllEnvironmentProjectSnapshotsReadyAtom({
5663
catalogValueAtom: catalog,
5764
shellStateValueAtom: shells,
65+
requiresPrimaryEnvironment,
5866
});
5967
const registry = AtomRegistry.make();
6068
return { catalog, shells, ready, registry };
6169
}
6270

6371
describe("project snapshot readiness", () => {
64-
it("does not clear a saved scope before catalog loading finishes", () => {
72+
it("does not clear a saved scope while the ready catalog is still empty", () => {
6573
const { catalog, ready, registry } = makeHarness();
6674
expect(registry.get(ready)).toBe(false);
6775
registry.set(catalog, catalogState([]));
76+
expect(registry.get(ready)).toBe(false);
77+
registry.dispose();
78+
});
79+
80+
it("waits for primary discovery even if a persisted remote is already live", () => {
81+
const { catalog, shells, ready, registry } = makeHarness();
82+
registry.set(catalog, catalogState([REMOTE]));
83+
registry.set(shells(REMOTE), shellState("live"));
84+
expect(registry.get(ready)).toBe(false);
85+
86+
registry.set(catalog, catalogState([LOCAL, REMOTE]));
87+
expect(registry.get(ready)).toBe(false);
88+
registry.set(shells(LOCAL), shellState("live"));
89+
expect(registry.get(ready)).toBe(true);
90+
registry.dispose();
91+
});
92+
93+
it("allows a hosted client to load projects without a primary environment", () => {
94+
const { catalog, shells, ready, registry } = makeHarness(false);
95+
registry.set(catalog, catalogState([REMOTE]));
96+
expect(registry.get(ready)).toBe(false);
97+
registry.set(shells(REMOTE), shellState("live"));
6898
expect(registry.get(ready)).toBe(true);
6999
registry.dispose();
70100
});

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { AsyncResult, Atom } from "effect/unstable/reactivity";
1616

1717
import { environmentCatalog } from "../connection/catalog";
1818
import { connectionAtomRuntime } from "../connection/runtime";
19+
import { isHostedStaticApp } from "../hostedPairing";
1920

2021
export const shellEnvironment = createShellEnvironmentAtoms(connectionAtomRuntime);
2122
export const environmentShell = createEnvironmentShellAtoms(connectionAtomRuntime);
@@ -54,10 +55,21 @@ export const allEnvironmentShellsBootstrappedAtom = Atom.make((get) => {
5455
export function createAllEnvironmentProjectSnapshotsReadyAtom(input: {
5556
readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
5657
readonly shellStateValueAtom: (environmentId: EnvironmentId) => Atom.Atom<EnvironmentShellState>;
58+
readonly requiresPrimaryEnvironment: boolean;
5759
}) {
5860
return Atom.make((get) => {
5961
const catalog = get(input.catalogValueAtom);
60-
if (!catalog.isReady) return false;
62+
// The persisted catalog can emit before platform discovery registers the
63+
// primary environment. Neither that gap nor an empty catalog proves absence.
64+
if (!catalog.isReady || catalog.entries.size === 0) return false;
65+
if (
66+
input.requiresPrimaryEnvironment &&
67+
!Array.from(catalog.entries.values()).some(
68+
(entry) => entry.target._tag === "PrimaryConnectionTarget",
69+
)
70+
) {
71+
return false;
72+
}
6173
for (const environmentId of catalog.entries.keys()) {
6274
const shell = get(input.shellStateValueAtom(environmentId));
6375
if (shell.status !== "live" || Option.isNone(shell.snapshot)) return false;
@@ -70,4 +82,5 @@ export const allEnvironmentProjectSnapshotsReadyAtom =
7082
createAllEnvironmentProjectSnapshotsReadyAtom({
7183
catalogValueAtom: environmentCatalog.catalogValueAtom,
7284
shellStateValueAtom: environmentShell.stateValueAtom,
85+
requiresPrimaryEnvironment: !isHostedStaticApp(),
7386
});

0 commit comments

Comments
 (0)