The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { Outlet, createFileRoute, redirect } from "@tanstack/react-router"; |
| 2 | |
| 3 | import { ThreadRouteView } from "../components/ThreadRouteView"; |
| 4 | import { GlobalVoiceInputIndicator } from "../components/chat/GlobalVoiceInputIndicator"; |
| 5 | import { isAppModalPage } from "../components/sidebar/mainAppLocation"; |
| 6 | import { SidebarInset } from "../components/ui/sidebar"; |
| 7 | import { resolveThreadRouteTarget } from "../threadRoutes"; |
| 8 | import { retainThreadFindForRoute } from "../components/chat/threadFindStore"; |
| 9 | import { ThreadLinkPrompt } from "../components/promptLinks/ThreadLinkPrompt"; |
| 10 | import { useShownThreadRouteTarget } from "../hooks/useShownThreadRouteTarget"; |
| 11 | |
| 12 | function ChatRouteLayout() { |
| 13 | // Render both routes here so draft promotion keeps the same ChatView mounted. |
| 14 | const { shownTarget, modalOpen } = useShownThreadRouteTarget(); |
| 15 | return ( |
| 16 | <> |
| 17 | {shownTarget?.kind === "server" && !modalOpen ? ( |
| 18 | <ThreadLinkPrompt threadRef={shownTarget.threadRef} /> |
| 19 | ) : null} |
| 20 | {shownTarget ? ( |
| 21 | <ThreadRouteView target={shownTarget} background={modalOpen} /> |
| 22 | ) : modalOpen ? ( |
| 23 | <SidebarInset className="h-dvh" /> |
| 24 | ) : ( |
| 25 | <Outlet /> |
| 26 | )} |
| 27 | {modalOpen ? <Outlet /> : null} |
| 28 | <GlobalVoiceInputIndicator hidden={modalOpen} /> |
| 29 | </> |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | export const Route = createFileRoute("/_chat")({ |
| 34 | beforeLoad: async ({ context, location }) => { |
| 35 | if ( |
| 36 | context.authGateState.status !== "authenticated" && |
| 37 | context.authGateState.status !== "hosted-static" |
| 38 | ) { |
| 39 | throw redirect({ to: "/pair", replace: true }); |
| 40 | } |
| 41 | return { appModalPage: isAppModalPage(location.pathname) }; |
| 42 | }, |
| 43 | component: ChatRouteLayout, |
| 44 | // Every match carries the whole location's params, so this layout sees the thread route's too. |
| 45 | onEnter: ({ context, params }) => retainThreadFindForRouteMatch(context.appModalPage, params), |
| 46 | onStay: ({ context, params }) => retainThreadFindForRouteMatch(context.appModalPage, params), |
| 47 | onLeave: () => retainThreadFindForRoute({ modalPage: false, threadRef: null }), |
| 48 | }); |
| 49 | |
| 50 | function retainThreadFindForRouteMatch( |
| 51 | modalPage: boolean, |
| 52 | params: Partial<Record<"environmentId" | "threadId", string>>, |
| 53 | ) { |
| 54 | const target = resolveThreadRouteTarget(params); |
| 55 | retainThreadFindForRoute({ |
| 56 | modalPage, |
| 57 | threadRef: target?.kind === "server" ? target.threadRef : null, |
| 58 | }); |
| 59 | } |