Skip to content

Commit 55dd5c9

Browse files
fix(web): visits queued before a thread loads land once it does
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7b346d3 commit 55dd5c9

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎apps/web/src/browserHistoryStore.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { readPreparedConnection, entities, settingsHydration } = vi.hoisted(() =>
88
entities: {
99
projects: [] as Array<Record<string, unknown>>,
1010
shellProjectId: null as string | null,
11+
shellArrived: new Promise<boolean>(() => {}),
1112
},
1213
settingsHydration: {
1314
hydrated: true,
@@ -26,6 +27,8 @@ vi.mock("~/state/entities", () => ({
2627
readProjects: () => entities.projects,
2728
useProjects: () => entities.projects,
2829
useQuietThreadShell: () => null,
30+
waitForThreadShell: () => entities.shellArrived,
31+
waitForProject: () => new Promise(() => {}),
2932
}));
3033
vi.mock("./hooks/useSettings", async () => {
3134
const { DEFAULT_CLIENT_SETTINGS } = await import("@t3tools/contracts/settings");
@@ -63,6 +66,7 @@ beforeEach(() => {
6366
readPreparedConnection.mockReturnValue(null);
6467
entities.projects = [];
6568
entities.shellProjectId = null;
69+
entities.shellArrived = new Promise<boolean>(() => {});
6670
settingsHydration.hydrated = true;
6771
settingsHydration.promise = Promise.resolve();
6872
useComposerDraftStore.setState({ draftThreadsByThreadKey: {} });
@@ -299,6 +303,25 @@ describe("thread project resolution", () => {
299303
useBrowserHistoryStore.getState().byProjectKey["env-1:/repo"]?.map((entry) => entry.url),
300304
).toEqual(["http://b.test/", "http://a.test/"]);
301305
});
306+
307+
it("lands a visit queued before the thread loaded once the thread arrives", async () => {
308+
let shellArrives = (_found: boolean) => {};
309+
entities.shellArrived = new Promise<boolean>((resolve) => {
310+
shellArrives = resolve;
311+
});
312+
recordVisitForThread(threadRef, "http://a.test/", 1);
313+
expect(useBrowserHistoryStore.getState().byProjectKey).toEqual({});
314+
315+
entities.projects = [repoProject];
316+
entities.shellProjectId = "project-1";
317+
shellArrives(true);
318+
await entities.shellArrived;
319+
320+
expect(useBrowserHistoryStore.getState().byProjectKey).toEqual({
321+
"env-1:/repo": [{ url: "http://a.test/", lastVisitedAt: 1 }],
322+
});
323+
expect(useBrowserHistoryStore.getState().pendingVisitsByThreadKey).toEqual({});
324+
});
302325
});
303326

304327
describe("pendingVisitsByThreadKey", () => {

‎apps/web/src/browserHistoryStore.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
readThreadShell,
1616
useProjects,
1717
useQuietThreadShell,
18+
waitForProject,
19+
waitForThreadShell,
1820
} from "~/state/entities";
1921
import { readPrimaryEnvironmentId, usePrimaryEnvironmentId } from "~/state/environments";
2022
import { readPreparedConnection } from "~/state/session";
@@ -378,7 +380,8 @@ function readThreadProjectRef(ref: ScopedThreadRef): ScopedProjectRef | null {
378380
/**
379381
* Registers the thread under its current project so queued visits and titles
380382
* land. Until the project loads, or client settings hydrate (the grouping is
381-
* unknown before then), the last registration stands and writes stay queued.
383+
* unknown before then), the last registration stands and writes stay queued
384+
* until whichever is missing arrives.
382385
*/
383386
function resolveThreadProjectKey(ref: ScopedThreadRef): string | undefined {
384387
const state = useBrowserHistoryStore.getState();
@@ -392,7 +395,16 @@ function resolveThreadProjectKey(ref: ScopedThreadRef): string | undefined {
392395
}
393396
const projectRef = readThreadProjectRef(ref);
394397
const project = projectRef ? readProject(projectRef) : null;
395-
if (!project) return registered;
398+
if (!project) {
399+
if (registered === undefined) {
400+
const arrived = projectRef ? waitForProject(projectRef) : waitForThreadShell(ref);
401+
void arrived.then(
402+
(found) => (found ? resolveThreadProjectKey(ref) : undefined),
403+
() => undefined,
404+
);
405+
}
406+
return registered;
407+
}
396408
const projectKey = deriveHistoryProjectKey(
397409
project,
398410
readProjects(),

0 commit comments

Comments
 (0)