Open History to see the last commit
1import type {
2 DeviceServiceState,
3 EnvironmentId,
4 MessageId,
5 ScopedThreadRef,
6 ThreadId,
7} from "@t3tools/contracts";
8import { useAtomValue } from "@effect/atom-react";
9import { Atom } from "effect/reactivity";
10import { useEffect } from "react";
11
12import { appAtomRegistry } from "~/rpc/atomRegistry";
13import { useRightPanelStore } from "~/rightPanelStore";
14import { DeviceSetup } from "../device/DeviceSetup";
15import { PullRequestThreadDialog } from "../PullRequestThreadDialog";
16import {
17 AlertDialog,
18 AlertDialogClose,
19 AlertDialogDescription,
20 AlertDialogFooter,
21 AlertDialogHeader,
22 AlertDialogPopup,
23 AlertDialogTitle,
24} from "../ui/alert-dialog";
25import { Button } from "../ui/button";
26import { Dialog } from "../ui/dialog";
27import { WizardPopup } from "../ui/wizard";
28
29// Atoms isolate overlay renders. Thread keys prevent overlays following thread switches.
30type ThreadOverlayAtom<T> = Atom.Writable<{ readonly threadKey: string; readonly value: T } | null>;
31
32function makeThreadOverlayAtom<T>(label: string): ThreadOverlayAtom<T> {
33 return Atom.make<{ readonly threadKey: string; readonly value: T } | null>(null).pipe(
34 Atom.keepAlive,
35 Atom.withLabel(`chat:${label}`),
36 );
37}
38
39function closeThreadOverlay<T>(atom: ThreadOverlayAtom<T>): void {
40 appAtomRegistry.set(atom, null);
41}
42
43function useThreadOverlay<T>(atom: ThreadOverlayAtom<T>, threadKey: string | null): T | null {
44 const entry = useAtomValue(atom);
45 useEffect(
46 () => () => {
47 if (appAtomRegistry.get(atom)?.threadKey === threadKey) closeThreadOverlay(atom);
48 },
49 [atom, threadKey],
50 );
51 return threadKey !== null && entry?.threadKey === threadKey ? entry.value : null;
52}
53
54const pullRequestThreadDialogAtom = makeThreadOverlayAtom<{
55 readonly initialReference: string | null;
56 readonly key: number;
57}>("pull-request-thread-dialog");
58
59export function openPullRequestThreadDialog(threadKey: string, reference?: string): void {
60 appAtomRegistry.set(pullRequestThreadDialogAtom, {
61 threadKey,
62 value: { initialReference: reference ?? null, key: Date.now() },
63 });
64}
65
66export function PullRequestThreadDialogHost(props: {
67 readonly threadKey: string | null;
68 readonly environmentId: EnvironmentId;
69 readonly threadId: ThreadId;
70 readonly cwd: string | null;
71 readonly onPrepared: (input: {
72 branch: string;
73 worktreePath: string | null;
74 }) => Promise<void> | void;
75}) {
76 const state = useThreadOverlay(pullRequestThreadDialogAtom, props.threadKey);
77 if (state === null) return null;
78 return (
79 <PullRequestThreadDialog
80 key={state.key}
81 open
82 environmentId={props.environmentId}
83 threadId={props.threadId}
84 cwd={props.cwd}
85 initialReference={state.initialReference}
86 onOpenChange={(open) => {
87 if (!open) closeThreadOverlay(pullRequestThreadDialogAtom);
88 }}
89 onPrepared={props.onPrepared}
90 />
91 );
92}
93
94const deviceSetupDialogAtom = makeThreadOverlayAtom<true>("device-setup-dialog");
95
96export function openDeviceSetupDialog(threadKey: string): void {
97 appAtomRegistry.set(deviceSetupDialogAtom, { threadKey, value: true });
98}
99
100export function DeviceSetupDialogHost(props: {
101 readonly threadKey: string | null;
102 readonly threadRef: ScopedThreadRef | null;
103 readonly state: DeviceServiceState;
104}) {
105 const open = useThreadOverlay(deviceSetupDialogAtom, props.threadKey) !== null;
106 const { threadRef } = props;
107 return (
108 <Dialog
109 open={open}
110 onOpenChange={(nextOpen) => {
111 if (!nextOpen) closeThreadOverlay(deviceSetupDialogAtom);
112 }}
113 >
114 <WizardPopup>
115 {threadRef ? (
116 <DeviceSetup
117 environmentId={threadRef.environmentId}
118 state={props.state}
119 onComplete={() => {
120 useRightPanelStore.getState().open(threadRef, "device");
121 closeThreadOverlay(deviceSetupDialogAtom);
122 }}
123 />
124 ) : null}
125 </WizardPopup>
126 </Dialog>
127 );
128}
129
130const branchRestoreConfirmAtom = makeThreadOverlayAtom<true>("branch-restore-confirm");
131
132export function openBranchRestoreConfirm(threadKey: string): void {
133 appAtomRegistry.set(branchRestoreConfirmAtom, { threadKey, value: true });
134}
135
136export function BranchRestoreConfirmHost(props: {
137 readonly threadKey: string | null;
138 readonly threadBranch: string;
139 readonly canWriteSourceControl: boolean;
140 readonly onConfirm: () => void;
141}) {
142 const open = useThreadOverlay(branchRestoreConfirmAtom, props.threadKey) !== null;
143 return (
144 <AlertDialog
145 open={open}
146 onOpenChange={(nextOpen) => {
147 if (!nextOpen) closeThreadOverlay(branchRestoreConfirmAtom);
148 }}
149 >
150 <AlertDialogPopup>
151 <AlertDialogHeader>
152 <AlertDialogTitle>
153 Switch to <code className="font-medium">{props.threadBranch}</code>?
154 </AlertDialogTitle>
155 <AlertDialogDescription>
156 You have uncommitted changes. They'll carry over to the other branch, or block the
157 switch if they conflict.
158 </AlertDialogDescription>
159 </AlertDialogHeader>
160 <AlertDialogFooter>
161 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
162 <Button
163 variant="default"
164 disabled={!props.canWriteSourceControl}
165 onClick={() => {
166 closeThreadOverlay(branchRestoreConfirmAtom);
167 props.onConfirm();
168 }}
169 >
170 Switch branch
171 </Button>
172 </AlertDialogFooter>
173 </AlertDialogPopup>
174 </AlertDialog>
175 );
176}
177
178interface PendingRevert {
179 readonly turnCount: number;
180 readonly messageId: MessageId;
181}
182
183const revertConfirmAtom = makeThreadOverlayAtom<PendingRevert>("revert-confirm");
184
185export function openRevertConfirm(threadKey: string, pending: PendingRevert): void {
186 appAtomRegistry.set(revertConfirmAtom, { threadKey, value: pending });
187}
188
189export function RevertConfirmHost(props: {
190 readonly threadKey: string;
191 readonly onConfirm: (turnCount: number, messageId: MessageId, restoreFiles: boolean) => void;
192}) {
193 const pending = useThreadOverlay(revertConfirmAtom, props.threadKey);
194 const confirm = (restoreFiles: boolean) => {
195 if (pending === null) return;
196 closeThreadOverlay(revertConfirmAtom);
197 props.onConfirm(pending.turnCount, pending.messageId, restoreFiles);
198 };
199 return (
200 <AlertDialog
201 open={pending !== null}
202 onOpenChange={(open) => {
203 if (!open) closeThreadOverlay(revertConfirmAtom);
204 }}
205 >
206 <AlertDialogPopup>
207 <AlertDialogHeader>
208 <AlertDialogTitle>Edit from here?</AlertDialogTitle>
209 <AlertDialogDescription>
210 Rewind chat to before this message. Your prompt and attachments return to the composer.
211 “Revert files too” also undoes your own edits.
212 </AlertDialogDescription>
213 </AlertDialogHeader>
214 <AlertDialogFooter>
215 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
216 <Button variant="destructive-outline" onClick={() => confirm(true)}>
217 Revert files too
218 </Button>
219 <Button onClick={() => confirm(false)}>Revert and keep changes</Button>
220 </AlertDialogFooter>
221 </AlertDialogPopup>
222 </AlertDialog>
223 );
224}