Skip to content

Commit 19227b7

Browse files
fix(web): draft threads keep browser history and favicons
Draft lookups keyed the composer draft map by the thread key, but that map is keyed by draft id, which differs from the draft's thread id. A draft's "Recently used" list stayed empty, its visits waited in memory until the first send, and its favicons never stored. Both stores now resolve drafts with getDraftSessionByRef. History registration also waits for client settings to hydrate, so a visit made before then no longer persists a default-grouped project key. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 02c8e85 commit 19227b7

5 files changed

Lines changed: 93 additions & 21 deletions

File tree

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ vi.mock("~/state/session", () => ({
1717
metadata.httpBaseUrl ? { httpBaseUrl: metadata.httpBaseUrl } : null,
1818
}));
1919

20-
import { useComposerDraftStore, type DraftThreadState } from "./composerDraftStore";
20+
import { DraftId, useComposerDraftStore } from "./composerDraftStore";
2121
import {
2222
lookupFavicon,
2323
lookupFaviconForThread,
@@ -103,14 +103,11 @@ describe("browser favicon store", () => {
103103
});
104104

105105
it("files a draft thread's capture under the draft's project with no view registering it", () => {
106-
useComposerDraftStore.setState({
107-
draftThreadsByThreadKey: {
108-
[scopedThreadKey(threadRef)]: {
109-
environmentId,
110-
projectId: ProjectId.make("project-1"),
111-
} as DraftThreadState,
112-
},
113-
});
106+
useComposerDraftStore
107+
.getState()
108+
.setLogicalProjectDraftThreadId("env-1:project-1", projectRef, DraftId.make("draft-1"), {
109+
threadId: threadRef.threadId,
110+
});
114111
expect(recordFaviconForThread(threadRef, favicon("http://localhost:3000/", 1))).toBe(true);
115112
expect(useBrowserFaviconStore.getState().byKey).toEqual({
116113
"env-1:project-1 http://localhost:3000": { dataUrl: PNG, capturedAt: 1 },

‎apps/web/src/browserFaviconStore.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function mergeBrowserFaviconState(
175175
function useFaviconProjectRefForThread(threadRef: ScopedThreadRef): ScopedProjectRef | null {
176176
const shell = useQuietThreadShell(threadRef);
177177
const draftProjectRef = useComposerDraftStore((state) => {
178-
const draft = state.draftThreadsByThreadKey[scopedThreadKey(threadRef)];
178+
const draft = state.getDraftSessionByRef(threadRef);
179179
return draft ? scopedProjectKey(scopeProjectRef(draft.environmentId, draft.projectId)) : null;
180180
});
181181
if (shell?.projectId) return scopeProjectRef(threadRef.environmentId, shell.projectId);
@@ -185,8 +185,7 @@ function useFaviconProjectRefForThread(threadRef: ScopedThreadRef): ScopedProjec
185185
function readThreadProjectRef(threadRef: ScopedThreadRef): ScopedProjectRef | null {
186186
const projectId = readThreadShell(threadRef)?.projectId;
187187
if (projectId) return scopeProjectRef(threadRef.environmentId, projectId);
188-
const draft =
189-
useComposerDraftStore.getState().draftThreadsByThreadKey[scopedThreadKey(threadRef)];
188+
const draft = useComposerDraftStore.getState().getDraftSessionByRef(threadRef);
190189
return draft ? scopeProjectRef(draft.environmentId, draft.projectId) : null;
191190
}
192191

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
22

3+
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
34
import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
45

5-
const { readPreparedConnection, entities } = vi.hoisted(() => ({
6+
const { readPreparedConnection, entities, settingsHydration } = vi.hoisted(() => ({
67
readPreparedConnection: vi.fn<() => { httpBaseUrl: string } | null>(() => null),
78
entities: {
89
projects: [] as Array<Record<string, unknown>>,
910
shellProjectId: null as string | null,
1011
},
12+
settingsHydration: {
13+
hydrated: true,
14+
finish: () => {},
15+
promise: Promise.resolve(),
16+
},
1117
}));
1218

1319
vi.mock("~/state/session", () => ({ readPreparedConnection }));
@@ -26,13 +32,16 @@ vi.mock("./hooks/useSettings", async () => {
2632
return {
2733
getClientSettings: () => DEFAULT_CLIENT_SETTINGS,
2834
useClientSettings: () => DEFAULT_CLIENT_SETTINGS,
35+
isClientSettingsHydrated: () => settingsHydration.hydrated,
36+
ensureClientSettingsHydrated: () => settingsHydration.promise,
2937
};
3038
});
3139
vi.mock("~/state/environments", () => ({
3240
readPrimaryEnvironmentId: () => null,
3341
usePrimaryEnvironmentId: () => null,
3442
}));
3543

44+
import { DraftId, useComposerDraftStore } from "./composerDraftStore";
3645
import {
3746
BROWSER_HISTORY_MAX_PROJECTS,
3847
type BrowserHistoryEntry,
@@ -54,6 +63,9 @@ beforeEach(() => {
5463
readPreparedConnection.mockReturnValue(null);
5564
entities.projects = [];
5665
entities.shellProjectId = null;
66+
settingsHydration.hydrated = true;
67+
settingsHydration.promise = Promise.resolve();
68+
useComposerDraftStore.setState({ draftThreadsByThreadKey: {} });
5769
});
5870
afterEach(() => vi.restoreAllMocks());
5971

@@ -195,6 +207,14 @@ describe("useBrowserHistoryStore", () => {
195207
});
196208
});
197209

210+
const repoProject = {
211+
environmentId: EnvironmentId.make("env-1"),
212+
id: ProjectId.make("project-1"),
213+
workspaceRoot: "/repo",
214+
repositoryIdentity: null,
215+
isScratch: false,
216+
};
217+
198218
describe("thread project resolution", () => {
199219
beforeEach(() => {
200220
resetBrowserHistoryForTests();
@@ -222,6 +242,45 @@ describe("thread project resolution", () => {
222242
expect(useBrowserHistoryStore.getState().pendingTitlesByThreadKey).toEqual({});
223243
});
224244

245+
it("files a draft thread's visits under the draft's project", () => {
246+
entities.projects = [repoProject];
247+
const projectRef = scopeProjectRef(EnvironmentId.make("env-1"), ProjectId.make("project-1"));
248+
useComposerDraftStore
249+
.getState()
250+
.setLogicalProjectDraftThreadId("env-1:/repo", projectRef, DraftId.make("draft-1"), {
251+
threadId: threadRef.threadId,
252+
});
253+
254+
recordVisitForThread(threadRef, "http://localhost:5173/", 1);
255+
256+
expect(useBrowserHistoryStore.getState().byProjectKey).toEqual({
257+
"env-1:/repo": [{ url: "http://localhost:5173/", lastVisitedAt: 1 }],
258+
});
259+
expect(useBrowserHistoryStore.getState().pendingVisitsByThreadKey).toEqual({});
260+
});
261+
262+
it("waits for client settings before filing a thread under its project", async () => {
263+
entities.projects = [repoProject];
264+
entities.shellProjectId = "project-1";
265+
settingsHydration.hydrated = false;
266+
settingsHydration.promise = new Promise<void>((resolve) => {
267+
settingsHydration.finish = () => {
268+
settingsHydration.hydrated = true;
269+
resolve();
270+
};
271+
});
272+
273+
recordVisitForThread(threadRef, "http://localhost:5173/", 1);
274+
expect(useBrowserHistoryStore.getState().projectKeyByThreadKey).toEqual({});
275+
276+
settingsHydration.finish();
277+
await settingsHydration.promise;
278+
expect(useBrowserHistoryStore.getState().byProjectKey).toEqual({
279+
"env-1:/repo": [{ url: "http://localhost:5173/", lastVisitedAt: 1 }],
280+
});
281+
expect(useBrowserHistoryStore.getState().pendingVisitsByThreadKey).toEqual({});
282+
});
283+
225284
it("lands visits queued before the thread's project loaded with the next visit", () => {
226285
recordVisitForThread(threadRef, "http://a.test/", 1);
227286
entities.projects = [

‎apps/web/src/browserHistoryStore.ts‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ import { readPreparedConnection } from "~/state/session";
2121

2222
import { isLocalLoopbackHost, normalizeHostname } from "./browser/browserTargetResolver";
2323
import { useComposerDraftStore } from "./composerDraftStore";
24-
import { getClientSettings, useClientSettings } from "./hooks/useSettings";
24+
import {
25+
ensureClientSettingsHydrated,
26+
getClientSettings,
27+
isClientSettingsHydrated,
28+
useClientSettings,
29+
} from "./hooks/useSettings";
2530
import { resolveStorage } from "./lib/storage";
2631
import {
2732
deriveLogicalProjectKeyFromSettings,
@@ -366,20 +371,28 @@ function deriveHistoryProjectKey(
366371
function readThreadProjectRef(ref: ScopedThreadRef): ScopedProjectRef | null {
367372
const projectId = readThreadShell(ref)?.projectId;
368373
if (projectId) return scopeProjectRef(ref.environmentId, projectId);
369-
const draft = useComposerDraftStore.getState().draftThreadsByThreadKey[scopedThreadKey(ref)];
374+
const draft = useComposerDraftStore.getState().getDraftSessionByRef(ref);
370375
return draft ? scopeProjectRef(draft.environmentId, draft.projectId) : null;
371376
}
372377

373378
/**
374-
* Registers the thread under its current project so earlier visits and titles
375-
* land. A thread whose project is not loaded yet keeps its last registration,
376-
* or none, and its writes wait in the pending queues.
379+
* Registers the thread under its current project so queued visits and titles
380+
* land. Until the project loads, or client settings hydrate (the grouping is
381+
* unknown before then), the last registration stands and writes stay queued.
377382
*/
378383
function resolveThreadProjectKey(ref: ScopedThreadRef): string | undefined {
384+
const state = useBrowserHistoryStore.getState();
385+
const registered = state.projectKeyByThreadKey[scopedThreadKey(ref)];
386+
if (!isClientSettingsHydrated()) {
387+
void ensureClientSettingsHydrated().then(
388+
() => resolveThreadProjectKey(ref),
389+
() => undefined,
390+
);
391+
return registered;
392+
}
379393
const projectRef = readThreadProjectRef(ref);
380394
const project = projectRef ? readProject(projectRef) : null;
381-
const state = useBrowserHistoryStore.getState();
382-
if (!project) return state.projectKeyByThreadKey[scopedThreadKey(ref)];
395+
if (!project) return registered;
383396
const projectKey = deriveHistoryProjectKey(
384397
project,
385398
readProjects(),
@@ -444,7 +457,7 @@ function useThreadHistoryProjectKey(ref: ScopedThreadRef): string | null {
444457
const threadKey = scopedThreadKey(ref);
445458
const shellProjectId = useQuietThreadShell(ref)?.projectId ?? null;
446459
const draftProjectKey = useComposerDraftStore((state) => {
447-
const draft = state.draftThreadsByThreadKey[threadKey];
460+
const draft = state.getDraftSessionByRef(ref);
448461
return draft ? scopedProjectKey(scopeProjectRef(draft.environmentId, draft.projectId)) : null;
449462
});
450463
const projectKey = shellProjectId

‎apps/web/src/hooks/useSettings.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ export function getClientSettings(): ClientSettings {
265265
return getClientSettingsSnapshot();
266266
}
267267

268+
export function isClientSettingsHydrated(): boolean {
269+
return getClientSettingsHydratedSnapshot();
270+
}
271+
268272
/** Failed reads reject and remain retryable so defaults cannot overwrite saved preferences. */
269273
export function ensureClientSettingsHydrated(): Promise<void> {
270274
return hydrateClientSettings();

0 commit comments

Comments
 (0)