Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

_chat.index.tsx

185 lines7.0 KB

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