1- import { scopedThreadKey } from "@t3tools/client-runtime/environment" ;
2- import type { ScopedThreadRef } from "@t3tools/contracts" ;
1+ import {
2+ scopedProjectKey ,
3+ scopedThreadKey ,
4+ scopeProjectRef ,
5+ } from "@t3tools/client-runtime/environment" ;
6+ import type { EnvironmentId , ScopedProjectRef , ScopedThreadRef } from "@t3tools/contracts" ;
37import { create } from "zustand" ;
48import { createJSONStorage , persist } from "zustand/middleware" ;
59import { useShallow } from "zustand/react/shallow" ;
610
711import { normalizePreviewUrl } from "@t3tools/shared/preview" ;
12+ import {
13+ readProject ,
14+ readProjects ,
15+ readThreadShell ,
16+ useProjects ,
17+ useQuietThreadShell ,
18+ } from "~/state/entities" ;
19+ import { readPrimaryEnvironmentId , usePrimaryEnvironmentId } from "~/state/environments" ;
820import { readPreparedConnection } from "~/state/session" ;
921
1022import { isLocalLoopbackHost , normalizeHostname } from "./browser/browserTargetResolver" ;
23+ import { useComposerDraftStore } from "./composerDraftStore" ;
24+ import { getClientSettings , useClientSettings } from "./hooks/useSettings" ;
1125import { resolveStorage } from "./lib/storage" ;
26+ import {
27+ deriveLogicalProjectKeyFromSettings ,
28+ derivePhysicalProjectKey ,
29+ selectProjectGroupingSettings ,
30+ type ProjectGroupingSettings ,
31+ } from "./logicalProject" ;
32+ import { buildPhysicalToLogicalProjectKeyMap } from "./sidebarProjectGrouping" ;
33+ import type { Project } from "./types" ;
1234
1335export type BrowserHistoryEntry = { url : string ; lastVisitedAt : number ; title ?: string } ;
1436
@@ -323,10 +345,55 @@ function migratePersistedThreadProjectKeys(
323345 ) ;
324346}
325347
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 ( ) . draftThreadsByThreadKey [ scopedThreadKey ( ref ) ] ;
370+ return draft ? scopeProjectRef ( draft . environmentId , draft . projectId ) : null ;
371+ }
372+
373+ /**
374+ * Registers the thread under its current project so earlier visits and titles
375+ * land. A thread whose project is not loaded yet keeps its last registration,
376+ * or none, and its writes wait in the pending queues.
377+ */
378+ function resolveThreadProjectKey ( ref : ScopedThreadRef ) : string | undefined {
379+ const projectRef = readThreadProjectRef ( ref ) ;
380+ const project = projectRef ? readProject ( projectRef ) : null ;
381+ const state = useBrowserHistoryStore . getState ( ) ;
382+ if ( ! project ) return state . projectKeyByThreadKey [ scopedThreadKey ( ref ) ] ;
383+ const projectKey = deriveHistoryProjectKey (
384+ project ,
385+ readProjects ( ) ,
386+ selectProjectGroupingSettings ( getClientSettings ( ) ) ,
387+ readPrimaryEnvironmentId ( ) ,
388+ ) ;
389+ state . registerThreadProject ( ref , projectKey ) ;
390+ return projectKey ;
391+ }
392+
326393export function recordVisitForThread ( ref : ScopedThreadRef , url : string , at ?: number ) : void {
327394 const threadKey = scopedThreadKey ( ref ) ;
395+ const projectKey = resolveThreadProjectKey ( ref ) ;
328396 const state = useBrowserHistoryStore . getState ( ) ;
329- const projectKey = state . projectKeyByThreadKey [ threadKey ] ;
330397 const visitAt = at ?? Date . now ( ) ;
331398 const connection = readPreparedConnection ( ref . environmentId ) ;
332399 const environmentHostname = connection ? new URL ( connection . httpBaseUrl ) . hostname : null ;
@@ -350,8 +417,8 @@ export function setTitleForThreadUrl(
350417 environmentHostname ?: string | null ,
351418) : void {
352419 const threadKey = scopedThreadKey ( ref ) ;
420+ const projectKey = resolveThreadProjectKey ( ref ) ;
353421 const state = useBrowserHistoryStore . getState ( ) ;
354- const projectKey = state . projectKeyByThreadKey [ threadKey ] ;
355422 if ( ! projectKey ) {
356423 useBrowserHistoryStore . setState ( {
357424 pendingTitlesByThreadKey : addPendingByThread ( state . pendingTitlesByThreadKey , threadKey , {
@@ -366,21 +433,45 @@ export function setTitleForThreadUrl(
366433}
367434
368435export function removeUrlForThread ( ref : ScopedThreadRef , url : string ) : void {
369- const state = useBrowserHistoryStore . getState ( ) ;
370- const projectKey = state . projectKeyByThreadKey [ scopedThreadKey ( ref ) ] ;
436+ const projectKey = resolveThreadProjectKey ( ref ) ;
371437 if ( ! projectKey ) return ;
372- state . removeUrl ( projectKey , url ) ;
438+ useBrowserHistoryStore . getState ( ) . removeUrl ( projectKey , url ) ;
373439}
374440
375441const EMPTY_HISTORY : ReadonlyArray < BrowserHistoryEntry > = [ ] ;
376442
443+ function useThreadHistoryProjectKey ( ref : ScopedThreadRef ) : string | null {
444+ const threadKey = scopedThreadKey ( ref ) ;
445+ const shellProjectId = useQuietThreadShell ( ref ) ?. projectId ?? null ;
446+ const draftProjectKey = useComposerDraftStore ( ( state ) => {
447+ const draft = state . draftThreadsByThreadKey [ threadKey ] ;
448+ return draft ? scopedProjectKey ( scopeProjectRef ( draft . environmentId , draft . projectId ) ) : null ;
449+ } ) ;
450+ const projectKey = shellProjectId
451+ ? scopedProjectKey ( scopeProjectRef ( ref . environmentId , shellProjectId ) )
452+ : draftProjectKey ;
453+ const projects = useProjects ( ) ;
454+ const settings = useClientSettings ( selectProjectGroupingSettings ) ;
455+ const primaryEnvironmentId = usePrimaryEnvironmentId ( ) ;
456+ const registered = useBrowserHistoryStore (
457+ ( state ) => state . projectKeyByThreadKey [ threadKey ] ?? null ,
458+ ) ;
459+ const project = projects . find (
460+ ( candidate ) =>
461+ scopedProjectKey ( scopeProjectRef ( candidate . environmentId , candidate . id ) ) === projectKey ,
462+ ) ;
463+ return project
464+ ? deriveHistoryProjectKey ( project , projects , settings , primaryEnvironmentId )
465+ : registered ;
466+ }
467+
377468export function useThreadRecentHistory (
378469 ref : ScopedThreadRef ,
379470 limit : number ,
380471) : ReadonlyArray < BrowserHistoryEntry > {
472+ const projectKey = useThreadHistoryProjectKey ( ref ) ;
381473 return useBrowserHistoryStore (
382474 useShallow ( ( state ) => {
383- const projectKey = state . projectKeyByThreadKey [ scopedThreadKey ( ref ) ] ;
384475 const entries = projectKey ? state . byProjectKey [ projectKey ] : undefined ;
385476 return entries && entries . length > 0 ? entries . slice ( 0 , limit ) : EMPTY_HISTORY ;
386477 } ) ,
0 commit comments