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

_chat.index.tsx

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