Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

_chat.tsx

74 lines3.0 KB

1import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2import { Outlet, createFileRoute, redirect, useMatches } from "@tanstack/react-router";
3import { useState } from "react";
4​
5import { ThreadRouteView } from "../components/ThreadRouteView";
6import { isAppModalPage } from "../components/sidebar/mainAppLocation";
7import { SidebarInset } from "../components/ui/sidebar";
8import { resolveThreadRouteTarget, type ThreadRouteTarget } from "../threadRoutes";
9import { retainThreadFindForRoute } from "../components/chat/threadFindStore";
10import { ThreadLinkPrompt } from "../components/promptLinks/ThreadLinkPrompt";
11import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget";
12​
13function threadRouteTargetKey(target: ThreadRouteTarget | null) {
14 if (!target) return null;
15 return target.kind === "server" ? scopedThreadKey(target.threadRef) : target.draftId;
17​
18function ChatRouteLayout() {
19 // Render both routes here so draft promotion keeps the same ChatView mounted.
20 const threadTarget = useThreadRouteTarget();
21 // Read matches, not location, so this agrees with the params useThreadRouteTarget reads.
22 const modalOpen = useMatches({
23 select: (matches) => matches.some((match) => isAppModalPage(match.pathname)),
24 });
25 // Settings and Usage render over the thread that opened them, so ChatView never remounts.
26 const [backgroundTarget, setBackgroundTarget] = useState(threadTarget);
27 if (!modalOpen && threadRouteTargetKey(backgroundTarget) !== threadRouteTargetKey(threadTarget)) {
28 setBackgroundTarget(threadTarget);
29 }
30 const shownTarget = modalOpen ? backgroundTarget : threadTarget;
31 return (
32 <>
33 {shownTarget?.kind === "server" && !modalOpen ? (
34 <ThreadLinkPrompt threadRef={shownTarget.threadRef} />
35 ) : null}
36 {shownTarget ? (
37 <ThreadRouteView target={shownTarget} background={modalOpen} />
38 ) : modalOpen ? (
39 <SidebarInset className="h-dvh" />
40 ) : (
41 <Outlet />
42 )}
43 {modalOpen ? <Outlet /> : null}
44 </>
45 );
47​
48export const Route = createFileRoute("/_chat")({
49 beforeLoad: async ({ context, location }) => {
50 if (
51 context.authGateState.status !== "authenticated" &&
52 context.authGateState.status !== "hosted-static"
53 ) {
54 throw redirect({ to: "/pair", replace: true });
55 }
56 return { appModalPage: isAppModalPage(location.pathname) };
57 },
58 component: ChatRouteLayout,
59 // Every match carries the whole location's params, so this layout sees the thread route's too.
60 onEnter: ({ context, params }) => retainThreadFindForRouteMatch(context.appModalPage, params),
61 onStay: ({ context, params }) => retainThreadFindForRouteMatch(context.appModalPage, params),
62 onLeave: () => retainThreadFindForRoute({ modalPage: false, threadRef: null }),
63});
64​
65function retainThreadFindForRouteMatch(
66 modalPage: boolean,
67 params: Partial<Record<"environmentId" | "threadId", string>>,
68) {
69 const target = resolveThreadRouteTarget(params);
70 retainThreadFindForRoute({
71 modalPage,
72 threadRef: target?.kind === "server" ? target.threadRef : null,
73 });