Open History to see the last commit
1import { RefreshIcon } from "~/components/ui/refresh-icon";
2import { scopeProjectRef } from "@t3tools/client-runtime/environment";
3import { createFileRoute, useRouter } from "@tanstack/react-router";
4import { useEffect, useEffectEvent, useRef, useState } from "react";
5
6import { isElectron } from "../env";
7import { NoProjectsHero } from "../components/NoProjectsHero";
8import { sortScopedProjectsForSidebar } from "../components/Sidebar.logic";
9import { Button } from "../components/ui/button";
10import { HostedEmptyState } from "../components/HostedEmptyState";
11import { Empty, EmptyDescription, EmptyHeader, EmptyTitle } from "../components/ui/empty";
12import { SidebarInset } from "../components/ui/sidebar";
13import { WorkspacePageHeader } from "../components/WorkspacePageHeader";
14import { useNewThreadHandler } from "../hooks/useHandleNewThread";
15import {
16 readProject,
17 useAllEnvironmentShellsBootstrapped,
18 useProjects,
19 useThreadShells,
20} from "../state/entities";
21import { useEnvironments } from "../state/environments";
22
23function ChatIndexRouteView() {
24 const { authGateState } = Route.useRouteContext();
25 const { environments, isReady } = useEnvironments();
26
27 if (authGateState.status === "hosted-static") {
28 if (!isReady) return null;
29 if (environments.length === 0) return <HostedEmptyState />;
30 }
31
32 return <IndexDraftLanding />;
33}
34
35function IndexDraftLanding() {
36 const projects = useProjects();
37 const threads = useThreadShells();
38 const bootstrapped = useAllEnvironmentShellsBootstrapped();
39 const handleNewThread = useNewThreadHandler();
40 const router = useRouter();
41 // Read at effect time; this page can stay mounted while the router is already navigating away.
42 const isStillOnIndex = useEffectEvent(() => router.state.location.pathname === "/");
43 const startedAttemptRef = useRef<number | null>(null);
44 const [startState, setStartState] = useState({ failed: false, retryRequest: 0 });
45
46 const mostRecentProject =
47 sortScopedProjectsForSidebar(projects, threads, "updated_at")[0] ?? null;
48 const attempt = startState.retryRequest;
49
50 useEffect(() => {
51 // Starting a draft after the user has left would replace the page they picked.
52 if (mostRecentProject === null || startedAttemptRef.current === attempt || !isStillOnIndex()) {
53 return;
54 }
55 const projectRef = scopeProjectRef(mostRecentProject.environmentId, mostRecentProject.id);
56 if (readProject(projectRef) === null) {
57 return;
58 }
59 startedAttemptRef.current = attempt;
60 void handleNewThread(projectRef, { replace: true })
61 .then((opened) => {
62 if (opened === null) {
63 setStartState((state) => ({ ...state, retryRequest: state.retryRequest + 1 }));
64 }
65 })
66 .catch(() => {
67 setStartState((state) => ({ ...state, failed: true }));
68 });
69 }, [attempt, handleNewThread, mostRecentProject]);
70
71 if (mostRecentProject !== null) {
72 return startState.failed ? (
73 <DraftStartError
74 onRetry={() => {
75 setStartState((state) => ({
76 failed: false,
77 retryRequest: state.retryRequest + 1,
78 }));
79 }}
80 />
81 ) : null;
82 }
83 // A pending environment cannot prove the workspace is empty.
84 return bootstrapped ? <NoProjectsHero /> : null;
85}
86
87function DraftStartError({ onRetry }: { readonly onRetry: () => void }) {
88 return (
89 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none">
90 {isElectron ? <WorkspacePageHeader electron /> : null}
91 <Empty className="flex-1">
92 <EmptyHeader className="max-w-md">
93 <EmptyTitle>Couldn’t start a new thread</EmptyTitle>
94 <EmptyDescription>
95 The project is still available. Try opening the draft again.
96 </EmptyDescription>
97 <div className="mt-5 flex justify-center">
98 <Button size="sm" onClick={onRetry}>
99 <RefreshIcon size="md" />
100 Try again
101 </Button>
102 </div>
103 </EmptyHeader>
104 </Empty>
105 </SidebarInset>
106 );
107}
108
109export const Route = createFileRoute("/_chat/")({
110 component: ChatIndexRouteView,
111});