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

_chat.index.tsx

164 lines6.6 KB

1import { RefreshIcon } from "~/components/ui/refresh-icon";
2import { scopeProjectRef } from "@t3tools/client-runtime/environment";
3import { createFileRoute, Link } from "@tanstack/react-router";
4import { LinkIcon, PlusIcon } from "lucide-react";
5import { useEffect, 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 useAllEnvironmentShellsBootstrapped,
18 useProjects,
19 useThreadShells,
20} from "../state/entities";
21import { useEnvironments } from "../state/environments";
22import { APP_DISPLAY_NAME } from "~/branding";
23import { hasCloudPublicConfig } from "~/cloud/publicConfig";
24​
25function 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 <HostedStaticOnboardingState />;
32 }
33​
34 return <IndexDraftLanding />;
36​
37/**
38 * Landing on the index route drops straight into a draft thread for the most
39 * recently active project, so the first screen is a prompt instead of a dead
40 * end. Falls back to an add-project hero when no project exists yet.
41 */
42function IndexDraftLanding() {
43 const projects = useProjects();
44 const threads = useThreadShells();
45 const bootstrapped = useAllEnvironmentShellsBootstrapped();
46 const handleNewThread = useNewThreadHandler();
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 if (mostRecentProject === null || startingRef.current) {
60 return;
61 }
62 startingRef.current = true;
63 void handleNewThread(scopeProjectRef(mostRecentProject.environmentId, mostRecentProject.id), {
64 replace: true,
65 }).catch(() => {
66 startingRef.current = false;
67 setStartState((state) => ({ ...state, failed: true }));
68 });
69 }, [handleNewThread, mostRecentProject, startState.retryRequest]);
70​
71 if (!bootstrapped) {
72 return null;
73 }
74 if (mostRecentProject !== null) {
75 return startState.failed ? (
76 <DraftStartError
77 onRetry={() => {
78 setStartState((state) => ({
79 failed: false,
80 retryRequest: state.retryRequest + 1,
81 }));
82 }}
83 />
84 ) : null;
85 }
86 // First-run routing to the welcome wizard happens in FirstRunGate at the
87 // root, before this route ever renders.
88 return <NoProjectsHero />;
90​
91function DraftStartError({ onRetry }: { readonly onRetry: () => void }) {
92 return (
93 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground">
94 <Empty className="flex-1">
95 <EmptyHeader className="max-w-md">
96 <EmptyTitle className="text-foreground text-xl">Couldn’t start a new thread</EmptyTitle>
97 <EmptyDescription className="mt-2 text-sm text-muted-foreground/78">
98 The project is still available. Try opening the draft again.
99 </EmptyDescription>
100 <div className="mt-5 flex justify-center">
101 <Button size="sm" onClick={onRetry}>
102 <RefreshIcon className="size-4" />
103 Try again
104 </Button>
105 </div>
106 </EmptyHeader>
107 </Empty>
108 </SidebarInset>
109 );
111​
112export const Route = createFileRoute("/_chat/")({
113 component: ChatIndexRouteView,
114});
115​
116function HostedStaticOnboardingState() {
117 const cloudEnabled = hasCloudPublicConfig();
118 const localEnvironmentOff = isLocalEnvironmentDisabled();
119 const description = localEnvironmentOff
120 ? "The local environment is turned off. Connect a remote environment, or turn the local environment back on in Connections."
121 : cloudEnabled
122 ? "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."
123 : "Open Connections and add that machine using its pairing link. This app must be able to reach it.";
124​
125 return (
126 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground">
127 <div className="flex min-h-0 min-w-0 flex-1 flex-col overflow-x-hidden bg-background">
128 <WorkspacePageHeader electron={isElectron} className="border-b border-border">
129 <div className="flex items-center gap-2">
130 <span className="text-sm font-medium text-foreground md:text-muted-foreground/60">
131 {APP_DISPLAY_NAME}
132 </span>
133 </div>
134 </WorkspacePageHeader>
135​
136 <Empty className="flex-1">
137 <div className="w-full max-w-xl rounded-3xl border border-border/55 bg-card/20 px-8 py-12 shadow-sm/5">
138 <EmptyHeader className="max-w-none">
139 <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">
140 <LinkIcon className="size-5" />
141 </div>
142 <EmptyTitle className="text-foreground text-xl">
143 Connect to a computer running T3 Code
144 </EmptyTitle>
145 <EmptyDescription className="mt-2 text-sm leading-relaxed text-muted-foreground/78">
146 This app connects to T3 Code running on your computer or a server. Start the T3 Code
147 desktop app or command-line server on that machine and keep it running.
148 </EmptyDescription>
149 <EmptyDescription className="mt-2 text-sm leading-relaxed text-muted-foreground/78">
150 {description}
151 </EmptyDescription>
152 <div className="mt-6 flex justify-center">
153 <Button render={<Link to="/settings/connections" />} size="sm">
154 <PlusIcon className="size-4" />
155 Open Connections
156 </Button>
157 </div>
158 </EmptyHeader>
159 </div>
160 </Empty>
161 </div>
162 </SidebarInset>
163 );