11import {
2+ parseScopedProjectKey,
23 scopedProjectKey,
34 scopedThreadKey,
45 scopeProjectRef,
@@ -9,8 +10,8 @@ import { create } from "zustand";
910import { createJSONStorage, persist } from "zustand/middleware";
1011
1112import { 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
1516import {
1617 BROWSER_FAVICON_MAX_ALIASES_PER_ENTRY,
@@ -23,20 +24,19 @@ import {
2324 isValidFaviconCapturedAt,
2425 migratePersistedBrowserFaviconState,
2526} from "./browserFaviconLogic";
27+ import { useComposerDraftStore } from "./composerDraftStore";
2628import { createMemoryStorage, type StateStorage } from "./lib/storage";
2729
2830const BROWSER_FAVICON_STORAGE_KEY = "t3code:browser-favicons:v1";
2931const MAX_PENDING_ORIGINS_PER_THREAD = 10;
3032const MAX_PENDING_THREADS = 20;
31- const MAX_REGISTERED_THREADS = 100;
3233
3334type PendingFavicon = DesktopPreviewFavicon;
3435type PendingFaviconsByOrigin = Record<string, PendingFavicon>;
3536
3637export 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
209193export 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+ */
235224export 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+
308311export 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}
0 commit comments