Skip to content

Commit e870f30

Browse files
fix(web): preview favicons resolve their project without ChatView syncing it
The favicon store only knew a draft thread's project after ChatView pushed it in with an effect (registerFaviconProjectForThread, capped at 100 threads), and usePreviewBridge ran a second effect to flush captures that arrived before that. A capture for a thread whose ChatView had not registered stayed pending. recordFaviconForThread now reads the project from the thread shell or the composer draft, and the environment host from the prepared connection, at capture time. Pending captures land with the next capture that can resolve them and show on the thread's tab meanwhile. Effects: ChatView.tsx drops the registration effect; usePreviewBridge.ts drops the flush effect and keeps the bridge subscription effect (an external subscription with cleanup). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c298a40 commit e870f30

4 files changed

Lines changed: 90 additions & 90 deletions

File tree

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

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
1+
import { scopedThreadKey, scopeProjectRef } from "@t3tools/client-runtime/environment";
22
import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
33
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
44

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+
}));
79

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";
821
import {
9-
flushPendingFaviconsForThread,
1022
lookupFavicon,
1123
recordFaviconForProject,
1224
recordFaviconForThread,
@@ -26,7 +38,12 @@ const favicon = (pageUrl: string, capturedAt: number, dataUrl = PNG) => ({
2638
});
2739

2840
describe("browser favicon store", () => {
29-
beforeEach(resetBrowserFaviconsForTests);
41+
beforeEach(() => {
42+
resetBrowserFaviconsForTests();
43+
metadata.projectId = null;
44+
metadata.httpBaseUrl = null;
45+
useComposerDraftStore.setState({ draftThreadsByThreadKey: {} });
46+
});
3047
afterEach(() => {
3148
vi.unstubAllGlobals();
3249
vi.restoreAllMocks();
@@ -67,45 +84,46 @@ describe("browser favicon store", () => {
6784
).toBeNull();
6885
});
6986

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);
87+
it("retains origins until the thread's project is known, then lands them with the next capture", () => {
88+
expect(recordFaviconForThread(threadRef, favicon("http://localhost:3000/", 1))).toBe(false);
89+
expect(recordFaviconForThread(threadRef, favicon("http://localhost:5173/", 2))).toBe(false);
7790
expect(
7891
Object.keys(Object.values(useBrowserFaviconStore.getState().pendingByThreadKey)[0] ?? {}),
7992
).toHaveLength(2);
8093

81-
expect(flushPendingFaviconsForThread(threadRef, projectRef, "192.168.64.2")).toBe(true);
94+
metadata.projectId = "project-1";
95+
metadata.httpBaseUrl = "http://192.168.64.2:3773";
96+
expect(recordFaviconForThread(threadRef, favicon("http://localhost:5173/", 3))).toBe(true);
8297
expect(Object.keys(useBrowserFaviconStore.getState().byKey).toSorted()).toEqual([
8398
"env-1:project-1 http://localhost:3000",
8499
"env-1:project-1 http://localhost:5173",
85100
]);
86101
expect(useBrowserFaviconStore.getState().pendingByThreadKey).toEqual({});
87102
});
88103

104+
it("files a draft thread's capture under the draft's project with no view registering it", () => {
105+
useComposerDraftStore.setState({
106+
draftThreadsByThreadKey: {
107+
[scopedThreadKey(threadRef)]: {
108+
environmentId,
109+
projectId: ProjectId.make("project-1"),
110+
} as DraftThreadState,
111+
},
112+
});
113+
expect(recordFaviconForThread(threadRef, favicon("http://localhost:3000/", 1))).toBe(true);
114+
expect(useBrowserFaviconStore.getState().byKey).toEqual({
115+
"env-1:project-1 http://localhost:3000": { dataUrl: PNG, capturedAt: 1 },
116+
});
117+
});
118+
89119
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);
120+
metadata.projectId = "project-1";
121+
expect(recordFaviconForThread(threadRef, favicon("http://localhost:3000/", 1))).toBe(true);
98122
expect(useBrowserFaviconStore.getState().byKey).toEqual({
99123
"env-1:project-1 http://localhost:3000": { dataUrl: PNG, capturedAt: 1 },
100124
});
101125

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);
126+
expect(recordFaviconForThread(threadRef, favicon("http://192.168.64.2:5173/", 2))).toBe(false);
109127
expect(Object.values(useBrowserFaviconStore.getState().pendingByThreadKey)[0]).toBeDefined();
110128
});
111129

‎apps/web/src/browserFaviconStore.ts‎

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
parseScopedProjectKey,
23
scopedProjectKey,
34
scopedThreadKey,
45
scopeProjectRef,
@@ -9,8 +10,8 @@ import { create } from "zustand";
910
import { createJSONStorage, persist } from "zustand/middleware";
1011

1112
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";
1415

1516
import {
1617
BROWSER_FAVICON_MAX_ALIASES_PER_ENTRY,
@@ -23,20 +24,19 @@ import {
2324
isValidFaviconCapturedAt,
2425
migratePersistedBrowserFaviconState,
2526
} from "./browserFaviconLogic";
27+
import { useComposerDraftStore } from "./composerDraftStore";
2628
import { createMemoryStorage, type StateStorage } from "./lib/storage";
2729

2830
const BROWSER_FAVICON_STORAGE_KEY = "t3code:browser-favicons:v1";
2931
const MAX_PENDING_ORIGINS_PER_THREAD = 10;
3032
const MAX_PENDING_THREADS = 20;
31-
const MAX_REGISTERED_THREADS = 100;
3233

3334
type PendingFavicon = DesktopPreviewFavicon;
3435
type PendingFaviconsByOrigin = Record<string, PendingFavicon>;
3536

3637
export interface BrowserFaviconStoreState {
3738
byKey: Record<string, BrowserFaviconEntry>;
3839
pendingByThreadKey: Record<string, PendingFaviconsByOrigin>;
39-
projectRefByThreadKey: Record<string, ScopedProjectRef>;
4040
recordFavicon: (
4141
key: string,
4242
dataUrl: string,
@@ -121,7 +121,6 @@ export const useBrowserFaviconStore = create<BrowserFaviconStoreState>()(
121121
(set) => ({
122122
byKey: {},
123123
pendingByThreadKey: {},
124-
projectRefByThreadKey: {},
125124
recordFavicon: (key, dataUrl, capturedAt, aliases = []) =>
126125
set((state) => {
127126
if (!isStorableFaviconDataUrl(dataUrl)) return state;
@@ -172,38 +171,23 @@ function mergeBrowserFaviconState(
172171
};
173172
}
174173

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;
195180
});
181+
if (shell?.projectId) return scopeProjectRef(threadRef.environmentId, shell.projectId);
182+
return draftProjectRef === null ? null : parseScopedProjectKey(draftProjectRef);
196183
}
197184

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;
207191
}
208192

209193
export function recordFaviconForProject(
@@ -232,18 +216,25 @@ export function recordFaviconForProject(
232216
return true;
233217
}
234218

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+
*/
235224
export function recordFaviconForThread(
236225
threadRef: ScopedThreadRef,
237226
favicon: DesktopPreviewFavicon,
238-
projectRef: ScopedProjectRef | null,
239-
environmentHostname: string | undefined,
240227
): boolean {
241228
if (
242229
!isStorableFaviconDataUrl(favicon.dataUrl) ||
243230
!isValidFaviconCapturedAt(favicon.capturedAt) ||
244231
!pendingOriginKey(favicon.pageUrl)
245232
)
246233
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);
247238
const hostname =
248239
environmentHostname !== undefined
249240
? environmentHostname
@@ -266,7 +257,7 @@ export function recordFaviconForThread(
266257
return false;
267258
}
268259

269-
export function flushPendingFaviconsForThread(
260+
function flushPendingFaviconsForThread(
270261
threadRef: ScopedThreadRef,
271262
projectRef: ScopedProjectRef,
272263
environmentHostname: string | undefined,
@@ -300,11 +291,23 @@ export function useFaviconForThreadUrl(threadRef: ScopedThreadRef, url: string):
300291
const environmentHostname = Option.isSome(preparedConnection)
301292
? new URL(preparedConnection.value.httpBaseUrl).hostname
302293
: null;
303-
return useBrowserFaviconStore((state) =>
304-
lookupFavicon(state.byKey, projectRef, url, environmentHostname),
294+
const threadKey = scopedThreadKey(threadRef);
295+
return useBrowserFaviconStore(
296+
(state) =>
297+
lookupFavicon(state.byKey, projectRef, url, environmentHostname) ??
298+
lookupPendingFavicon(state.pendingByThreadKey[threadKey], url),
305299
);
306300
}
307301

302+
/** A capture waiting for its thread's project still shows on that thread. */
303+
function lookupPendingFavicon(
304+
pending: PendingFaviconsByOrigin | undefined,
305+
url: string,
306+
): string | null {
307+
const originKey = pending ? pendingOriginKey(url) : null;
308+
return originKey ? (pending?.[originKey]?.dataUrl ?? null) : null;
309+
}
310+
308311
export function lookupFavicon(
309312
byKey: Record<string, BrowserFaviconEntry>,
310313
projectRef: ScopedProjectRef | null,
@@ -331,7 +334,6 @@ export function resetBrowserFaviconsForTests(): void {
331334
useBrowserFaviconStore.setState({
332335
byKey: {},
333336
pendingByThreadKey: {},
334-
projectRefByThreadKey: {},
335337
});
336338
useBrowserFaviconStore.persist.clearStorage();
337339
}

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
178178
import { stackedThreadToast, toastManager } from "./ui/toast";
179179
import { projectScriptIdFromCommand } from "~/projectScripts";
180180
import { newMessageId } from "~/lib/utils";
181-
import { registerFaviconProjectForThread } from "~/browserFaviconStore";
182181
import {
183182
applyProviderInstanceSettings,
184183
deriveProviderInstanceEntries,
@@ -1110,10 +1109,6 @@ export default function ChatView(props: ChatViewProps) {
11101109
}, [activeEnvironmentBootstrapComplete, activeProject, activeThreadRef]);
11111110

11121111
const primaryEnvironmentId = primaryEnvironment?.environmentId ?? null;
1113-
useEffect(() => {
1114-
if (!activeThreadRef || !activeProjectRef) return;
1115-
registerFaviconProjectForThread(activeThreadRef, activeProjectRef);
1116-
}, [activeProjectRef, activeThreadRef]);
11171112
const activeEnvironment =
11181113
activeThread == null ? null : (environmentById.get(activeThread.environmentId) ?? null);
11191114
const activeEnvironmentUnavailable =

‎apps/web/src/components/preview/usePreviewBridge.ts‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@ import type {
44
ScopedThreadRef,
55
ThreadId,
66
} from "@t3tools/contracts";
7-
import * as Option from "effect/Option";
87
import { useEffect, useEffectEvent, useRef } from "react";
98

10-
import {
11-
flushPendingFaviconsForThread,
12-
recordFaviconForThread,
13-
useFaviconProjectRefForThread,
14-
} from "~/browserFaviconStore";
9+
import { recordFaviconForThread } from "~/browserFaviconStore";
1510
import { useBrowserPointerStore } from "~/browser/browserPointerStore";
1611
import { applyPreviewDesktopState, type DesktopPreviewOverlay } from "~/previewStateStore";
1712
import { previewEnvironment } from "~/state/preview";
18-
import { usePreparedConnection } from "~/state/session";
1913
import { useAtomCommand } from "~/state/use-atom-command";
2014

2115
import { previewBridge } from "./previewBridge";
@@ -40,11 +34,6 @@ export function usePreviewBridge(input: {
4034
const reportStatus = useAtomCommand(previewEnvironment.reportStatus, "preview status report");
4135
const { environmentId, threadId } = threadRef;
4236
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;
4837

4938
const lastReportedUrl = useRef<string | null>(null);
5039
const lastReportedKind = useRef<DesktopPreviewTabState["navStatus"]["kind"] | null>(null);
@@ -58,7 +47,7 @@ export function usePreviewBridge(input: {
5847
lastDesktopNavStatus.current = state.navStatus;
5948
applyPreviewDesktopState(stableThreadRef, tabId, projectDesktopState(state));
6049
if (state.favicon) {
61-
recordFaviconForThread(stableThreadRef, state.favicon, projectRef, environmentHostname);
50+
recordFaviconForThread(stableThreadRef, state.favicon);
6251
}
6352
const reported = buildReportInput({
6453
threadId: stableThreadRef.threadId,
@@ -88,10 +77,6 @@ export function usePreviewBridge(input: {
8877
stableThreadRef,
8978
tabId,
9079
]);
91-
useEffect(() => {
92-
if (!projectRef) return;
93-
flushPendingFaviconsForThread(stableThreadRef, projectRef, environmentHostname);
94-
}, [environmentHostname, projectRef, stableThreadRef]);
9580
}
9681

9782
function shouldClearBrowserPointer(

0 commit comments

Comments
 (0)