Skip to content

Commit 7cb1189

Browse files
fix(web): browser history and titles show up again
Nothing registered a thread's project with the browser history store after the V2 orchestrator merge (de34391) removed ChatView's sync effect, so every visit and title stayed pending and the Browser panel's "Recently used" list was always empty. Resolve the thread's project at the moment history is written (thread shell or composer draft, keyed through the sidebar's project grouping) and derive the same key in useThreadRecentHistory, mirroring how favicons resolve their project since f995643. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 28df7a2 commit 7cb1189

3 files changed

Lines changed: 183 additions & 11 deletions

File tree

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

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
22

3-
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3+
import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
44

5-
const { readPreparedConnection } = vi.hoisted(() => ({
5+
const { readPreparedConnection, entities } = vi.hoisted(() => ({
66
readPreparedConnection: vi.fn<() => { httpBaseUrl: string } | null>(() => null),
7+
entities: {
8+
projects: [] as Array<Record<string, unknown>>,
9+
shellProjectId: null as string | null,
10+
},
711
}));
812

913
vi.mock("~/state/session", () => ({ readPreparedConnection }));
14+
vi.mock("~/state/entities", () => ({
15+
readThreadShell: () => (entities.shellProjectId ? { projectId: entities.shellProjectId } : null),
16+
readProject: (ref: { environmentId: string; projectId: string }) =>
17+
entities.projects.find(
18+
(project) => project.environmentId === ref.environmentId && project.id === ref.projectId,
19+
) ?? null,
20+
readProjects: () => entities.projects,
21+
useProjects: () => entities.projects,
22+
useQuietThreadShell: () => null,
23+
}));
24+
vi.mock("./hooks/useSettings", async () => {
25+
const { DEFAULT_CLIENT_SETTINGS } = await import("@t3tools/contracts/settings");
26+
return {
27+
getClientSettings: () => DEFAULT_CLIENT_SETTINGS,
28+
useClientSettings: () => DEFAULT_CLIENT_SETTINGS,
29+
};
30+
});
31+
vi.mock("~/state/environments", () => ({
32+
readPrimaryEnvironmentId: () => null,
33+
usePrimaryEnvironmentId: () => null,
34+
}));
1035

1136
import {
1237
BROWSER_HISTORY_MAX_PROJECTS,
@@ -25,7 +50,11 @@ function entry(overrides: Partial<BrowserHistoryEntry> = {}): BrowserHistoryEntr
2550
return { url: "http://localhost:3000/", lastVisitedAt: 1000, ...overrides };
2651
}
2752

28-
beforeEach(() => readPreparedConnection.mockReturnValue(null));
53+
beforeEach(() => {
54+
readPreparedConnection.mockReturnValue(null);
55+
entities.projects = [];
56+
entities.shellProjectId = null;
57+
});
2958
afterEach(() => vi.restoreAllMocks());
3059

3160
describe("normalizeHistoryUrl", () => {
@@ -166,6 +195,53 @@ describe("useBrowserHistoryStore", () => {
166195
});
167196
});
168197

198+
describe("thread project resolution", () => {
199+
beforeEach(() => {
200+
resetBrowserHistoryForTests();
201+
});
202+
203+
it("files a browser tab's visits and titles under the thread's project with no view registering it", () => {
204+
entities.projects = [
205+
{
206+
environmentId: EnvironmentId.make("env-1"),
207+
id: ProjectId.make("project-1"),
208+
workspaceRoot: "/repo",
209+
repositoryIdentity: null,
210+
isScratch: false,
211+
},
212+
];
213+
entities.shellProjectId = "project-1";
214+
215+
recordVisitForThread(threadRef, "http://localhost:5173/", 1);
216+
setTitleForThreadUrl(threadRef, "http://localhost:5173/", "Dev App");
217+
218+
expect(useBrowserHistoryStore.getState().byProjectKey).toEqual({
219+
"env-1:/repo": [{ url: "http://localhost:5173/", lastVisitedAt: 1, title: "Dev App" }],
220+
});
221+
expect(useBrowserHistoryStore.getState().pendingVisitsByThreadKey).toEqual({});
222+
expect(useBrowserHistoryStore.getState().pendingTitlesByThreadKey).toEqual({});
223+
});
224+
225+
it("lands visits queued before the thread's project loaded with the next visit", () => {
226+
recordVisitForThread(threadRef, "http://a.test/", 1);
227+
entities.projects = [
228+
{
229+
environmentId: EnvironmentId.make("env-1"),
230+
id: ProjectId.make("project-1"),
231+
workspaceRoot: "/repo",
232+
repositoryIdentity: null,
233+
isScratch: false,
234+
},
235+
];
236+
entities.shellProjectId = "project-1";
237+
recordVisitForThread(threadRef, "http://b.test/", 2);
238+
239+
expect(
240+
useBrowserHistoryStore.getState().byProjectKey["env-1:/repo"]?.map((entry) => entry.url),
241+
).toEqual(["http://b.test/", "http://a.test/"]);
242+
});
243+
});
244+
169245
describe("pendingVisitsByThreadKey", () => {
170246
beforeEach(() => {
171247
resetBrowserHistoryForTests();

‎apps/web/src/browserHistoryStore.ts‎

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
1-
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2-
import type { ScopedThreadRef } from "@t3tools/contracts";
1+
import {
2+
scopedProjectKey,
3+
scopedThreadKey,
4+
scopeProjectRef,
5+
} from "@t3tools/client-runtime/environment";
6+
import type { EnvironmentId, ScopedProjectRef, ScopedThreadRef } from "@t3tools/contracts";
37
import { create } from "zustand";
48
import { createJSONStorage, persist } from "zustand/middleware";
59
import { useShallow } from "zustand/react/shallow";
610

711
import { normalizePreviewUrl } from "@t3tools/shared/preview";
12+
import {
13+
readProject,
14+
readProjects,
15+
readThreadShell,
16+
useProjects,
17+
useQuietThreadShell,
18+
} from "~/state/entities";
19+
import { readPrimaryEnvironmentId, usePrimaryEnvironmentId } from "~/state/environments";
820
import { readPreparedConnection } from "~/state/session";
921

1022
import { isLocalLoopbackHost, normalizeHostname } from "./browser/browserTargetResolver";
23+
import { useComposerDraftStore } from "./composerDraftStore";
24+
import { getClientSettings, useClientSettings } from "./hooks/useSettings";
1125
import { resolveStorage } from "./lib/storage";
26+
import {
27+
deriveLogicalProjectKeyFromSettings,
28+
derivePhysicalProjectKey,
29+
selectProjectGroupingSettings,
30+
type ProjectGroupingSettings,
31+
} from "./logicalProject";
32+
import { buildPhysicalToLogicalProjectKeyMap } from "./sidebarProjectGrouping";
33+
import type { Project } from "./types";
1234

1335
export type BrowserHistoryEntry = { url: string; lastVisitedAt: number; title?: string };
1436

@@ -323,10 +345,55 @@ function migratePersistedThreadProjectKeys(
323345
);
324346
}
325347

348+
/**
349+
* History follows the sidebar's project rows, so a duplicate row shares the
350+
* history of the group it is listed under rather than keying by its own path.
351+
*/
352+
function deriveHistoryProjectKey(
353+
project: Project,
354+
projects: ReadonlyArray<Project>,
355+
settings: ProjectGroupingSettings,
356+
primaryEnvironmentId: EnvironmentId | null,
357+
): string {
358+
return (
359+
buildPhysicalToLogicalProjectKeyMap({ projects, settings, primaryEnvironmentId }).get(
360+
derivePhysicalProjectKey(project),
361+
) ?? deriveLogicalProjectKeyFromSettings(project, settings)
362+
);
363+
}
364+
365+
/** Server threads name their project in the shell; drafts in the composer draft store. */
366+
function readThreadProjectRef(ref: ScopedThreadRef): ScopedProjectRef | null {
367+
const projectId = readThreadShell(ref)?.projectId;
368+
if (projectId) return scopeProjectRef(ref.environmentId, projectId);
369+
const draft = useComposerDraftStore.getState().draftThreadsByThreadKey[scopedThreadKey(ref)];
370+
return draft ? scopeProjectRef(draft.environmentId, draft.projectId) : null;
371+
}
372+
373+
/**
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.
377+
*/
378+
function resolveThreadProjectKey(ref: ScopedThreadRef): string | undefined {
379+
const projectRef = readThreadProjectRef(ref);
380+
const project = projectRef ? readProject(projectRef) : null;
381+
const state = useBrowserHistoryStore.getState();
382+
if (!project) return state.projectKeyByThreadKey[scopedThreadKey(ref)];
383+
const projectKey = deriveHistoryProjectKey(
384+
project,
385+
readProjects(),
386+
selectProjectGroupingSettings(getClientSettings()),
387+
readPrimaryEnvironmentId(),
388+
);
389+
state.registerThreadProject(ref, projectKey);
390+
return projectKey;
391+
}
392+
326393
export function recordVisitForThread(ref: ScopedThreadRef, url: string, at?: number): void {
327394
const threadKey = scopedThreadKey(ref);
395+
const projectKey = resolveThreadProjectKey(ref);
328396
const state = useBrowserHistoryStore.getState();
329-
const projectKey = state.projectKeyByThreadKey[threadKey];
330397
const visitAt = at ?? Date.now();
331398
const connection = readPreparedConnection(ref.environmentId);
332399
const environmentHostname = connection ? new URL(connection.httpBaseUrl).hostname : null;
@@ -350,8 +417,8 @@ export function setTitleForThreadUrl(
350417
environmentHostname?: string | null,
351418
): void {
352419
const threadKey = scopedThreadKey(ref);
420+
const projectKey = resolveThreadProjectKey(ref);
353421
const state = useBrowserHistoryStore.getState();
354-
const projectKey = state.projectKeyByThreadKey[threadKey];
355422
if (!projectKey) {
356423
useBrowserHistoryStore.setState({
357424
pendingTitlesByThreadKey: addPendingByThread(state.pendingTitlesByThreadKey, threadKey, {
@@ -366,21 +433,45 @@ export function setTitleForThreadUrl(
366433
}
367434

368435
export function removeUrlForThread(ref: ScopedThreadRef, url: string): void {
369-
const state = useBrowserHistoryStore.getState();
370-
const projectKey = state.projectKeyByThreadKey[scopedThreadKey(ref)];
436+
const projectKey = resolveThreadProjectKey(ref);
371437
if (!projectKey) return;
372-
state.removeUrl(projectKey, url);
438+
useBrowserHistoryStore.getState().removeUrl(projectKey, url);
373439
}
374440

375441
const EMPTY_HISTORY: ReadonlyArray<BrowserHistoryEntry> = [];
376442

443+
function useThreadHistoryProjectKey(ref: ScopedThreadRef): string | null {
444+
const threadKey = scopedThreadKey(ref);
445+
const shellProjectId = useQuietThreadShell(ref)?.projectId ?? null;
446+
const draftProjectKey = useComposerDraftStore((state) => {
447+
const draft = state.draftThreadsByThreadKey[threadKey];
448+
return draft ? scopedProjectKey(scopeProjectRef(draft.environmentId, draft.projectId)) : null;
449+
});
450+
const projectKey = shellProjectId
451+
? scopedProjectKey(scopeProjectRef(ref.environmentId, shellProjectId))
452+
: draftProjectKey;
453+
const projects = useProjects();
454+
const settings = useClientSettings(selectProjectGroupingSettings);
455+
const primaryEnvironmentId = usePrimaryEnvironmentId();
456+
const registered = useBrowserHistoryStore(
457+
(state) => state.projectKeyByThreadKey[threadKey] ?? null,
458+
);
459+
const project = projects.find(
460+
(candidate) =>
461+
scopedProjectKey(scopeProjectRef(candidate.environmentId, candidate.id)) === projectKey,
462+
);
463+
return project
464+
? deriveHistoryProjectKey(project, projects, settings, primaryEnvironmentId)
465+
: registered;
466+
}
467+
377468
export function useThreadRecentHistory(
378469
ref: ScopedThreadRef,
379470
limit: number,
380471
): ReadonlyArray<BrowserHistoryEntry> {
472+
const projectKey = useThreadHistoryProjectKey(ref);
381473
return useBrowserHistoryStore(
382474
useShallow((state) => {
383-
const projectKey = state.projectKeyByThreadKey[scopedThreadKey(ref)];
384475
const entries = projectKey ? state.byProjectKey[projectKey] : undefined;
385476
return entries && entries.length > 0 ? entries.slice(0, limit) : EMPTY_HISTORY;
386477
}),

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { EnvironmentId } from "@t3tools/contracts";
99
import * as Option from "effect/Option";
1010

1111
import { environmentCatalog } from "../connection/catalog";
12+
import { appAtomRegistry } from "../rpc/atomRegistry";
1213
import {
1314
environmentPresentations,
1415
environmentSummaries,
@@ -64,6 +65,10 @@ export function usePrimaryEnvironmentId(): EnvironmentId | null {
6465
return useAtomValue(primaryEnvironmentIdAtom);
6566
}
6667

68+
export function readPrimaryEnvironmentId(): EnvironmentId | null {
69+
return appAtomRegistry.get(primaryEnvironmentIdAtom);
70+
}
71+
6772
export function useEnvironment(
6873
environmentId: EnvironmentId | null,
6974
): EnvironmentPresentation | null {

0 commit comments

Comments
 (0)