Open History to see the last commit
1import { RefreshIcon } from "~/components/ui/refresh-icon";
2import { scopeProjectRef } from "@t3tools/client-runtime/environment";
3import { createFileRoute, Link, useRouter } from "@tanstack/react-router";
4import { LinkIcon, PlusIcon } from "lucide-react";
5import { useEffect, useEffectEvent, useRef, useState } from "react";
6
7import { isLocalEnvironmentDisabled } from "../localEnvironment";
8import { isElectron } from "../env";
9import { NoProjectsHero } from "../components/NoProjectsHero";
10import { sortScopedProjectsForSidebar } from "../components/Sidebar.logic";
11import { Button } from "../components/ui/button";
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";
23import { APP_DISPLAY_NAME } from "~/branding";
24import { hasCloudPublicConfig } from "~/cloud/publicConfig";
25
26function ChatIndexRouteView() {
27 const { authGateState } = Route.useRouteContext();
28 const { environments, isReady } = useEnvironments();
29
30 if (authGateState.status === "hosted-static") {
31 if (!isReady) return null;
32 if (environments.length === 0) return <HostedStaticOnboardingState />;
33 }
34
35 return <IndexDraftLanding />;
36}
37
38function IndexDraftLanding() {
39 const projects = useProjects();
40 const threads = useThreadShells();
41 const bootstrapped = useAllEnvironmentShellsBootstrapped();
42 const handleNewThread = useNewThreadHandler();
43 const router = useRouter();
44 // Read at effect time: this page stays mounted while a navigation away from
45 // "/" is still loading, but the router already points at the new location.
46 const isStillOnIndex = useEffectEvent(() => router.state.location.pathname === "/");
47 // One start per attempt; a retry request opens the next attempt.
48 const startedAttemptRef = useRef<number | null>(null);
49 const [startState, setStartState] = useState({ failed: false, retryRequest: 0 });
50
51 const mostRecentProject = bootstrapped
52 ? (sortScopedProjectsForSidebar(projects, threads, "updated_at")[0] ?? null)
53 : null;
54 const attempt = startState.retryRequest;
55
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 (!bootstrapped) {
78 return null;
79 }
80 if (mostRecentProject !== null) {
81 return startState.failed ? (
82 <DraftStartError
83 onRetry={() => {
84 setStartState((state) => ({
85 failed: false,
86 retryRequest: state.retryRequest + 1,
87 }));
88 }}
89 />
90 ) : null;
91 }
92 return <NoProjectsHero />;
93}
94
95function DraftStartError({ onRetry }: { readonly onRetry: () => void }) {
96 return (
97 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none">
98 {isElectron ? <WorkspacePageHeader electron /> : null}
99 <Empty className="flex-1">
100 <EmptyHeader className="max-w-md">
101 <EmptyTitle>Couldn’t start a new thread</EmptyTitle>
102 <EmptyDescription>
103 The project is still available. Try opening the draft again.
104 </EmptyDescription>
105 <div className="mt-5 flex justify-center">
106 <Button size="sm" onClick={onRetry}>
107 <RefreshIcon size="md" />
108 Try again
109 </Button>
110 </div>
111 </EmptyHeader>
112 </Empty>
113 </SidebarInset>
114 );
115}
116
117export const Route = createFileRoute("/_chat/")({
118 component: ChatIndexRouteView,
119});
120
121function HostedStaticOnboardingState() {
122 const cloudEnabled = hasCloudPublicConfig();
123 const localEnvironmentOff = isLocalEnvironmentDisabled();
124 const description = localEnvironmentOff
125 ? "The local environment is turned off. Connect a remote environment, or turn the local environment back on in Connections."
126 : cloudEnabled
127 ? "Enable T3 Connect on that machine, then open Connections here to sign in with the same account. You can also add the machine using a pairing link."
128 : "Open Connections and add that machine using its pairing link. This app must be able to reach it.";
129
130 return (
131 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none">
132 <div className="flex min-h-0 min-w-0 flex-1 flex-col overflow-x-hidden bg-background">
133 <WorkspacePageHeader electron={isElectron} className="border-b border-border">
134 <div className="flex items-center gap-2">
135 <span className="text-sm font-medium text-foreground md:text-muted-foreground/60">
136 {APP_DISPLAY_NAME}
137 </span>
138 </div>
139 </WorkspacePageHeader>
140
141 <Empty className="flex-1">
142 <div className="w-full max-w-xl rounded-3xl border border-border/55 bg-card/20 px-8 py-12 shadow-sm/5">
143 <EmptyHeader className="max-w-none">
144 <div className="mx-auto mb-5 flex size-11 items-center justify-center rounded-xl border border-border/70 bg-background/70 text-muted-foreground">
145 <LinkIcon className="size-5" />
146 </div>
147 <EmptyTitle>Connect to a computer running T3 Code</EmptyTitle>
148 <EmptyDescription>
149 This app connects to T3 Code running on your computer or a server. Start the T3 Code
150 desktop app or command-line server on that machine and keep it running.
151 </EmptyDescription>
152 <EmptyDescription>{description}</EmptyDescription>
153 <div className="mt-6 flex justify-center">
154 <Button render={<Link to="/settings/connections" />} size="sm">
155 <PlusIcon className="size-4" />
156 Open Connections
157 </Button>
158 </div>
159 </EmptyHeader>
160 </div>
161 </Empty>
162 </div>
163 </SidebarInset>
164 );
165}