The best way to control Claude Code, Codex and more from any computer or phone

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