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, useMemo, 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 const startingRef = useRef(false);
48 const [startState, setStartState] = useState({ failed: false, retryRequest: 0 });
49
50 const mostRecentProject = useMemo(
51 () =>
52 bootstrapped
53 ? (sortScopedProjectsForSidebar(projects, threads, "updated_at")[0] ?? null)
54 : null,
55 [bootstrapped, projects, threads],
56 );
57
58 useEffect(() => {
59 // Starting a draft after the user has left would replace the page they picked.
60 if (mostRecentProject === null || startingRef.current || !isStillOnIndex()) {
61 return;
62 }
63 const projectRef = scopeProjectRef(mostRecentProject.environmentId, mostRecentProject.id);
64 if (readProject(projectRef) === null) {
65 return;
66 }
67 startingRef.current = true;
68 void handleNewThread(projectRef, { replace: true })
69 .then((opened) => {
70 if (opened === null) {
71 startingRef.current = false;
72 setStartState((state) => ({ ...state, retryRequest: state.retryRequest + 1 }));
73 }
74 })
75 .catch(() => {
76 startingRef.current = false;
77 setStartState((state) => ({ ...state, failed: true }));
78 });
79 }, [handleNewThread, mostRecentProject, startState.retryRequest]);
80
81 if (!bootstrapped) {
82 return null;
83 }
84 if (mostRecentProject !== null) {
85 return startState.failed ? (
86 <DraftStartError
87 onRetry={() => {
88 setStartState((state) => ({
89 failed: false,
90 retryRequest: state.retryRequest + 1,
91 }));
92 }}
93 />
94 ) : null;
95 }
96 return <NoProjectsHero />;
97}
98
99function DraftStartError({ onRetry }: { readonly onRetry: () => void }) {
100 return (
101 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none">
102 {isElectron ? <WorkspacePageHeader electron /> : null}
103 <Empty className="flex-1">
104 <EmptyHeader className="max-w-md">
105 <EmptyTitle>Couldn’t start a new thread</EmptyTitle>
106 <EmptyDescription>
107 The project is still available. Try opening the draft again.
108 </EmptyDescription>
109 <div className="mt-5 flex justify-center">
110 <Button size="sm" onClick={onRetry}>
111 <RefreshIcon size="md" />
112 Try again
113 </Button>
114 </div>
115 </EmptyHeader>
116 </Empty>
117 </SidebarInset>
118 );
119}
120
121export const Route = createFileRoute("/_chat/")({
122 component: ChatIndexRouteView,
123});
124
125function HostedStaticOnboardingState() {
126 const cloudEnabled = hasCloudPublicConfig();
127 const localEnvironmentOff = isLocalEnvironmentDisabled();
128 const description = localEnvironmentOff
129 ? "The local environment is turned off. Connect a remote environment, or turn the local environment back on in Connections."
130 : cloudEnabled
131 ? "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."
132 : "Open Connections and add that machine using its pairing link. This app must be able to reach it.";
133
134 return (
135 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none">
136 <div className="flex min-h-0 min-w-0 flex-1 flex-col overflow-x-hidden bg-background">
137 <WorkspacePageHeader electron={isElectron} className="border-b border-border">
138 <div className="flex items-center gap-2">
139 <span className="text-sm font-medium text-foreground md:text-muted-foreground/60">
140 {APP_DISPLAY_NAME}
141 </span>
142 </div>
143 </WorkspacePageHeader>
144
145 <Empty className="flex-1">
146 <div className="w-full max-w-xl rounded-3xl border border-border/55 bg-card/20 px-8 py-12 shadow-sm/5">
147 <EmptyHeader className="max-w-none">
148 <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">
149 <LinkIcon className="size-5" />
150 </div>
151 <EmptyTitle>Connect to a computer running T3 Code</EmptyTitle>
152 <EmptyDescription>
153 This app connects to T3 Code running on your computer or a server. Start the T3 Code
154 desktop app or command-line server on that machine and keep it running.
155 </EmptyDescription>
156 <EmptyDescription>{description}</EmptyDescription>
157 <div className="mt-6 flex justify-center">
158 <Button render={<Link to="/settings/connections" />} size="sm">
159 <PlusIcon className="size-4" />
160 Open Connections
161 </Button>
162 </div>
163 </EmptyHeader>
164 </div>
165 </Empty>
166 </div>
167 </SidebarInset>
168 );
169}