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

ThreadWorkspaceDialog.tsx

236 lines8.1 KB

1import { useAtomValue } from "@effect/atom-react";
2import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
3import { scopeProjectRef } from "@t3tools/client-runtime/environment";
4import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
5import type { ProjectId, ScopedThreadRef } from "@t3tools/contracts";
6import { T, useGT } from "gt-react";
7import { FolderXIcon } from "lucide-react";
8import { useState } from "react";
9​
10import { m } from "../../i18n/messages";
11import { useProject, useProjects, useThreadShell } from "../../state/entities";
12import { projectEnvironment } from "../../state/projects";
13import {
14 closeThreadWorkspaceDialog,
15 openThreadWorkspaceDialog,
16 threadWorkspaceDialogAtom,
17 type ThreadWorkspaceDialogRequest,
18} from "../../state/threadWorkspace";
19import { useAtomCommand } from "../../state/use-atom-command";
20import { Button } from "../ui/button";
21import {
22 Dialog,
23 DialogDescription,
24 DialogFooter,
25 DialogHeader,
26 DialogPanel,
27 DialogPopup,
28 DialogTitle,
29} from "../ui/dialog";
30import { Radio, RadioGroup } from "../ui/radio-group";
31import { toastManager } from "../ui/toast";
32import { Alert, AlertDescription, AlertTitle } from "../ui/alert";
33​
34export function MissingWorkspaceWarning({
35 thread,
36 path,
37 changeProject,
38 canMove,
39 checking,
40 onCheck,
41}: {
42 readonly thread: ScopedThreadRef;
43 readonly path: string;
44 readonly changeProject: boolean;
45 readonly canMove: boolean;
46 readonly checking: boolean;
47 readonly onCheck: () => void;
48}) {
49 const gt = useGT();
50 return (
51 <div className="chat-content-lane py-3">
52 <Alert variant="warning" size="compact" controlAlignment="first-line">
53 <FolderXIcon />
54 <AlertTitle>
55 <T>Workspace folder missing</T>
56 </AlertTitle>
57 <AlertDescription>
58 <code className="break-all">{path}</code>
59 <div className="flex flex-wrap gap-1.5">
60 {canMove ? (
61 <>
62 <Button
63 size="xs"
64 onClick={() =>
65 openThreadWorkspaceDialog(
66 thread,
67 changeProject ? "project-directory" : "directory",
68 )
69 }
70 >
71 {changeProject ? <T>Change project folder</T> : <T>Choose folder</T>}
72 </Button>
73 <Button
74 size="xs"
75 variant="outline"
76 onClick={() => openThreadWorkspaceDialog(thread, "scratch")}
77 >
78 <T>Continue without project</T>
79 </Button>
80 </>
81 ) : null}
82 <Button size="xs" variant="ghost" disabled={checking} onClick={onCheck}>
83 {checking ? gt`Checking…` : gt`Check again`}
84 </Button>
85 </div>
86 </AlertDescription>
87 </Alert>
88 </div>
89 );
91​
92export function ThreadWorkspaceDialogHost() {
93 const request = useAtomValue(threadWorkspaceDialogAtom);
94 return request === null ? null : (
95 <ThreadWorkspaceDialog key={request.thread.threadId + request.mode} request={request} />
96 );
98​
99function ThreadWorkspaceDialog({ request }: { readonly request: ThreadWorkspaceDialogRequest }) {
100 const { thread, mode } = request;
101 const gt = useGT();
102 const shell = useThreadShell(thread);
103 const current = useProject(shell ? scopeProjectRef(thread.environmentId, shell.projectId) : null);
104 const projects = useProjects().filter(
105 (project) =>
106 project.environmentId === thread.environmentId &&
107 !project.archivedAt &&
108 !project.isScratch &&
109 (project.id !== shell?.projectId || shell.worktreePath !== null),
110 );
111 const move = useAtomCommand(projectEnvironment.moveThreadWorkspace, { reportFailure: false });
112 const [selection, setSelection] = useState<ProjectId | "scratch" | null>(null);
113 const [pending, setPending] = useState(false);
114 const [error, setError] = useState<string | null>(null);
115 const source = shell?.worktreePath ?? current?.workspaceRoot;
116 const close = () => closeThreadWorkspaceDialog(request);
117 const submit = async () => {
118 const target =
119 mode === "scratch" || selection === "scratch"
120 ? ({ type: "scratch" } as const)
121 : selection === null
122 ? null
123 : ({ type: "project", projectId: selection } as const);
124 if (!target || pending) return;
125 setPending(true);
126 setError(null);
127 const result = await move({
128 environmentId: thread.environmentId,
129 input: { threadId: thread.threadId, target },
130 });
131 setPending(false);
132 if (result._tag === "Failure") {
133 setError(
134 errorDisplayMessage(squashAtomCommandFailure(result), m) ??
135 gt`Could not change the workspace.`,
136 );
137 return;
138 }
139 toastManager.add({
140 type: "success",
141 title: gt`Thread workspace updated`,
142 });
143 close();
144 };
145​
146 return (
147 <Dialog
148 open
149 onOpenChange={(open) => {
150 if (!open && !pending) close();
151 }}
152 >
153 <DialogPopup showCloseButton={!pending}>
154 <DialogHeader>
155 <DialogTitle>
156 {mode === "scratch" ? <T>Continue without project</T> : <T>Move thread to project</T>}
157 </DialogTitle>
158 <DialogDescription>
159 {mode === "scratch" ? (
160 <T>This thread will continue in an empty folder. Its conversation stays here.</T>
161 ) : (
162 <T>The conversation stays with the thread. Files stay in their current folder.</T>
163 )}
164 </DialogDescription>
165 </DialogHeader>
166 <DialogPanel>
167 <form
168 id="thread-workspace-form"
169 onSubmit={(event) => {
170 event.preventDefault();
171 void submit();
172 }}
173 className="space-y-4"
174 >
175 {mode === "project" && source ? (
176 <div className="space-y-1 text-sm">
177 <div className="font-medium">
178 <T>Current folder</T>
179 </div>
180 <code className="block break-all text-xs text-muted-foreground">{source}</code>
181 </div>
182 ) : null}
183 {mode === "project" ? (
184 <RadioGroup
185 aria-label={gt`Destination project`}
186 value={selection ?? ""}
187 onValueChange={(value) => setSelection(value as ProjectId | "scratch")}
188 disabled={pending}
189 >
190 {projects.map((project) => (
191 <label
192 key={project.id}
193 className="flex cursor-pointer items-center gap-3 rounded-lg border border-border p-3"
194 >
195 <Radio value={project.id} />
196 <span className="min-w-0">
197 <span className="block text-sm font-medium">{project.title}</span>
198 <span className="block break-all font-mono text-xs text-muted-foreground">
199 {project.workspaceRoot}
200 </span>
201 </span>
202 </label>
203 ))}
204 {!current?.isScratch ? (
205 <label className="flex cursor-pointer items-center gap-3 rounded-lg border border-border p-3">
206 <Radio value="scratch" />
207 <span className="text-sm font-medium">
208 <T>No project</T>
209 </span>
210 </label>
211 ) : null}
212 </RadioGroup>
213 ) : null}
214 {error ? (
215 <p role="alert" className="text-sm text-destructive">
216 {error}
217 </p>
218 ) : null}
219 </form>
220 </DialogPanel>
221 <DialogFooter>
222 <Button variant="outline" onClick={close} disabled={pending}>
223 <T>Cancel</T>
224 </Button>
225 <Button
226 type="submit"
227 form="thread-workspace-form"
228 disabled={pending || (mode === "project" && selection === null)}
229 >
230 {pending ? <T>Saving…</T> : mode === "scratch" ? <T>Continue</T> : <T>Move thread</T>}
231 </Button>
232 </DialogFooter>
233 </DialogPopup>
234 </Dialog>
235 );