import { createFileRoute, useNavigate } from "@tanstack/react-router";
import { useEffect } from "react";
import ChatView from "../components/ChatView";
import { threadHasStarted } from "../components/ChatView.logic";
import {
DraftId,
markPromotedDraftThreadByRef,
useComposerDraftStore,
} from "../composerDraftStore";
import { SidebarInset } from "../components/ui/sidebar";
import { buildThreadRouteParams } from "../threadRoutes";
import { useThread, useThreadRefs } from "../state/entities";
function DraftChatThreadRouteView() {
const navigate = useNavigate();
const { draftId: rawDraftId } = Route.useParams();
const draftId = DraftId.make(rawDraftId);
const draftSession = useComposerDraftStore((store) => store.getDraftSession(draftId));
const threadRefs = useThreadRefs();
const inferredThreadRef = draftSession
? (threadRefs.find(
(ref) =>
ref.environmentId === draftSession.environmentId &&
ref.threadId === draftSession.threadId,
) ?? null)
: null;
const serverThreadRef = draftSession?.promotedTo ?? inferredThreadRef;
const serverThread = useThread(serverThreadRef);
const serverThreadStarted = threadHasStarted(serverThread);
const canonicalThreadRef = serverThreadStarted ? serverThreadRef : null;
useEffect(() => {
if (!inferredThreadRef || draftSession?.promotedTo) {
return;
}
markPromotedDraftThreadByRef(inferredThreadRef);
}, [draftSession?.promotedTo, inferredThreadRef]);
useEffect(() => {
if (!canonicalThreadRef) {
return;
}
void navigate({
to: "/$environmentId/$threadId",
params: buildThreadRouteParams(canonicalThreadRef),
replace: true,
});
}, [canonicalThreadRef, navigate]);
useEffect(() => {
if (draftSession || canonicalThreadRef) {
return;
}
void navigate({ to: "/", replace: true });
}, [canonicalThreadRef, draftSession, navigate]);
if (canonicalThreadRef) {
return (
);
}
if (!draftSession) {
return null;
}
return (
);
}
export const Route = createFileRoute("/_chat/draft/$draftId")({
component: DraftChatThreadRouteView,
});