@devries-cameron20 / t3code Mirror · read-only / commits / 7c6e85cfda
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 15:45 UTC

fix(web): browser history and titles show up again (#25491)

Julius Marminge committed Oct 26, 2026 at 07:50 UTC 7c6e85cfdaadca358125b2c6ae909eaed3dbe54b
6 files changed +257 -22
apps/web/src/browserFaviconStore.test.ts
+6 -9
@@ -17,7 +17,7 @@ vi.mock("~/state/session", () => ({
17 metadata.httpBaseUrl ? { httpBaseUrl: metadata.httpBaseUrl } : null,
18 }));
19
20 -import { useComposerDraftStore, type DraftThreadState } from "./composerDraftStore";
20 +import { DraftId, useComposerDraftStore } from "./composerDraftStore";
21 import {
22 lookupFavicon,
23 lookupFaviconForThread,
@@ -103,14 +103,11 @@ describe("browser favicon store", () => {
103 });
104
105 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 + });
111 expect(recordFaviconForThread(threadRef, favicon("http://localhost:3000/", 1))).toBe(true);
112 expect(useBrowserFaviconStore.getState().byKey).toEqual({
113 "env-1:project-1 http://localhost:3000": { dataUrl: PNG, capturedAt: 1 },
apps/web/src/browserFaviconStore.ts
+2 -3
@@ -175,7 +175,7 @@ function mergeBrowserFaviconState(
175 function useFaviconProjectRefForThread(threadRef: ScopedThreadRef): ScopedProjectRef | null {
176 const shell = useQuietThreadShell(threadRef);
177 const draftProjectRef = useComposerDraftStore((state) => {
178 - const draft = state.draftThreadsByThreadKey[scopedThreadKey(threadRef)];
178 + const draft = state.getDraftSessionByRef(threadRef);
179 return draft ? scopedProjectKey(scopeProjectRef(draft.environmentId, draft.projectId)) : null;
180 });
181 if (shell?.projectId) return scopeProjectRef(threadRef.environmentId, shell.projectId);
@@ -185,8 +185,7 @@ function useFaviconProjectRefForThread(threadRef: ScopedThreadRef): ScopedProjec
185 function readThreadProjectRef(threadRef: ScopedThreadRef): ScopedProjectRef | null {
186 const projectId = readThreadShell(threadRef)?.projectId;
187 if (projectId) return scopeProjectRef(threadRef.environmentId, projectId);
188 - const draft =
189 - useComposerDraftStore.getState().draftThreadsByThreadKey[scopedThreadKey(threadRef)];
188 + const draft = useComposerDraftStore.getState().getDraftSessionByRef(threadRef);
189 return draft ? scopeProjectRef(draft.environmentId, draft.projectId) : null;
190 }
191
apps/web/src/browserHistoryStore.test.ts
+124 -3
@@ -1,13 +1,52 @@
1 import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
3 -import { EnvironmentId, type PreviewSessionSnapshot, ThreadId } from "@t3tools/contracts";
3 +import { scopeProjectRef } from "@t3tools/client-runtime/environment";
4 +import {
5 + EnvironmentId,
6 + type PreviewSessionSnapshot,
7 + ProjectId,
8 + ThreadId,
9 +} from "@t3tools/contracts";
10
5 -const { readPreparedConnection } = vi.hoisted(() => ({
11 +const { readPreparedConnection, entities, settingsHydration } = vi.hoisted(() => ({
12 readPreparedConnection: vi.fn<() => { httpBaseUrl: string } | null>(() => null),
13 + entities: {
14 + projects: [] as Array<Record<string, unknown>>,
15 + shellProjectId: null as string | null,
16 + },
17 + settingsHydration: {
18 + hydrated: true,
19 + finish: () => {},
20 + promise: Promise.resolve(),
21 + },
22 }));
23
24 vi.mock("~/state/session", () => ({ readPreparedConnection }));
25 +vi.mock("~/state/entities", () => ({
26 + readThreadShell: () => (entities.shellProjectId ? { projectId: entities.shellProjectId } : null),
27 + readProject: (ref: { environmentId: string; projectId: string }) =>
28 + entities.projects.find(
29 + (project) => project.environmentId === ref.environmentId && project.id === ref.projectId,
30 + ) ?? null,
31 + readProjects: () => entities.projects,
32 + useProjects: () => entities.projects,
33 + useQuietThreadShell: () => null,
34 +}));
35 +vi.mock("./hooks/useSettings", async () => {
36 + const { DEFAULT_CLIENT_SETTINGS } = await import("@t3tools/contracts/settings");
37 + return {
38 + getClientSettings: () => DEFAULT_CLIENT_SETTINGS,
39 + useClientSettings: () => DEFAULT_CLIENT_SETTINGS,
40 + isClientSettingsHydrated: () => settingsHydration.hydrated,
41 + ensureClientSettingsHydrated: () => settingsHydration.promise,
42 + };
43 +});
44 +vi.mock("~/state/environments", () => ({
45 + readPrimaryEnvironmentId: () => null,
46 + usePrimaryEnvironmentId: () => null,
47 +}));
48
49 +import { DraftId, useComposerDraftStore } from "./composerDraftStore";
50 import {
51 BROWSER_HISTORY_MAX_PROJECTS,
52 type BrowserHistoryEntry,
@@ -26,7 +65,14 @@ function entry(overrides: Partial<BrowserHistoryEntry> = {}): BrowserHistoryEntr
65 return { url: "http://localhost:3000/", lastVisitedAt: 1000, ...overrides };
66 }
67
29 -beforeEach(() => readPreparedConnection.mockReturnValue(null));
68 +beforeEach(() => {
69 + readPreparedConnection.mockReturnValue(null);
70 + entities.projects = [];
71 + entities.shellProjectId = null;
72 + settingsHydration.hydrated = true;
73 + settingsHydration.promise = Promise.resolve();
74 + useComposerDraftStore.setState({ draftThreadsByThreadKey: {} });
75 +});
76 afterEach(() => vi.restoreAllMocks());
77
78 describe("normalizeHistoryUrl", () => {
@@ -251,6 +297,81 @@ describe("useBrowserHistoryStore", () => {
297 });
298 });
299
300 +const repoProject = {
301 + environmentId: EnvironmentId.make("env-1"),
302 + id: ProjectId.make("project-1"),
303 + workspaceRoot: "/repo",
304 + repositoryIdentity: null,
305 + isScratch: false,
306 +};
307 +
308 +describe("thread project resolution", () => {
309 + beforeEach(resetStores);
310 +
311 + it("files a browser tab's visits and titles under the thread's project with no view registering it", () => {
312 + entities.projects = [repoProject];
313 + entities.shellProjectId = "project-1";
314 +
315 + recordVisitForThread(threadRef, "http://localhost:5173/", 1);
316 + settle("http://localhost:5173/", "Dev App");
317 +
318 + expect(useBrowserHistoryStore.getState().byProjectKey).toEqual({
319 + "env-1:/repo": [{ url: "http://localhost:5173/", lastVisitedAt: 1, title: "Dev App" }],
320 + });
321 + expect(useBrowserHistoryStore.getState().pendingVisitsByThreadKey).toEqual({});
322 + });
323 +
324 + it("files a draft thread's visits under the draft's project", () => {
325 + entities.projects = [repoProject];
326 + const projectRef = scopeProjectRef(EnvironmentId.make("env-1"), ProjectId.make("project-1"));
327 + useComposerDraftStore
328 + .getState()
329 + .setLogicalProjectDraftThreadId("env-1:/repo", projectRef, DraftId.make("draft-1"), {
330 + threadId: threadRef.threadId,
331 + });
332 +
333 + recordVisitForThread(threadRef, "http://localhost:5173/", 1);
334 +
335 + expect(useBrowserHistoryStore.getState().byProjectKey).toEqual({
336 + "env-1:/repo": [{ url: "http://localhost:5173/", lastVisitedAt: 1 }],
337 + });
338 + expect(useBrowserHistoryStore.getState().pendingVisitsByThreadKey).toEqual({});
339 + });
340 +
341 + it("waits for client settings before filing a thread under its project", async () => {
342 + entities.projects = [repoProject];
343 + entities.shellProjectId = "project-1";
344 + settingsHydration.hydrated = false;
345 + settingsHydration.promise = new Promise<void>((resolve) => {
346 + settingsHydration.finish = () => {
347 + settingsHydration.hydrated = true;
348 + resolve();
349 + };
350 + });
351 +
352 + recordVisitForThread(threadRef, "http://localhost:5173/", 1);
353 + expect(useBrowserHistoryStore.getState().projectKeyByThreadKey).toEqual({});
354 +
355 + settingsHydration.finish();
356 + await settingsHydration.promise;
357 + expect(useBrowserHistoryStore.getState().byProjectKey).toEqual({
358 + "env-1:/repo": [{ url: "http://localhost:5173/", lastVisitedAt: 1 }],
359 + });
360 + expect(useBrowserHistoryStore.getState().pendingVisitsByThreadKey).toEqual({});
361 + });
362 +
363 + it("lands visits queued before the thread's project loaded with the next visit", () => {
364 + recordVisitForThread(threadRef, "http://a.test/", 1);
365 + entities.projects = [repoProject];
366 + entities.shellProjectId = "project-1";
367 + recordVisitForThread(threadRef, "http://b.test/", 2);
368 +
369 + expect(
370 + useBrowserHistoryStore.getState().byProjectKey["env-1:/repo"]?.map((entry) => entry.url),
371 + ).toEqual(["http://b.test/", "http://a.test/"]);
372 + });
373 +});
374 +
375 describe("pendingVisitsByThreadKey", () => {
376 beforeEach(resetStores);
377
apps/web/src/browserHistoryStore.ts
+116 -7
@@ -1,5 +1,14 @@
1 -import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2 -import type { PreviewSessionSnapshot, ScopedThreadRef } from "@t3tools/contracts";
1 +import {
2 + scopedProjectKey,
3 + scopedThreadKey,
4 + scopeProjectRef,
5 +} from "@t3tools/client-runtime/environment";
6 +import type {
7 + EnvironmentId,
8 + PreviewSessionSnapshot,
9 + ScopedProjectRef,
10 + ScopedThreadRef,
11 +} from "@t3tools/contracts";
12 import { create } from "zustand";
13 import { createJSONStorage, persist } from "zustand/middleware";
14 import { useShallow } from "zustand/react/shallow";
@@ -12,10 +21,33 @@ import {
21 type ThreadPreviewState,
22 } from "~/previewStateStore";
23 import { appAtomRegistry } from "~/rpc/atomRegistry";
24 +import {
25 + readProject,
26 + readProjects,
27 + readThreadShell,
28 + useProjects,
29 + useQuietThreadShell,
30 +} from "~/state/entities";
31 +import { readPrimaryEnvironmentId, usePrimaryEnvironmentId } from "~/state/environments";
32 import { readPreparedConnection } from "~/state/session";
33
34 import { isLocalLoopbackHost, normalizeHostname } from "./browser/browserTargetResolver";
35 +import { useComposerDraftStore } from "./composerDraftStore";
36 +import {
37 + ensureClientSettingsHydrated,
38 + getClientSettings,
39 + isClientSettingsHydrated,
40 + useClientSettings,
41 +} from "./hooks/useSettings";
42 import { resolveStorage } from "./lib/storage";
43 +import {
44 + deriveLogicalProjectKeyFromSettings,
45 + derivePhysicalProjectKey,
46 + selectProjectGroupingSettings,
47 + type ProjectGroupingSettings,
48 +} from "./logicalProject";
49 +import { buildPhysicalToLogicalProjectKeyMap } from "./sidebarProjectGrouping";
50 +import type { Project } from "./types";
51
52 export type BrowserHistoryEntry = { url: string; lastVisitedAt: number; title?: string };
53
@@ -313,10 +345,63 @@ function migratePersistedThreadProjectKeys(
345 );
346 }
347
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().getDraftSessionByRef(ref);
370 + return draft ? scopeProjectRef(draft.environmentId, draft.projectId) : null;
371 +}
372 +
373 +/**
374 + * Registers the thread under its current project so queued visits and titles
375 + * land. Until the project loads, or client settings hydrate (the grouping is
376 + * unknown before then), the last registration stands and writes stay queued.
377 + */
378 +function resolveThreadProjectKey(ref: ScopedThreadRef): string | undefined {
379 + const state = useBrowserHistoryStore.getState();
380 + const registered = state.projectKeyByThreadKey[scopedThreadKey(ref)];
381 + if (!isClientSettingsHydrated()) {
382 + void ensureClientSettingsHydrated().then(
383 + () => resolveThreadProjectKey(ref),
384 + () => undefined,
385 + );
386 + return registered;
387 + }
388 + const projectRef = readThreadProjectRef(ref);
389 + const project = projectRef ? readProject(projectRef) : null;
390 + if (!project) return registered;
391 + const projectKey = deriveHistoryProjectKey(
392 + project,
393 + readProjects(),
394 + selectProjectGroupingSettings(getClientSettings()),
395 + readPrimaryEnvironmentId(),
396 + );
397 + state.registerThreadProject(ref, projectKey);
398 + return projectKey;
399 +}
400 +
401 export function recordVisitForThread(ref: ScopedThreadRef, url: string, at?: number): void {
402 const threadKey = scopedThreadKey(ref);
403 + const projectKey = resolveThreadProjectKey(ref);
404 const state = useBrowserHistoryStore.getState();
319 - const projectKey = state.projectKeyByThreadKey[threadKey];
405 const visitAt = at ?? Date.now();
406 const connection = readPreparedConnection(ref.environmentId);
407 const environmentHostname = connection ? new URL(connection.httpBaseUrl).hostname : null;
@@ -412,21 +497,45 @@ export function startPreviewTitleRecording(): () => void {
497 }
498
499 export function removeUrlForThread(ref: ScopedThreadRef, url: string): void {
415 - const state = useBrowserHistoryStore.getState();
416 - const projectKey = state.projectKeyByThreadKey[scopedThreadKey(ref)];
500 + const projectKey = resolveThreadProjectKey(ref);
501 if (!projectKey) return;
418 - state.removeUrl(projectKey, url);
502 + useBrowserHistoryStore.getState().removeUrl(projectKey, url);
503 }
504
505 const EMPTY_HISTORY: ReadonlyArray<BrowserHistoryEntry> = [];
506
507 +function useThreadHistoryProjectKey(ref: ScopedThreadRef): string | null {
508 + const threadKey = scopedThreadKey(ref);
509 + const shellProjectId = useQuietThreadShell(ref)?.projectId ?? null;
510 + const draftProjectKey = useComposerDraftStore((state) => {
511 + const draft = state.getDraftSessionByRef(ref);
512 + return draft ? scopedProjectKey(scopeProjectRef(draft.environmentId, draft.projectId)) : null;
513 + });
514 + const projectKey = shellProjectId
515 + ? scopedProjectKey(scopeProjectRef(ref.environmentId, shellProjectId))
516 + : draftProjectKey;
517 + const projects = useProjects();
518 + const settings = useClientSettings(selectProjectGroupingSettings);
519 + const primaryEnvironmentId = usePrimaryEnvironmentId();
520 + const registered = useBrowserHistoryStore(
521 + (state) => state.projectKeyByThreadKey[threadKey] ?? null,
522 + );
523 + const project = projects.find(
524 + (candidate) =>
525 + scopedProjectKey(scopeProjectRef(candidate.environmentId, candidate.id)) === projectKey,
526 + );
527 + return project
528 + ? deriveHistoryProjectKey(project, projects, settings, primaryEnvironmentId)
529 + : registered;
530 +}
531 +
532 export function useThreadRecentHistory(
533 ref: ScopedThreadRef,
534 limit: number,
535 ): ReadonlyArray<BrowserHistoryEntry> {
536 + const projectKey = useThreadHistoryProjectKey(ref);
537 return useBrowserHistoryStore(
538 useShallow((state) => {
429 - const projectKey = state.projectKeyByThreadKey[scopedThreadKey(ref)];
539 const entries = projectKey ? state.byProjectKey[projectKey] : undefined;
540 return entries && entries.length > 0 ? entries.slice(0, limit) : EMPTY_HISTORY;
541 }),
apps/web/src/hooks/useSettings.ts
+4
@@ -265,6 +265,10 @@ export function getClientSettings(): ClientSettings {
265 return getClientSettingsSnapshot();
266 }
267
268 +export function isClientSettingsHydrated(): boolean {
269 + return getClientSettingsHydratedSnapshot();
270 +}
271 +
272 /** Failed reads reject and remain retryable so defaults cannot overwrite saved preferences. */
273 export function ensureClientSettingsHydrated(): Promise<void> {
274 return hydrateClientSettings();
apps/web/src/state/environments.ts
+5
@@ -9,6 +9,7 @@ import type { EnvironmentId } from "@t3tools/contracts";
9 import * as Option from "effect/Option";
10
11 import { environmentCatalog } from "../connection/catalog";
12 +import { appAtomRegistry } from "../rpc/atomRegistry";
13 import {
14 environmentPresentations,
15 environmentSummaries,
@@ -64,6 +65,10 @@ export function usePrimaryEnvironmentId(): EnvironmentId | null {
65 return useAtomValue(primaryEnvironmentIdAtom);
66 }
67
68 +export function readPrimaryEnvironmentId(): EnvironmentId | null {
69 + return appAtomRegistry.get(primaryEnvironmentIdAtom);
70 +}
71 +
72 export function useEnvironment(
73 environmentId: EnvironmentId | null,
74 ): EnvironmentPresentation | null {