@devries-cameron20 / t3code-1 / commits / f9956434fa
Fork 0

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

fix(web): preview favicons resolve their project without ChatView syncing it (#25068)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 25, 2026 at 17:23 UTC f9956434fa2f032111a249b2ca4ca09af77c9778
4 files changed +129 -91
apps/web/src/browserFaviconStore.test.ts
+68 -28
@@ -1,13 +1,26 @@
1 -import { scopeProjectRef } from "@t3tools/client-runtime/environment";
1 +import { scopedThreadKey, scopeProjectRef } from "@t3tools/client-runtime/environment";
2 import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
3 import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4
5 -vi.mock("~/state/entities", () => ({ useQuietThreadShell: () => null }));
6 -vi.mock("~/state/session", () => ({ usePreparedConnection: () => ({ _tag: "None" }) }));
5 +const metadata = vi.hoisted(() => ({
6 + projectId: null as string | null,
7 + httpBaseUrl: null as string | null,
8 +}));
9
10 +vi.mock("~/state/entities", () => ({
11 + useQuietThreadShell: () => null,
12 + readThreadShell: () => (metadata.projectId ? { projectId: metadata.projectId } : null),
13 +}));
14 +vi.mock("~/state/session", () => ({
15 + usePreparedConnection: () => ({ _tag: "None" }),
16 + readPreparedConnection: () =>
17 + metadata.httpBaseUrl ? { httpBaseUrl: metadata.httpBaseUrl } : null,
18 +}));
19 +
20 +import { useComposerDraftStore, type DraftThreadState } from "./composerDraftStore";
21 import {
9 - flushPendingFaviconsForThread,
22 lookupFavicon,
23 + lookupFaviconForThread,
24 recordFaviconForProject,
25 recordFaviconForThread,
26 resetBrowserFaviconsForTests,
@@ -26,7 +39,12 @@ const favicon = (pageUrl: string, capturedAt: number, dataUrl = PNG) => ({
39 });
40
41 describe("browser favicon store", () => {
29 - beforeEach(resetBrowserFaviconsForTests);
42 + beforeEach(() => {
43 + resetBrowserFaviconsForTests();
44 + metadata.projectId = null;
45 + metadata.httpBaseUrl = null;
46 + useComposerDraftStore.setState({ draftThreadsByThreadKey: {} });
47 + });
48 afterEach(() => {
49 vi.unstubAllGlobals();
50 vi.restoreAllMocks();
@@ -67,18 +85,16 @@ describe("browser favicon store", () => {
85 ).toBeNull();
86 });
87
70 - it("retains multiple origins until project and connection metadata hydrate", () => {
71 - expect(
72 - recordFaviconForThread(threadRef, favicon("http://localhost:3000/", 1), null, undefined),
73 - ).toBe(false);
74 - expect(
75 - recordFaviconForThread(threadRef, favicon("http://localhost:5173/", 2), null, undefined),
76 - ).toBe(false);
88 + it("retains origins until the thread's project is known, then lands them with the next capture", () => {
89 + expect(recordFaviconForThread(threadRef, favicon("http://localhost:3000/", 1))).toBe(false);
90 + expect(recordFaviconForThread(threadRef, favicon("http://localhost:5173/", 2))).toBe(false);
91 expect(
92 Object.keys(Object.values(useBrowserFaviconStore.getState().pendingByThreadKey)[0] ?? {}),
93 ).toHaveLength(2);
94
81 - expect(flushPendingFaviconsForThread(threadRef, projectRef, "192.168.64.2")).toBe(true);
95 + metadata.projectId = "project-1";
96 + metadata.httpBaseUrl = "http://192.168.64.2:3773";
97 + expect(recordFaviconForThread(threadRef, favicon("http://localhost:5173/", 3))).toBe(true);
98 expect(Object.keys(useBrowserFaviconStore.getState().byKey).toSorted()).toEqual([
99 "env-1:project-1 http://localhost:3000",
100 "env-1:project-1 http://localhost:5173",
@@ -86,29 +102,53 @@ describe("browser favicon store", () => {
102 expect(useBrowserFaviconStore.getState().pendingByThreadKey).toEqual({});
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 + });
114 + expect(recordFaviconForThread(threadRef, favicon("http://localhost:3000/", 1))).toBe(true);
115 + expect(useBrowserFaviconStore.getState().byKey).toEqual({
116 + "env-1:project-1 http://localhost:3000": { dataUrl: PNG, capturedAt: 1 },
117 + });
118 + });
119 +
120 it("persists unambiguous loopback captures while the environment is offline", () => {
90 - expect(
91 - recordFaviconForThread(
92 - threadRef,
93 - favicon("http://localhost:3000/", 1),
94 - projectRef,
95 - undefined,
96 - ),
97 - ).toBe(true);
121 + metadata.projectId = "project-1";
122 + expect(recordFaviconForThread(threadRef, favicon("http://localhost:3000/", 1))).toBe(true);
123 expect(useBrowserFaviconStore.getState().byKey).toEqual({
124 "env-1:project-1 http://localhost:3000": { dataUrl: PNG, capturedAt: 1 },
125 });
126
102 - recordFaviconForThread(
103 - threadRef,
104 - favicon("http://192.168.64.2:5173/", 2),
105 - projectRef,
106 - undefined,
107 - );
108 - expect(flushPendingFaviconsForThread(threadRef, projectRef, undefined)).toBe(false);
127 + expect(recordFaviconForThread(threadRef, favicon("http://192.168.64.2:5173/", 2))).toBe(false);
128 expect(Object.values(useBrowserFaviconStore.getState().pendingByThreadKey)[0]).toBeDefined();
129 });
130
131 + it("shows a newer pending capture over the stored favicon for the same origin", () => {
132 + const NEWER = "data:image/png;base64,BBBB";
133 + metadata.projectId = "project-1";
134 + metadata.httpBaseUrl = "http://192.168.64.2:3773";
135 + expect(recordFaviconForThread(threadRef, favicon("http://192.168.64.2:5173/", 1))).toBe(true);
136 +
137 + metadata.httpBaseUrl = null;
138 + expect(recordFaviconForThread(threadRef, favicon("http://192.168.64.2:5173/", 2, NEWER))).toBe(
139 + false,
140 + );
141 + expect(
142 + lookupFaviconForThread(
143 + useBrowserFaviconStore.getState(),
144 + scopedThreadKey(threadRef),
145 + projectRef,
146 + "http://192.168.64.2:5173/",
147 + "192.168.64.2",
148 + ),
149 + ).toBe(NEWER);
150 + });
151 +
152 it("keeps the memory shadow authoritative after an asymmetric primary write failure", () => {
153 vi.stubGlobal("window", {
154 localStorage: {
apps/web/src/browserFaviconStore.ts
+59 -41
@@ -1,4 +1,5 @@
1 import {
2 + parseScopedProjectKey,
3 scopedProjectKey,
4 scopedThreadKey,
5 scopeProjectRef,
@@ -9,8 +10,8 @@ import { create } from "zustand";
10 import { createJSONStorage, persist } from "zustand/middleware";
11
12 import { normalizeHostname } from "~/browser/browserTargetResolver";
12 -import { useQuietThreadShell } from "~/state/entities";
13 -import { usePreparedConnection } from "~/state/session";
13 +import { readThreadShell, useQuietThreadShell } from "~/state/entities";
14 +import { readPreparedConnection, usePreparedConnection } from "~/state/session";
15
16 import {
17 BROWSER_FAVICON_MAX_ALIASES_PER_ENTRY,
@@ -23,12 +24,12 @@ import {
24 isValidFaviconCapturedAt,
25 migratePersistedBrowserFaviconState,
26 } from "./browserFaviconLogic";
27 +import { useComposerDraftStore } from "./composerDraftStore";
28 import { createMemoryStorage, type StateStorage } from "./lib/storage";
29
30 const BROWSER_FAVICON_STORAGE_KEY = "t3code:browser-favicons:v1";
31 const MAX_PENDING_ORIGINS_PER_THREAD = 10;
32 const MAX_PENDING_THREADS = 20;
31 -const MAX_REGISTERED_THREADS = 100;
33
34 type PendingFavicon = DesktopPreviewFavicon;
35 type PendingFaviconsByOrigin = Record<string, PendingFavicon>;
@@ -36,7 +37,6 @@ type PendingFaviconsByOrigin = Record<string, PendingFavicon>;
37 export interface BrowserFaviconStoreState {
38 byKey: Record<string, BrowserFaviconEntry>;
39 pendingByThreadKey: Record<string, PendingFaviconsByOrigin>;
39 - projectRefByThreadKey: Record<string, ScopedProjectRef>;
40 recordFavicon: (
41 key: string,
42 dataUrl: string,
@@ -121,7 +121,6 @@ export const useBrowserFaviconStore = create<BrowserFaviconStoreState>()(
121 (set) => ({
122 byKey: {},
123 pendingByThreadKey: {},
124 - projectRefByThreadKey: {},
124 recordFavicon: (key, dataUrl, capturedAt, aliases = []) =>
125 set((state) => {
126 if (!isStorableFaviconDataUrl(dataUrl)) return state;
@@ -172,38 +171,23 @@ function mergeBrowserFaviconState(
171 };
172 }
173
175 -export function registerFaviconProjectForThread(
176 - threadRef: ScopedThreadRef,
177 - projectRef: ScopedProjectRef,
178 -): void {
179 - const threadKey = scopedThreadKey(threadRef);
180 - const state = useBrowserFaviconStore.getState();
181 - const current = state.projectRefByThreadKey[threadKey];
182 - if (
183 - current?.environmentId === projectRef.environmentId &&
184 - current.projectId === projectRef.projectId
185 - ) {
186 - return;
187 - }
188 - useBrowserFaviconStore.setState({
189 - projectRefByThreadKey: Object.fromEntries(
190 - [
191 - ...Object.entries(state.projectRefByThreadKey).filter(([key]) => key !== threadKey),
192 - [threadKey, projectRef],
193 - ].slice(-MAX_REGISTERED_THREADS),
194 - ),
174 +/** Server threads name their project in the shell; drafts in the composer draft store. */
175 +function useFaviconProjectRefForThread(threadRef: ScopedThreadRef): ScopedProjectRef | null {
176 + const shell = useQuietThreadShell(threadRef);
177 + const draftProjectRef = useComposerDraftStore((state) => {
178 + const draft = state.draftThreadsByThreadKey[scopedThreadKey(threadRef)];
179 + return draft ? scopedProjectKey(scopeProjectRef(draft.environmentId, draft.projectId)) : null;
180 });
181 + if (shell?.projectId) return scopeProjectRef(threadRef.environmentId, shell.projectId);
182 + return draftProjectRef === null ? null : parseScopedProjectKey(draftProjectRef);
183 }
184
198 -export function useFaviconProjectRefForThread(threadRef: ScopedThreadRef): ScopedProjectRef | null {
199 - const shell = useQuietThreadShell(threadRef);
200 - const shellProjectId = shell?.projectId ?? null;
201 - const environmentId = threadRef.environmentId;
202 - const shellProjectRef = shellProjectId ? scopeProjectRef(environmentId, shellProjectId) : null;
203 - const registered = useBrowserFaviconStore(
204 - (state) => state.projectRefByThreadKey[scopedThreadKey(threadRef)] ?? null,
205 - );
206 - return shellProjectRef ?? registered;
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)];
190 + return draft ? scopeProjectRef(draft.environmentId, draft.projectId) : null;
191 }
192
193 export function recordFaviconForProject(
@@ -232,11 +216,14 @@ export function recordFaviconForProject(
216 return true;
217 }
218
219 +/**
220 + * The thread's project and environment host may not be known yet when a capture
221 + * arrives; such captures wait per thread and land with the next capture that can
222 + * resolve them.
223 + */
224 export function recordFaviconForThread(
225 threadRef: ScopedThreadRef,
226 favicon: DesktopPreviewFavicon,
238 - projectRef: ScopedProjectRef | null,
239 - environmentHostname: string | undefined,
227 ): boolean {
228 if (
229 !isStorableFaviconDataUrl(favicon.dataUrl) ||
@@ -244,6 +231,10 @@ export function recordFaviconForThread(
231 !pendingOriginKey(favicon.pageUrl)
232 )
233 return false;
234 + const projectRef = readThreadProjectRef(threadRef);
235 + const connection = readPreparedConnection(threadRef.environmentId);
236 + const environmentHostname = connection ? new URL(connection.httpBaseUrl).hostname : undefined;
237 + if (projectRef) flushPendingFaviconsForThread(threadRef, projectRef, environmentHostname);
238 const hostname =
239 environmentHostname !== undefined
240 ? environmentHostname
@@ -266,7 +257,7 @@ export function recordFaviconForThread(
257 return false;
258 }
259
269 -export function flushPendingFaviconsForThread(
260 +function flushPendingFaviconsForThread(
261 threadRef: ScopedThreadRef,
262 projectRef: ScopedProjectRef,
263 environmentHostname: string | undefined,
@@ -300,28 +291,56 @@ export function useFaviconForThreadUrl(threadRef: ScopedThreadRef, url: string):
291 const environmentHostname = Option.isSome(preparedConnection)
292 ? new URL(preparedConnection.value.httpBaseUrl).hostname
293 : null;
294 + const threadKey = scopedThreadKey(threadRef);
295 return useBrowserFaviconStore((state) =>
304 - lookupFavicon(state.byKey, projectRef, url, environmentHostname),
296 + lookupFaviconForThread(state, threadKey, projectRef, url, environmentHostname),
297 );
298 }
299
300 +/**
301 + * A capture waiting for its thread's project still shows on that thread, and wins
302 + * over an older stored capture for the same origin.
303 + */
304 +export function lookupFaviconForThread(
305 + state: Pick<BrowserFaviconStoreState, "byKey" | "pendingByThreadKey">,
306 + threadKey: string,
307 + projectRef: ScopedProjectRef | null,
308 + url: string,
309 + environmentHostname: string | null,
310 +): string | null {
311 + const stored = lookupFaviconEntry(state.byKey, projectRef, url, environmentHostname);
312 + const originKey = pendingOriginKey(url);
313 + const pending = originKey ? state.pendingByThreadKey[threadKey]?.[originKey] : undefined;
314 + if (pending && (!stored || pending.capturedAt > stored.capturedAt)) return pending.dataUrl;
315 + return stored?.dataUrl ?? null;
316 +}
317 +
318 export function lookupFavicon(
319 byKey: Record<string, BrowserFaviconEntry>,
320 projectRef: ScopedProjectRef | null,
321 url: string,
322 environmentHostname: string | null,
323 ): string | null {
324 + return lookupFaviconEntry(byKey, projectRef, url, environmentHostname)?.dataUrl ?? null;
325 +}
326 +
327 +function lookupFaviconEntry(
328 + byKey: Record<string, BrowserFaviconEntry>,
329 + projectRef: ScopedProjectRef | null,
330 + url: string,
331 + environmentHostname: string | null,
332 +): BrowserFaviconEntry | null {
333 const key = projectRef
334 ? faviconKey(scopedProjectKey(projectRef), url, environmentHostname)
335 : null;
336 if (!key) return null;
337 const direct = byKey[key];
319 - if (direct) return direct.dataUrl;
338 + if (direct) return direct;
339 try {
340 const requestedHost = normalizeHostname(new URL(url).hostname);
341 const localKey = faviconKey(scopedProjectKey(projectRef!), url, requestedHost);
342 const localEntry = localKey ? byKey[localKey] : null;
324 - return localEntry?.aliases?.includes(requestedHost) ? localEntry.dataUrl : null;
343 + return localEntry?.aliases?.includes(requestedHost) ? localEntry : null;
344 } catch {
345 return null;
346 }
@@ -331,7 +350,6 @@ export function resetBrowserFaviconsForTests(): void {
350 useBrowserFaviconStore.setState({
351 byKey: {},
352 pendingByThreadKey: {},
334 - projectRefByThreadKey: {},
353 });
354 useBrowserFaviconStore.persist.clearStorage();
355 }
apps/web/src/components/ChatView.tsx
-5
@@ -178,7 +178,6 @@ import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
178 import { stackedThreadToast, toastManager } from "./ui/toast";
179 import { projectScriptIdFromCommand } from "~/projectScripts";
180 import { newMessageId } from "~/lib/utils";
181 -import { registerFaviconProjectForThread } from "~/browserFaviconStore";
181 import {
182 applyProviderInstanceSettings,
183 deriveProviderInstanceEntries,
@@ -1110,10 +1109,6 @@ export default function ChatView(props: ChatViewProps) {
1109 }, [activeEnvironmentBootstrapComplete, activeProject, activeThreadRef]);
1110
1111 const primaryEnvironmentId = primaryEnvironment?.environmentId ?? null;
1113 - useEffect(() => {
1114 - if (!activeThreadRef || !activeProjectRef) return;
1115 - registerFaviconProjectForThread(activeThreadRef, activeProjectRef);
1116 - }, [activeProjectRef, activeThreadRef]);
1112 const activeEnvironment =
1113 activeThread == null ? null : (environmentById.get(activeThread.environmentId) ?? null);
1114 const activeEnvironmentUnavailable =
apps/web/src/components/preview/usePreviewBridge.ts
+2 -17
@@ -4,18 +4,12 @@ import type {
4 ScopedThreadRef,
5 ThreadId,
6 } from "@t3tools/contracts";
7 -import * as Option from "effect/Option";
7 import { useEffect, useEffectEvent, useRef } from "react";
8
10 -import {
11 - flushPendingFaviconsForThread,
12 - recordFaviconForThread,
13 - useFaviconProjectRefForThread,
14 -} from "~/browserFaviconStore";
9 +import { recordFaviconForThread } from "~/browserFaviconStore";
10 import { useBrowserPointerStore } from "~/browser/browserPointerStore";
11 import { applyPreviewDesktopState, type DesktopPreviewOverlay } from "~/previewStateStore";
12 import { previewEnvironment } from "~/state/preview";
18 -import { usePreparedConnection } from "~/state/session";
13 import { useAtomCommand } from "~/state/use-atom-command";
14
15 import { previewBridge } from "./previewBridge";
@@ -40,11 +34,6 @@ export function usePreviewBridge(input: {
34 const reportStatus = useAtomCommand(previewEnvironment.reportStatus, "preview status report");
35 const { environmentId, threadId } = threadRef;
36 const stableThreadRef: ScopedThreadRef = { environmentId, threadId };
43 - const projectRef = useFaviconProjectRefForThread(stableThreadRef);
44 - const preparedConnection = usePreparedConnection(stableThreadRef.environmentId);
45 - const environmentHostname = Option.isSome(preparedConnection)
46 - ? new URL(preparedConnection.value.httpBaseUrl).hostname
47 - : undefined;
37
38 const lastReportedUrl = useRef<string | null>(null);
39 const lastReportedKind = useRef<DesktopPreviewTabState["navStatus"]["kind"] | null>(null);
@@ -58,7 +47,7 @@ export function usePreviewBridge(input: {
47 lastDesktopNavStatus.current = state.navStatus;
48 applyPreviewDesktopState(stableThreadRef, tabId, projectDesktopState(state));
49 if (state.favicon) {
61 - recordFaviconForThread(stableThreadRef, state.favicon, projectRef, environmentHostname);
50 + recordFaviconForThread(stableThreadRef, state.favicon);
51 }
52 const reported = buildReportInput({
53 threadId: stableThreadRef.threadId,
@@ -88,10 +77,6 @@ export function usePreviewBridge(input: {
77 stableThreadRef,
78 tabId,
79 ]);
91 - useEffect(() => {
92 - if (!projectRef) return;
93 - flushPendingFaviconsForThread(stableThreadRef, projectRef, environmentHostname);
94 - }, [environmentHostname, projectRef, stableThreadRef]);
80 }
81
82 function shouldClearBrowserPointer(