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