11import { scopedThreadKey } from "@t3tools/client-runtime/environment" ;
2- import type { ScopedThreadRef } from "@t3tools/contracts" ;
2+ import type { PreviewSessionSnapshot , ScopedThreadRef } from "@t3tools/contracts" ;
33import { create } from "zustand" ;
44import { createJSONStorage , persist } from "zustand/middleware" ;
55import { useShallow } from "zustand/react/shallow" ;
@@ -261,7 +261,10 @@ export const useBrowserHistoryStore = create<BrowserHistoryStoreState>()(
261261 insertOrdered : true ,
262262 environmentHostname : visit . environmentHostname ,
263263 } ) ;
264- recordPreviewTitles ( ref , readThreadPreviewState ( ref ) ) ;
264+ if ( pendingVisits )
265+ recordPreviewTitles ( ref , readThreadPreviewState ( ref ) , {
266+ urls : pendingVisits . map ( ( visit ) => visit . url ) ,
267+ } ) ;
265268 } ,
266269 } ) ,
267270 {
@@ -329,32 +332,42 @@ export function recordVisitForThread(ref: ScopedThreadRef, url: string, at?: num
329332 }
330333 state . recordVisit ( projectKey , url , visitAt , { environmentHostname } ) ;
331334 // A page can settle before the address bar records its visit.
332- recordPreviewTitles ( ref , readThreadPreviewState ( ref ) ) ;
335+ recordPreviewTitles ( ref , readThreadPreviewState ( ref ) , { urls : [ url ] } ) ;
333336}
334337
335338/**
336- * Titles only update entries already in the thread's project history, so they
337- * are applied both when a page settles and when its visit is recorded.
339+ * Titles only update entries already in history, so they apply on settle and on
340+ * visit. Callers pass only what just happened: history is shared across a
341+ * project's threads, so replaying an older settle would clobber a newer title.
338342 */
339343function recordPreviewTitles (
340344 ref : ScopedThreadRef ,
341345 preview : ThreadPreviewState ,
342- previousSessions ?: ThreadPreviewState [ "sessions" ] ,
346+ only :
347+ | { readonly urls : ReadonlyArray < string > }
348+ | { readonly changedSince : ThreadPreviewState [ "sessions" ] } ,
343349) : void {
344350 const state = useBrowserHistoryStore . getState ( ) ;
345351 const projectKey = state . projectKeyByThreadKey [ scopedThreadKey ( ref ) ] ;
346352 if ( ! projectKey || ! state . byProjectKey [ projectKey ] ?. length ) return ;
347353 const connection = readPreparedConnection ( ref . environmentId ) ;
348354 const environmentHostname = connection ? new URL ( connection . httpBaseUrl ) . hostname : null ;
355+ const titleKey = ( url : string ) => {
356+ const normalized = normalizeHistoryUrl ( url ) ;
357+ return normalized ? titleLookupKey ( normalized , environmentHostname ) : null ;
358+ } ;
359+ const visitKeys = "urls" in only ? new Set ( only . urls . map ( titleKey ) ) : null ;
360+ const include = ( session : PreviewSessionSnapshot , url : string ) =>
361+ "changedSince" in only
362+ ? only . changedSince [ session . tabId ] !== session
363+ : ( visitKeys ?. has ( titleKey ( url ) ) ?? false ) ;
349364 // Tabs can share a URL; applying oldest first lets the newest settle win.
350- // Unchanged sessions are skipped so a stale title cannot overwrite a newer
351- // one another thread in the same project recorded since.
352- const sessions = Object . values ( preview . sessions )
353- . filter ( ( session ) => previousSessions ?. [ session . tabId ] !== session )
354- . toSorted ( ( a , b ) => a . updatedAt . localeCompare ( b . updatedAt ) ) ;
365+ const sessions = Object . values ( preview . sessions ) . toSorted ( ( a , b ) =>
366+ a . updatedAt . localeCompare ( b . updatedAt ) ,
367+ ) ;
355368 for ( const session of sessions ) {
356369 const status = session . navStatus ;
357- if ( status . _tag !== "Success" || ! status . title ) continue ;
370+ if ( status . _tag !== "Success" || ! status . title || ! include ( session , status . url ) ) continue ;
358371 state . setTitleForUrl ( projectKey , status . url , status . title , environmentHostname ) ;
359372 }
360373}
@@ -373,7 +386,7 @@ export function startPreviewTitleRecording(): () => void {
373386 if ( next . sessions === previousSessions ) return ;
374387 const changedSince = previousSessions ;
375388 previousSessions = next . sessions ;
376- recordPreviewTitles ( threadRef , next , changedSince ) ;
389+ recordPreviewTitles ( threadRef , next , { changedSince } ) ;
377390 } ) ,
378391 ) ;
379392 }
0 commit comments