1- import { scopedThreadKey } from "@t3tools/client-runtime/environment" ;
2- import type { ScopedThreadRef } from "@t3tools/contracts" ;
3- import { useNavigate } from "@tanstack/react-router" ;
4- import { useEffect , useState } from "react" ;
1+ import { Navigate , useNavigate } from "@tanstack/react-router" ;
2+ import { useEffect } from "react" ;
53
64import ChatView from "./ChatView" ;
75import { BackgroundThreadContext } from "./chat/backgroundThread" ;
8- import { resolveDraftPromotionNavigationTarget , threadHasStarted } from "./ChatView.logic" ;
9- import { waitForDraftHeroTransition } from "./chat/draftHeroTransition" ;
106import { SidebarInset } from "./ui/sidebar" ;
11- import {
12- finalizePromotedDraftThreadByRef ,
13- markPromotedDraftThreadByRef ,
14- useBackgroundDraftSubmissionPending ,
15- useComposerDraftStore ,
16- } from "../composerDraftStore" ;
7+ import { useComposerDraftStore } from "../composerDraftStore" ;
178import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore" ;
18- import { useEnvironmentThreadRefs , useThreadRefs , useQuietThreadShell } from "../state/entities" ;
9+ import { useEnvironmentThreadRefs , useQuietThreadShell } from "../state/entities" ;
1910import { useEnvironmentShellStatus } from "../state/shell" ;
2011import { useEnvironmentThread } from "../state/threads" ;
2112import {
@@ -26,7 +17,9 @@ import {
2617} from "../threadRoutes" ;
2718
2819/**
29- * Render in the shared layout to retain ChatView across route swaps; plain server threads remain unkeyed.
20+ * Unkeyed so navigating between server threads reuses one ChatView. Rendered by the `_chat`
21+ * layout, since an element only survives a route swap when the same parent renders it. Draft
22+ * promotion runs in the draft store; this only follows the redirect it leaves behind.
3023 * `background` keeps the thread mounted, inert and in place under Settings or Usage.
3124 */
3225export function ThreadRouteView ( {
@@ -41,54 +34,23 @@ export function ThreadRouteView({
4134 const draftSession = useComposerDraftStore ( ( store ) =>
4235 draftId === null ? null : store . getDraftSession ( draftId ) ,
4336 ) ;
44- const threadRefs = useThreadRefs ( ) ;
45- // Promotion can begin before the server creates the reserved thread.
46- const inferredThreadRef = draftSession
47- ? ( threadRefs . find (
48- ( ref ) =>
49- ref . environmentId === draftSession . environmentId &&
50- ref . threadId === draftSession . threadId ,
51- ) ?? null )
52- : null ;
53- const serverThreadRef : ScopedThreadRef | null =
54- target . kind === "server" ? target . threadRef : inferredThreadRef ;
55- const serverThread = useQuietThreadShell ( serverThreadRef ) ;
56- const backgroundSubmissionPending = useBackgroundDraftSubmissionPending (
57- target . kind === "draft" ? serverThreadRef : null ,
37+ const draftRedirect = useComposerDraftStore ( ( store ) =>
38+ draftId === null ? null : store . getPromotedDraftRedirect ( draftId ) ,
5839 ) ;
59- const canonicalThreadRef =
60- target . kind === "draft"
61- ? resolveDraftPromotionNavigationTarget ( {
62- serverThreadRef,
63- serverThread,
64- backgroundSubmissionPending,
65- } )
66- : null ;
67-
40+ const serverThreadRef = target . kind === "server" ? target . threadRef : null ;
41+ const serverThreadShell = useQuietThreadShell ( serverThreadRef ) ;
6842 const shellStatus = useEnvironmentShellStatus ( serverThreadRef ?. environmentId ?? null ) ;
69- const serverThreadShell = serverThread ;
7043 const environmentThreadRefs = useEnvironmentThreadRefs ( serverThreadRef ?. environmentId ?? null ) ;
7144 const bootstrapComplete = isThreadRouteSnapshotAuthoritative ( shellStatus ) ;
7245 const draftThread = useComposerDraftStore ( ( store ) =>
7346 serverThreadRef ? store . getDraftThreadByRef ( serverThreadRef ) : null ,
7447 ) ;
48+ // The ChatView that carried a draft keeps its identity once the thread route takes over.
7549 const promotedDraftId = useComposerDraftStore ( ( store ) =>
76- target . kind === "server" ? store . getDraftIdByRef ( target . threadRef ) : null ,
50+ serverThreadRef
51+ ? ( store . getDraftIdByRef ( serverThreadRef ) ?? store . getPromotedDraftIdByRef ( serverThreadRef ) )
52+ : null ,
7753 ) ;
78- // Latch the key through promotion because the draft record disappears after the route swap.
79- const [ chatViewKey , setChatViewKey ] = useState < { threadKey : string ; key : string } | null > ( null ) ;
80- const serverThreadKey = target . kind === "server" ? scopedThreadKey ( target . threadRef ) : null ;
81- const nextChatViewKey =
82- serverThreadKey === null
83- ? null
84- : chatViewKey ?. threadKey === serverThreadKey
85- ? chatViewKey
86- : promotedDraftId
87- ? { threadKey : serverThreadKey , key : promotedDraftId }
88- : null ;
89- if ( nextChatViewKey !== chatViewKey ) {
90- setChatViewKey ( nextChatViewKey ) ;
91- }
9254 const environmentHasDraftThreads = useComposerDraftStore ( ( store ) =>
9355 serverThreadRef ? store . hasDraftThreadsInEnvironment ( serverThreadRef . environmentId ) : false ,
9456 ) ;
@@ -110,43 +72,8 @@ export function ThreadRouteView({
11072 unlistedThreadDetail,
11173 draftThreadExists : draftThread !== null ,
11274 } ) ;
113- const serverThreadStarted = threadHasStarted ( serverThreadShell ) ;
11475 const environmentHasAnyThreads = environmentThreadRefs . length > 0 || environmentHasDraftThreads ;
11576
116- useEffect ( ( ) => {
117- if ( ! inferredThreadRef || draftSession ?. promotedTo ) {
118- return ;
119- }
120- markPromotedDraftThreadByRef ( inferredThreadRef ) ;
121- } , [ draftSession ?. promotedTo , inferredThreadRef ] ) ;
122-
123- useEffect ( ( ) => {
124- if ( ! canonicalThreadRef || background ) {
125- return ;
126- }
127- let cancelled = false ;
128- void waitForDraftHeroTransition ( ) . then ( ( ) => {
129- if ( cancelled ) {
130- return ;
131- }
132- void navigate ( {
133- to : "/$environmentId/$threadId" ,
134- params : buildThreadRouteParams ( canonicalThreadRef ) ,
135- replace : true ,
136- } ) ;
137- } ) ;
138- return ( ) => {
139- cancelled = true ;
140- } ;
141- } , [ background , canonicalThreadRef , navigate ] ) ;
142-
143- useEffect ( ( ) => {
144- if ( background || target . kind !== "draft" || draftSession || canonicalThreadRef ) {
145- return ;
146- }
147- void navigate ( { to : "/" , replace : true } ) ;
148- } , [ background , canonicalThreadRef , draftSession , navigate , target . kind ] ) ;
149-
15077 useEffect ( ( ) => {
15178 if ( target . kind !== "server" || ! bootstrapComplete ) {
15279 return ;
@@ -160,14 +87,8 @@ export function ThreadRouteView({
16087 }
16188 } , [ background , bootstrapComplete , environmentHasAnyThreads , navigate , renderState , target ] ) ;
16289
163- useEffect ( ( ) => {
164- if ( target . kind !== "server" || ! serverThreadStarted || ! draftThread ) {
165- return ;
166- }
167- finalizePromotedDraftThreadByRef ( target . threadRef ) ;
168- } , [ draftThread , serverThreadStarted , target ] ) ;
169-
17090 let view : React . ReactNode = null ;
91+ let redirect : React . ReactNode = null ;
17192 if ( target . kind === "draft" ) {
17293 if ( draftSession ) {
17394 view = (
@@ -180,11 +101,30 @@ export function ThreadRouteView({
180101 forceExpandedMobileComposer
181102 />
182103 ) ;
104+ } else if ( draftRedirect ) {
105+ // Same slot and key as the server route renders, so the swap keeps this instance.
106+ view = (
107+ < ChatView
108+ key = { target . draftId }
109+ environmentId = { draftRedirect . environmentId }
110+ threadId = { draftRedirect . threadId }
111+ routeKind = "server"
112+ />
113+ ) ;
114+ redirect = (
115+ < Navigate
116+ to = "/$environmentId/$threadId"
117+ params = { buildThreadRouteParams ( draftRedirect ) }
118+ replace
119+ />
120+ ) ;
121+ } else {
122+ redirect = < Navigate to = "/" replace /> ;
183123 }
184124 } else if ( renderState === "ready" || ( renderState === "loading" && serverThreadShell !== null ) ) {
185125 view = (
186126 < ChatView
187- key = { nextChatViewKey ?. key }
127+ key = { promotedDraftId ?? undefined }
188128 environmentId = { target . threadRef . environmentId }
189129 threadId = { target . threadRef . threadId }
190130 routeKind = "server"
@@ -198,6 +138,7 @@ export function ThreadRouteView({
198138 inert = { background }
199139 >
200140 < BackgroundThreadContext value = { background } > { view } </ BackgroundThreadContext >
141+ { background ? null : redirect }
201142 </ SidebarInset >
202143 ) ;
203144}
0 commit comments