Open History to see the last commit
1import { createFileRoute, useNavigate } from "@tanstack/react-router";
2import { useEffect, useMemo } from "react";
3import ChatView from "../components/ChatView";
4import { threadHasStarted } from "../components/ChatView.logic";
5import { useComposerDraftStore, DraftId } from "../composerDraftStore";
6import { SidebarInset } from "../components/ui/sidebar";
7import { createThreadSelectorAcrossEnvironments } from "../storeSelectors";
8import { useStore } from "../store";
9import { buildThreadRouteParams } from "../threadRoutes";
10
11function DraftChatThreadRouteView() {
12 const navigate = useNavigate();
13 const { draftId: rawDraftId } = Route.useParams();
14 const draftId = DraftId.makeUnsafe(rawDraftId);
15 const draftSession = useComposerDraftStore((store) => store.getDraftSession(draftId));
16 const serverThread = useStore(
17 useMemo(
18 () => createThreadSelectorAcrossEnvironments(draftSession?.threadId ?? null),
19 [draftSession?.threadId],
20 ),
21 );
22 const serverThreadStarted = threadHasStarted(serverThread);
23 const canonicalThreadRef = useMemo(
24 () =>
25 draftSession?.promotedTo
26 ? serverThreadStarted
27 ? draftSession.promotedTo
28 : null
29 : serverThread
30 ? {
31 environmentId: serverThread.environmentId,
32 threadId: serverThread.id,
33 }
34 : null,
35 [draftSession?.promotedTo, serverThread, serverThreadStarted],
36 );
37
38 useEffect(() => {
39 if (!canonicalThreadRef) {
40 return;
41 }
42 void navigate({
43 to: "/$environmentId/$threadId",
44 params: buildThreadRouteParams(canonicalThreadRef),
45 replace: true,
46 });
47 }, [canonicalThreadRef, navigate]);
48
49 useEffect(() => {
50 if (draftSession || canonicalThreadRef) {
51 return;
52 }
53 void navigate({ to: "/", replace: true });
54 }, [canonicalThreadRef, draftSession, navigate]);
55
56 if (canonicalThreadRef) {
57 return (
58 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground">
59 <ChatView
60 environmentId={canonicalThreadRef.environmentId}
61 threadId={canonicalThreadRef.threadId}
62 routeKind="server"
63 />
64 </SidebarInset>
65 );
66 }
67
68 if (!draftSession) {
69 return null;
70 }
71
72 return (
73 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground">
74 <ChatView
75 draftId={draftId}
76 environmentId={draftSession.environmentId}
77 threadId={draftSession.threadId}
78 routeKind="draft"
79 />
80 </SidebarInset>
81 );
82}
83
84export const Route = createFileRoute("/_chat/draft/$draftId")({
85 component: DraftChatThreadRouteView,
86});