|
| 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 } from "react"; |
| 5 | + |
| 6 | +import ChatView from "./ChatView"; |
| 7 | +import { resolveDraftPromotionNavigationTarget, threadHasStarted } from "./ChatView.logic"; |
| 8 | +import { waitForDraftHeroTransition } from "./chat/draftHeroTransition"; |
| 9 | +import { SidebarInset } from "./ui/sidebar"; |
| 10 | +import { |
| 11 | + finalizePromotedDraftThreadByRef, |
| 12 | + markPromotedDraftThreadByRef, |
| 13 | + useBackgroundDraftSubmissionPending, |
| 14 | + useComposerDraftStore, |
| 15 | +} from "../composerDraftStore"; |
| 16 | +import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore"; |
| 17 | +import { |
| 18 | + useEnvironmentThreadRefs, |
| 19 | + useThread, |
| 20 | + useThreadDetail, |
| 21 | + useThreadRefs, |
| 22 | + useThreadShell, |
| 23 | + useThreadStatus, |
| 24 | +} from "../state/entities"; |
| 25 | +import { useEnvironmentQuery } from "../state/query"; |
| 26 | +import { environmentShell } from "../state/shell"; |
| 27 | +import { |
| 28 | + buildThreadRouteParams, |
| 29 | + resolveThreadRouteRenderState, |
| 30 | + type ThreadRouteTarget, |
| 31 | +} from "../threadRoutes"; |
| 32 | +import { resolveThreadSyncPhase } from "../threadSync"; |
| 33 | + |
| 34 | +/** |
| 35 | + * The single chat surface behind both `/draft/$draftId` and |
| 36 | + * `/$environmentId/$threadId`. A draft reserves its thread id up front, so |
| 37 | + * one ChatView element keyed by that id carries the draft through its |
| 38 | + * promotion to a server thread: the route swap only changes props, nothing |
| 39 | + * unmounts, and the timeline never paints an empty frame in between. |
| 40 | + * |
| 41 | + * Rendered by the `_chat` layout rather than by the two leaf routes, since |
| 42 | + * a keyed element only survives when the same parent renders it. |
| 43 | + */ |
| 44 | +export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) { |
| 45 | + const navigate = useNavigate(); |
| 46 | + const draftId = target.kind === "draft" ? target.draftId : null; |
| 47 | + const draftSession = useComposerDraftStore((store) => |
| 48 | + draftId === null ? null : store.getDraftSession(draftId), |
| 49 | + ); |
| 50 | + const threadRefs = useThreadRefs(); |
| 51 | + // The server thread this view is about: the route's own ref, or the draft's |
| 52 | + // reserved ref once the server knows it. |
| 53 | + const inferredThreadRef = draftSession |
| 54 | + ? (threadRefs.find( |
| 55 | + (ref) => |
| 56 | + ref.environmentId === draftSession.environmentId && |
| 57 | + ref.threadId === draftSession.threadId, |
| 58 | + ) ?? null) |
| 59 | + : null; |
| 60 | + const serverThreadRef: ScopedThreadRef | null = |
| 61 | + target.kind === "server" ? target.threadRef : (draftSession?.promotedTo ?? inferredThreadRef); |
| 62 | + const serverThread = useThread(serverThreadRef); |
| 63 | + const backgroundSubmissionPending = useBackgroundDraftSubmissionPending( |
| 64 | + target.kind === "draft" ? serverThreadRef : null, |
| 65 | + ); |
| 66 | + const canonicalThreadRef = |
| 67 | + target.kind === "draft" |
| 68 | + ? resolveDraftPromotionNavigationTarget({ |
| 69 | + serverThreadRef, |
| 70 | + serverThread, |
| 71 | + backgroundSubmissionPending, |
| 72 | + }) |
| 73 | + : null; |
| 74 | + |
| 75 | + const shell = useEnvironmentQuery( |
| 76 | + serverThreadRef === null ? null : environmentShell.stateAtom(serverThreadRef.environmentId), |
| 77 | + ); |
| 78 | + const serverThreadShell = useThreadShell(serverThreadRef); |
| 79 | + const serverThreadDetail = useThreadDetail(serverThreadRef); |
| 80 | + const serverThreadStatus = useThreadStatus(serverThreadRef); |
| 81 | + const environmentThreadRefs = useEnvironmentThreadRefs(serverThreadRef?.environmentId ?? null); |
| 82 | + const bootstrapComplete = shell.data?.snapshot._tag === "Some"; |
| 83 | + const draftThread = useComposerDraftStore((store) => |
| 84 | + serverThreadRef ? store.getDraftThreadByRef(serverThreadRef) : null, |
| 85 | + ); |
| 86 | + const environmentHasDraftThreads = useComposerDraftStore((store) => |
| 87 | + serverThreadRef ? store.hasDraftThreadsInEnvironment(serverThreadRef.environmentId) : false, |
| 88 | + ); |
| 89 | + const renderState = resolveThreadRouteRenderState({ |
| 90 | + bootstrapComplete, |
| 91 | + serverThreadShellExists: serverThreadShell !== null, |
| 92 | + serverThreadDetailExists: serverThreadDetail !== null, |
| 93 | + serverThreadDetailDeleted: serverThreadStatus === "deleted", |
| 94 | + draftThreadExists: draftThread !== null, |
| 95 | + }); |
| 96 | + const threadSyncPhase = resolveThreadSyncPhase({ |
| 97 | + detailExists: serverThreadDetail !== null, |
| 98 | + shellExists: serverThreadShell !== null, |
| 99 | + status: serverThreadStatus, |
| 100 | + }); |
| 101 | + const serverThreadStarted = threadHasStarted(serverThreadDetail); |
| 102 | + const environmentHasAnyThreads = environmentThreadRefs.length > 0 || environmentHasDraftThreads; |
| 103 | + |
| 104 | + useEffect(() => { |
| 105 | + if (!inferredThreadRef || draftSession?.promotedTo) { |
| 106 | + return; |
| 107 | + } |
| 108 | + markPromotedDraftThreadByRef(inferredThreadRef); |
| 109 | + }, [draftSession?.promotedTo, inferredThreadRef]); |
| 110 | + |
| 111 | + useEffect(() => { |
| 112 | + if (!canonicalThreadRef) { |
| 113 | + return; |
| 114 | + } |
| 115 | + let cancelled = false; |
| 116 | + void waitForDraftHeroTransition().then(() => { |
| 117 | + if (cancelled) { |
| 118 | + return; |
| 119 | + } |
| 120 | + void navigate({ |
| 121 | + to: "/$environmentId/$threadId", |
| 122 | + params: buildThreadRouteParams(canonicalThreadRef), |
| 123 | + replace: true, |
| 124 | + }); |
| 125 | + }); |
| 126 | + return () => { |
| 127 | + cancelled = true; |
| 128 | + }; |
| 129 | + }, [canonicalThreadRef, navigate]); |
| 130 | + |
| 131 | + useEffect(() => { |
| 132 | + if (target.kind !== "draft" || draftSession || canonicalThreadRef) { |
| 133 | + return; |
| 134 | + } |
| 135 | + void navigate({ to: "/", replace: true }); |
| 136 | + }, [canonicalThreadRef, draftSession, navigate, target.kind]); |
| 137 | + |
| 138 | + useEffect(() => { |
| 139 | + if (target.kind !== "server" || !bootstrapComplete) { |
| 140 | + return; |
| 141 | + } |
| 142 | + // Navigation already resolved onto this path, so a drop aimed here |
| 143 | + // passed its landing check; once the thread reads as missing it can |
| 144 | + // never be attached, release it even when there is nowhere to redirect. |
| 145 | + if (renderState === "missing") { |
| 146 | + const { clearPendingFileDropsForThread } = useSidebarPendingFileDropStore.getState(); |
| 147 | + clearPendingFileDropsForThread(target.threadRef); |
| 148 | + if (environmentHasAnyThreads) { |
| 149 | + void navigate({ to: "/", replace: true }); |
| 150 | + } |
| 151 | + } |
| 152 | + }, [bootstrapComplete, environmentHasAnyThreads, navigate, renderState, target]); |
| 153 | + |
| 154 | + useEffect(() => { |
| 155 | + if (target.kind !== "server" || !serverThreadStarted || !draftThread) { |
| 156 | + return; |
| 157 | + } |
| 158 | + finalizePromotedDraftThreadByRef(target.threadRef); |
| 159 | + }, [draftThread, serverThreadStarted, target]); |
| 160 | + |
| 161 | + let view: React.ReactNode = null; |
| 162 | + if (target.kind === "draft") { |
| 163 | + if (draftSession) { |
| 164 | + view = ( |
| 165 | + <ChatView |
| 166 | + key={scopedThreadKey({ |
| 167 | + environmentId: draftSession.environmentId, |
| 168 | + threadId: draftSession.threadId, |
| 169 | + })} |
| 170 | + draftId={target.draftId} |
| 171 | + environmentId={draftSession.environmentId} |
| 172 | + threadId={draftSession.threadId} |
| 173 | + routeKind="draft" |
| 174 | + forceExpandedMobileComposer |
| 175 | + /> |
| 176 | + ); |
| 177 | + } |
| 178 | + } else if (renderState === "ready" || (renderState === "loading" && serverThreadShell !== null)) { |
| 179 | + view = ( |
| 180 | + <ChatView |
| 181 | + key={scopedThreadKey(target.threadRef)} |
| 182 | + environmentId={target.threadRef.environmentId} |
| 183 | + threadId={target.threadRef.threadId} |
| 184 | + routeKind="server" |
| 185 | + threadSyncPhase={threadSyncPhase} |
| 186 | + /> |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + return ( |
| 191 | + <SidebarInset className="h-svh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground md:h-dvh"> |
| 192 | + {view} |
| 193 | + </SidebarInset> |
| 194 | + ); |
| 195 | +} |
0 commit comments