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

_chat.index.tsx

161 lines6.2 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">
94 {isElectron ? <WorkspacePageHeader electron /> : null}
95 <Empty className="flex-1">
96 <EmptyHeader className="max-w-md">
97 <EmptyTitle>Couldn’t start a new thread</EmptyTitle>
98 <EmptyDescription>
99 The project is still available. Try opening the draft again.
100 </EmptyDescription>
101 <div className="mt-5 flex justify-center">
102 <Button size="sm" onClick={onRetry}>
103 <RefreshIcon size="md" />
104 Try again
105 </Button>
106 </div>
107 </EmptyHeader>
108 </Empty>
109 </SidebarInset>
110 );
112​
113export const Route = createFileRoute("/_chat/")({
114 component: ChatIndexRouteView,
115});
116​
117function HostedStaticOnboardingState() {
118 const cloudEnabled = hasCloudPublicConfig();
119 const localEnvironmentOff = isLocalEnvironmentDisabled();
120 const description = localEnvironmentOff
121 ? "The local environment is turned off. Connect a remote environment, or turn the local environment back on in Connections."
122 : cloudEnabled
123 ? "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."
124 : "Open Connections and add that machine using its pairing link. This app must be able to reach it.";
125​
126 return (
127 <SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none">
128 <div className="flex min-h-0 min-w-0 flex-1 flex-col overflow-x-hidden bg-background">
129 <WorkspacePageHeader electron={isElectron} className="border-b border-border">
130 <div className="flex items-center gap-2">
131 <span className="text-sm font-medium text-foreground md:text-muted-foreground/60">
132 {APP_DISPLAY_NAME}
133 </span>
134 </div>
135 </WorkspacePageHeader>
136​
137 <Empty className="flex-1">
138 <div className="w-full max-w-xl rounded-3xl border border-border/55 bg-card/20 px-8 py-12 shadow-sm/5">
139 <EmptyHeader className="max-w-none">
140 <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">
141 <LinkIcon className="size-5" />
142 </div>
143 <EmptyTitle>Connect to a computer running T3 Code</EmptyTitle>
144 <EmptyDescription>
145 This app connects to T3 Code running on your computer or a server. Start the T3 Code
146 desktop app or command-line server on that machine and keep it running.
147 </EmptyDescription>
148 <EmptyDescription>{description}</EmptyDescription>
149 <div className="mt-6 flex justify-center">
150 <Button render={<Link to="/settings/connections" />} size="sm">
151 <PlusIcon className="size-4" />
152 Open Connections
153 </Button>
154 </div>
155 </EmptyHeader>
156 </div>
157 </Empty>
158 </div>
159 </SidebarInset>
160 );