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