Open History to see the last commit
1import type {
2 DeviceServiceState,
3 EnvironmentId,
4 MessageId,
5 OrchestrationV2ThreadProjection,
6 ScopedThreadRef,
7 ThreadId,
8} from "@t3tools/contracts";
9import { rewindFileRestore } from "@t3tools/client-runtime/state/thread-repositories";
10import { useAtomValue } from "@effect/atom-react";
11import { T, Var } from "gt-react";
12import { Atom } from "effect/reactivity";
13
14import { appAtomRegistry } from "~/rpc/atomRegistry";
15import { useRightPanelStore } from "~/rightPanelStore";
16import { DeviceSetup } from "../device/DeviceSetup";
17import { PullRequestThreadDialog } from "../PullRequestThreadDialog";
18import {
19 AlertDialog,
20 AlertDialogClose,
21 AlertDialogDescription,
22 AlertDialogFooter,
23 AlertDialogHeader,
24 AlertDialogPopup,
25 AlertDialogTitle,
26} from "../ui/alert-dialog";
27import { Button } from "../ui/button";
28import { Dialog } from "../ui/dialog";
29import { WizardPopup } from "../ui/wizard";
30import { findRevertCheckpoint } from "../../session-logic";
31import { filesystemEnvironment } from "../../state/filesystem";
32import { useEnvironmentQuery } from "../../state/query";
33import { registerThreadOverlay } from "./threadOverlayRetention";
34
35/**
36 * Open state for overlays the chat view hosts. It lives in atoms rather than
37 * ChatView state so opening or closing one renders only its host, not the
38 * whole chat chrome. Each entry carries the thread it was opened for; a host
39 * shows only its own thread's entry, and leaving that thread's route drops it.
40 */
41type ThreadOverlayAtom<T> = Atom.Writable<{ readonly threadKey: string; readonly value: T } | null>;
42
43function makeThreadOverlayAtom<T>(label: string): ThreadOverlayAtom<T> {
44 const atom = Atom.make<{ readonly threadKey: string; readonly value: T } | null>(null).pipe(
45 Atom.keepAlive,
46 Atom.withLabel(`chat:${label}`),
47 );
48 registerThreadOverlay((threadKey) => {
49 const entry = appAtomRegistry.get(atom);
50 if (entry !== null && entry.threadKey !== threadKey) closeThreadOverlay(atom);
51 });
52 return atom;
53}
54
55function closeThreadOverlay<T>(atom: ThreadOverlayAtom<T>): void {
56 appAtomRegistry.set(atom, null);
57}
58
59function useThreadOverlay<T>(atom: ThreadOverlayAtom<T>, threadKey: string | null): T | null {
60 const entry = useAtomValue(atom);
61 return threadKey !== null && entry?.threadKey === threadKey ? entry.value : null;
62}
63
64const pullRequestThreadDialogAtom = makeThreadOverlayAtom<{
65 readonly initialReference: string | null;
66 readonly key: number;
67}>("pull-request-thread-dialog");
68
69export function openPullRequestThreadDialog(threadKey: string, reference?: string): void {
70 appAtomRegistry.set(pullRequestThreadDialogAtom, {
71 threadKey,
72 value: { initialReference: reference ?? null, key: Date.now() },
73 });
74}
75
76export function PullRequestThreadDialogHost(props: {
77 readonly threadKey: string | null;
78 readonly environmentId: EnvironmentId;
79 readonly threadId: ThreadId;
80 readonly cwd: string | null;
81 readonly onPrepared: (input: {
82 branch: string;
83 worktreePath: string | null;
84 pullRequestUrl: string;
85 }) => Promise<void> | void;
86}) {
87 const state = useThreadOverlay(pullRequestThreadDialogAtom, props.threadKey);
88 if (state === null) return null;
89 return (
90 <PullRequestThreadDialog
91 key={state.key}
92 open
93 environmentId={props.environmentId}
94 threadId={props.threadId}
95 cwd={props.cwd}
96 initialReference={state.initialReference}
97 onOpenChange={(open) => {
98 if (!open) closeThreadOverlay(pullRequestThreadDialogAtom);
99 }}
100 onPrepared={props.onPrepared}
101 />
102 );
103}
104
105const deviceSetupDialogAtom = makeThreadOverlayAtom<true>("device-setup-dialog");
106
107export function openDeviceSetupDialog(threadKey: string): void {
108 appAtomRegistry.set(deviceSetupDialogAtom, { threadKey, value: true });
109}
110
111export function DeviceSetupDialogHost(props: {
112 readonly threadKey: string | null;
113 readonly threadRef: ScopedThreadRef | null;
114 readonly state: DeviceServiceState;
115}) {
116 const open = useThreadOverlay(deviceSetupDialogAtom, props.threadKey) !== null;
117 const { threadRef } = props;
118 return (
119 <Dialog
120 open={open}
121 onOpenChange={(nextOpen) => {
122 if (!nextOpen) closeThreadOverlay(deviceSetupDialogAtom);
123 }}
124 >
125 <WizardPopup>
126 {threadRef ? (
127 <DeviceSetup
128 environmentId={threadRef.environmentId}
129 state={props.state}
130 onComplete={() => {
131 useRightPanelStore.getState().open(threadRef, "device");
132 closeThreadOverlay(deviceSetupDialogAtom);
133 }}
134 />
135 ) : null}
136 </WizardPopup>
137 </Dialog>
138 );
139}
140
141const branchRestoreConfirmAtom = makeThreadOverlayAtom<true>("branch-restore-confirm");
142
143export function openBranchRestoreConfirm(threadKey: string): void {
144 appAtomRegistry.set(branchRestoreConfirmAtom, { threadKey, value: true });
145}
146
147export function BranchRestoreConfirmHost(props: {
148 readonly threadKey: string | null;
149 readonly threadBranch: string;
150 readonly canWriteSourceControl: boolean;
151 readonly onConfirm: () => void;
152}) {
153 const open = useThreadOverlay(branchRestoreConfirmAtom, props.threadKey) !== null;
154 return (
155 <AlertDialog
156 open={open}
157 onOpenChange={(nextOpen) => {
158 if (!nextOpen) closeThreadOverlay(branchRestoreConfirmAtom);
159 }}
160 >
161 <AlertDialogPopup>
162 <AlertDialogHeader>
163 <AlertDialogTitle>
164 <T>
165 Switch to{" "}
166 <code className="font-medium">
167 <Var>{props.threadBranch}</Var>
168 </code>
169 ?
170 </T>
171 </AlertDialogTitle>
172 <AlertDialogDescription>
173 <T>
174 You have uncommitted changes. They'll carry over to the other branch, or block the
175 switch if they conflict.
176 </T>
177 </AlertDialogDescription>
178 </AlertDialogHeader>
179 <AlertDialogFooter>
180 <AlertDialogClose render={<Button variant="outline" />}>
181 <T>Cancel</T>
182 </AlertDialogClose>
183 <Button
184 variant="default"
185 disabled={!props.canWriteSourceControl}
186 onClick={() => {
187 closeThreadOverlay(branchRestoreConfirmAtom);
188 props.onConfirm();
189 }}
190 >
191 <T>Switch branch</T>
192 </Button>
193 </AlertDialogFooter>
194 </AlertDialogPopup>
195 </AlertDialog>
196 );
197}
198
199interface PendingRevert {
200 readonly turnCount: number;
201 readonly messageId: MessageId;
202}
203
204const revertConfirmAtom = makeThreadOverlayAtom<PendingRevert>("revert-confirm");
205
206export function openRevertConfirm(threadKey: string, pending: PendingRevert): void {
207 appAtomRegistry.set(revertConfirmAtom, { threadKey, value: pending });
208}
209
210export function RevertConfirmHost(props: {
211 readonly threadKey: string;
212 readonly environmentId: EnvironmentId;
213 /** The thread's worktree, which may be a task folder. */
214 readonly taskFolderPath: string | null;
215 readonly projection: Pick<
216 OrchestrationV2ThreadProjection,
217 "checkpoints" | "checkpointScopes"
218 > | null;
219 readonly worktreePath: string | null;
220 readonly onConfirm: (turnCount: number, messageId: MessageId, restoreFiles: boolean) => void;
221}) {
222 const pending = useThreadOverlay(revertConfirmAtom, props.threadKey);
223 const checkpoint =
224 pending === null ? undefined : findRevertCheckpoint(props.projection, pending.turnCount);
225 // Only a missing snapshot depends on the task folder: its repositories may still restore.
226 const taskFolderQuery = useEnvironmentQuery(
227 checkpoint?.status === "missing" && props.taskFolderPath !== null
228 ? filesystemEnvironment.taskFolder({
229 environmentId: props.environmentId,
230 input: { worktreePath: props.taskFolderPath },
231 })
232 : null,
233 );
234 const fileRestore =
235 // The server refuses file restore outside an isolated worktree.
236 props.projection === null || props.worktreePath === null
237 ? null
238 : rewindFileRestore({
239 projection: props.projection,
240 checkpoint,
241 taskFolder: taskFolderQuery.data?.taskFolder,
242 });
243 const confirm = (restoreFiles: boolean) => {
244 if (pending === null || (restoreFiles && fileRestore === null)) return;
245 closeThreadOverlay(revertConfirmAtom);
246 props.onConfirm(pending.turnCount, pending.messageId, restoreFiles);
247 };
248 return (
249 <AlertDialog
250 open={pending !== null}
251 onOpenChange={(open) => {
252 if (!open) closeThreadOverlay(revertConfirmAtom);
253 }}
254 >
255 <AlertDialogPopup>
256 <AlertDialogHeader>
257 <AlertDialogTitle>
258 <T>Edit from here?</T>
259 </AlertDialogTitle>
260 <AlertDialogDescription>
261 {fileRestore === "repositories" ? (
262 <T>
263 Rewind chat to before this message. Your prompt and attachments return to the
264 composer. “Revert repositories too” also undoes your own edits in this thread’s
265 repository worktrees. Files in the project folder itself stay as they are.
266 </T>
267 ) : (
268 <T>
269 Rewind chat to before this message. Your prompt and attachments return to the
270 composer. “Revert files too” also undoes your own edits.
271 </T>
272 )}
273 </AlertDialogDescription>
274 </AlertDialogHeader>
275 <AlertDialogFooter>
276 <AlertDialogClose render={<Button variant="outline" />}>
277 <T>Cancel</T>
278 </AlertDialogClose>
279 <Button
280 variant="destructive-outline"
281 disabled={fileRestore === null}
282 onClick={() => confirm(true)}
283 >
284 {fileRestore === "repositories" ? (
285 <T>Revert repositories too</T>
286 ) : (
287 <T>Revert files too</T>
288 )}
289 </Button>
290 <Button onClick={() => confirm(false)}>
291 <T>Revert and keep changes</T>
292 </Button>
293 </AlertDialogFooter>
294 </AlertDialogPopup>
295 </AlertDialog>
296 );
297}