Open History to see the last commit
1import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2import { createFileRoute, Link } from "@tanstack/react-router";
3import { LinkIcon, PlusIcon, RotateCcwIcon } from "lucide-react";
4import { useCallback, useEffect, useMemo, useRef, useState } from "react";
5
6import { openCommandPalette } from "../commandPaletteBus";
7import { sortScopedProjectsForSidebar } from "../components/Sidebar.logic";
8import { Button } from "../components/ui/button";
9import { Empty, EmptyDescription, EmptyHeader, EmptyTitle } from "../components/ui/empty";
10import { SidebarInset } from "../components/ui/sidebar";
11import { WorkspacePageHeader } from "../components/WorkspacePageHeader";
12import { useNewThreadHandler } from "../hooks/useHandleNewThread";
13import {
14 useAllEnvironmentShellsBootstrapped,
15 useProjects,
16 useThreadShells,
17} from "../state/entities";
18import { useEnvironments } from "../state/environments";
19import { APP_DISPLAY_NAME } from "~/branding";
20import { hasCloudPublicConfig } from "~/cloud/publicConfig";
21
22function ChatIndexRouteView() {
23 const { authGateState } = Route.useRouteContext();
24 const { environments } = useEnvironments();
25
26 if (authGateState.status === "hosted-static" && environments.length === 0) {
27 return <HostedStaticOnboardingState />;
28 }
29
30 return <IndexDraftLanding />;
31}
32
33/**
34 * Landing on the index route drops straight into a draft thread for the most
35 * recently active project, so the first screen is a prompt instead of a dead
36 * end. Falls back to an add-project hero when no project exists yet.
37 */
38function IndexDraftLanding() {
39 const projects = useProjects();
40 const threads = useThreadShells();
41 const bootstrapped = useAllEnvironmentShellsBootstrapped();
42 const handleNewThread = useNewThreadHandler();
43 const startingRef = useRef(false);
44 const [startState, setStartState] = useState({ failed: false, retryRequest: 0 });
45
46 const mostRecentProject = useMemo(
47 () =>
48 bootstrapped
49 ? (sortScopedProjectsForSidebar(projects, threads, "updated_at")[0] ?? null)
50 : null,
51 [bootstrapped, projects, threads],
52 );
53
54 useEffect(() => {
55 if (mostRecentProject === null || startingRef.current) {
56 return;
57 }
58 startingRef.current = true;
59 void handleNewThread(scopeProjectRef(mostRecentProject.environmentId, mostRecentProject.id), {
60 replace: true,
61 }).catch(() => {
62 startingRef.current = false;
63 setStartState((state) => ({ ...state, failed: true }));
64 });
65 }, [handleNewThread, mostRecentProject, startState.retryRequest]);
66
67 if (!bootstrapped) {
68 return null;
69 }
70 if (mostRecentProject !== null) {
71 return startState.failed ? (
72 <DraftStartError
73 onRetry={() => {
74 setStartState((state) => ({
75 failed: false,
76 retryRequest: state.retryRequest + 1,
77 }));
78 }}
79 />
80 ) : null;
81 }
82 return <NoProjectsHero />;
83}
84
85function DraftStartError({ onRetry }: { readonly onRetry: () => void }) {
86 return (
87 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground">
88 <Empty className="flex-1">
89 <EmptyHeader className="max-w-md">
90 <EmptyTitle className="text-foreground text-xl">Couldn’t start a new thread</EmptyTitle>
91 <EmptyDescription className="mt-2 text-sm text-muted-foreground/78">
92 The project is still available. Try opening the draft again.
93 </EmptyDescription>
94 <div className="mt-5 flex justify-center">
95 <Button size="sm" onClick={onRetry}>
96 <RotateCcwIcon className="size-4" />
97 Try again
98 </Button>
99 </div>
100 </EmptyHeader>
101 </Empty>
102 </SidebarInset>
103 );
104}
105
106function NoProjectsHero() {
107 const openAddProject = useCallback(() => openCommandPalette({ open: "add-project" }), []);
108
109 return (
110 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground">
111 <div className="flex min-h-0 min-w-0 flex-1 flex-col overflow-x-hidden bg-background">
112 <Empty className="flex-1">
113 <div className="w-full max-w-lg px-8 py-12">
114 <EmptyHeader className="max-w-none">
115 <EmptyTitle className="text-foreground text-2xl sm:text-3xl">
116 What should we work on?
117 </EmptyTitle>
118 <EmptyDescription className="mt-2 text-sm text-muted-foreground/78">
119 Add a project to start your first thread.
120 </EmptyDescription>
121 <div className="mt-6 flex justify-center">
122 <Button size="sm" onClick={openAddProject}>
123 <PlusIcon className="size-4" />
124 Add project
125 </Button>
126 </div>
127 </EmptyHeader>
128 </div>
129 </Empty>
130 </div>
131 </SidebarInset>
132 );
133}
134
135export const Route = createFileRoute("/_chat/")({
136 component: ChatIndexRouteView,
137});
138
139function HostedStaticOnboardingState() {
140 const cloudEnabled = hasCloudPublicConfig();
141
142 return (
143 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground">
144 <div className="flex min-h-0 min-w-0 flex-1 flex-col overflow-x-hidden bg-background">
145 <WorkspacePageHeader className="border-b border-border">
146 <div className="flex items-center gap-2">
147 <span className="text-sm font-medium text-foreground md:text-muted-foreground/60">
148 {APP_DISPLAY_NAME}
149 </span>
150 </div>
151 </WorkspacePageHeader>
152
153 <Empty className="flex-1">
154 <div className="w-full max-w-xl rounded-3xl border border-border/55 bg-card/20 px-8 py-12 shadow-sm/5">
155 <EmptyHeader className="max-w-none">
156 <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">
157 <LinkIcon className="size-5" />
158 </div>
159 <EmptyTitle className="text-foreground text-xl">
160 Connect an environment to get started
161 </EmptyTitle>
162 <EmptyDescription className="mt-2 text-sm leading-relaxed text-muted-foreground/78">
163 {cloudEnabled
164 ? "Sign in to T3 Connect to connect a linked environment through its managed tunnel, or add a reachable backend manually."
165 : "Add a reachable backend manually to start working from this browser."}
166 </EmptyDescription>
167 <div className="mt-6 flex justify-center">
168 <Button render={<Link to="/settings/connections" />} size="sm">
169 <PlusIcon className="size-4" />
170 {cloudEnabled ? "Open Connections" : "Add environment"}
171 </Button>
172 </div>
173 </EmptyHeader>
174 </div>
175 </Empty>
176 </div>
177 </SidebarInset>
178 );
179}