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 stays mounted while a navigation away from
42 // "/" is still loading, but the router already points at the new location.
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 useEffect(() => {
52 // Starting a draft after the user has left would replace the page they picked.
53 if (mostRecentProject === null || startedAttemptRef.current === attempt || !isStillOnIndex()) {
54 return;
55 }
56 const projectRef = scopeProjectRef(mostRecentProject.environmentId, mostRecentProject.id);
57 if (readProject(projectRef) === null) {
58 return;
59 }
60 startedAttemptRef.current = attempt;
61 void handleNewThread(projectRef, { replace: true })
62 .then((opened) => {
63 if (opened === null) {
64 setStartState((state) => ({ ...state, retryRequest: state.retryRequest + 1 }));
65 }
66 })
67 .catch(() => {
68 setStartState((state) => ({ ...state, failed: true }));
69 });
70 }, [attempt, handleNewThread, mostRecentProject]);
71
72 if (mostRecentProject !== null) {
73 return startState.failed ? (
74 <DraftStartError
75 onRetry={() => {
76 setStartState((state) => ({
77 failed: false,
78 retryRequest: state.retryRequest + 1,
79 }));
80 }}
81 />
82 ) : null;
83 }
84 // A pending environment cannot prove the workspace is empty.
85 return bootstrapped ? <NoProjectsHero /> : null;
86}
87
88function DraftStartError({ onRetry }: { readonly onRetry: () => void }) {
89 return (
90 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none">
91 {isElectron ? <WorkspacePageHeader electron /> : null}
92 <Empty className="flex-1">
93 <EmptyHeader className="max-w-md">
94 <EmptyTitle>Couldn’t start a new thread</EmptyTitle>
95 <EmptyDescription>
96 The project is still available. Try opening the draft again.
97 </EmptyDescription>
98 <div className="mt-5 flex justify-center">
99 <Button size="sm" onClick={onRetry}>
100 <RefreshIcon size="md" />
101 Try again
102 </Button>
103 </div>
104 </EmptyHeader>
105 </Empty>
106 </SidebarInset>
107 );
108}
109
110export const Route = createFileRoute("/_chat/")({
111 component: ChatIndexRouteView,
112});