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/**
30 * Open state for overlays the chat view hosts. It lives in atoms rather than
31 * ChatView state so opening or closing one renders only its host, not the
32 * whole chat chrome. Each entry carries the thread it was opened for; a host
33 * shows only its own thread's entry and drops it when the thread changes.
34 */
35type ThreadOverlayAtom<T> = Atom.Writable<{ readonly threadKey: string; readonly value: T } | null>;
36
37function makeThreadOverlayAtom<T>(label: string): ThreadOverlayAtom<T> {
38 return Atom.make<{ readonly threadKey: string; readonly value: T } | null>(null).pipe(
39 Atom.keepAlive,
40 Atom.withLabel(`chat:${label}`),
41 );
42}
43
44function closeThreadOverlay<T>(atom: ThreadOverlayAtom<T>): void {
45 appAtomRegistry.set(atom, null);
46}
47
48function useThreadOverlay<T>(atom: ThreadOverlayAtom<T>, threadKey: string | null): T | null {
49 const entry = useAtomValue(atom);
50 useEffect(
51 () => () => {
52 if (appAtomRegistry.get(atom)?.threadKey === threadKey) closeThreadOverlay(atom);
53 },
54 [atom, threadKey],
55 );
56 return threadKey !== null && entry?.threadKey === threadKey ? entry.value : null;
57}
58
59const pullRequestThreadDialogAtom = makeThreadOverlayAtom<{
60 readonly initialReference: string | null;
61 readonly key: number;
62}>("pull-request-thread-dialog");
63
64export function openPullRequestThreadDialog(threadKey: string, reference?: string): void {
65 appAtomRegistry.set(pullRequestThreadDialogAtom, {
66 threadKey,
67 value: { initialReference: reference ?? null, key: Date.now() },
68 });
69}
70
71export function PullRequestThreadDialogHost(props: {
72 readonly threadKey: string | null;
73 readonly environmentId: EnvironmentId;
74 readonly threadId: ThreadId;
75 readonly cwd: string | null;
76 readonly onPrepared: (input: {
77 branch: string;
78 worktreePath: string | null;
79 }) => Promise<void> | void;
80}) {
81 const state = useThreadOverlay(pullRequestThreadDialogAtom, props.threadKey);
82 if (state === null) return null;
83 return (
84 <PullRequestThreadDialog
85 key={state.key}
86 open
87 environmentId={props.environmentId}
88 threadId={props.threadId}
89 cwd={props.cwd}
90 initialReference={state.initialReference}
91 onOpenChange={(open) => {
92 if (!open) closeThreadOverlay(pullRequestThreadDialogAtom);
93 }}
94 onPrepared={props.onPrepared}
95 />
96 );
97}
98
99const deviceSetupDialogAtom = makeThreadOverlayAtom<true>("device-setup-dialog");
100
101export function openDeviceSetupDialog(threadKey: string): void {
102 appAtomRegistry.set(deviceSetupDialogAtom, { threadKey, value: true });
103}
104
105export function DeviceSetupDialogHost(props: {
106 readonly threadKey: string | null;
107 readonly threadRef: ScopedThreadRef | null;
108 readonly state: DeviceServiceState;
109}) {
110 const open = useThreadOverlay(deviceSetupDialogAtom, props.threadKey) !== null;
111 const { threadRef } = props;
112 return (
113 <Dialog
114 open={open}
115 onOpenChange={(nextOpen) => {
116 if (!nextOpen) closeThreadOverlay(deviceSetupDialogAtom);
117 }}
118 >
119 <WizardPopup>
120 {threadRef ? (
121 <DeviceSetup
122 environmentId={threadRef.environmentId}
123 state={props.state}
124 onComplete={() => {
125 useRightPanelStore.getState().open(threadRef, "device");
126 closeThreadOverlay(deviceSetupDialogAtom);
127 }}
128 />
129 ) : null}
130 </WizardPopup>
131 </Dialog>
132 );
133}
134
135const branchRestoreConfirmAtom = makeThreadOverlayAtom<true>("branch-restore-confirm");
136
137export function openBranchRestoreConfirm(threadKey: string): void {
138 appAtomRegistry.set(branchRestoreConfirmAtom, { threadKey, value: true });
139}
140
141export function BranchRestoreConfirmHost(props: {
142 readonly threadKey: string | null;
143 readonly threadBranch: string;
144 readonly canWriteSourceControl: boolean;
145 readonly onConfirm: () => void;
146}) {
147 const open = useThreadOverlay(branchRestoreConfirmAtom, props.threadKey) !== null;
148 return (
149 <AlertDialog
150 open={open}
151 onOpenChange={(nextOpen) => {
152 if (!nextOpen) closeThreadOverlay(branchRestoreConfirmAtom);
153 }}
154 >
155 <AlertDialogPopup>
156 <AlertDialogHeader>
157 <AlertDialogTitle>
158 Switch to <code className="font-medium">{props.threadBranch}</code>?
159 </AlertDialogTitle>
160 <AlertDialogDescription>
161 You have uncommitted changes. They'll carry over to the other branch, or block the
162 switch if they conflict.
163 </AlertDialogDescription>
164 </AlertDialogHeader>
165 <AlertDialogFooter>
166 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
167 <Button
168 variant="default"
169 disabled={!props.canWriteSourceControl}
170 onClick={() => {
171 closeThreadOverlay(branchRestoreConfirmAtom);
172 props.onConfirm();
173 }}
174 >
175 Switch branch
176 </Button>
177 </AlertDialogFooter>
178 </AlertDialogPopup>
179 </AlertDialog>
180 );
181}
182
183interface PendingRevert {
184 readonly turnCount: number;
185 readonly messageId: MessageId;
186}
187
188const revertConfirmAtom = makeThreadOverlayAtom<PendingRevert>("revert-confirm");
189
190export function openRevertConfirm(threadKey: string, pending: PendingRevert): void {
191 appAtomRegistry.set(revertConfirmAtom, { threadKey, value: pending });
192}
193
194export function RevertConfirmHost(props: {
195 readonly threadKey: string;
196 readonly onConfirm: (turnCount: number, messageId: MessageId, restoreFiles: boolean) => void;
197}) {
198 const pending = useThreadOverlay(revertConfirmAtom, props.threadKey);
199 const confirm = (restoreFiles: boolean) => {
200 if (pending === null) return;
201 closeThreadOverlay(revertConfirmAtom);
202 props.onConfirm(pending.turnCount, pending.messageId, restoreFiles);
203 };
204 return (
205 <AlertDialog
206 open={pending !== null}
207 onOpenChange={(open) => {
208 if (!open) closeThreadOverlay(revertConfirmAtom);
209 }}
210 >
211 <AlertDialogPopup>
212 <AlertDialogHeader>
213 <AlertDialogTitle>Edit from here?</AlertDialogTitle>
214 <AlertDialogDescription>
215 Rewind chat to before this message. Your prompt and attachments return to the composer.
216 </AlertDialogDescription>
217 </AlertDialogHeader>
218 <AlertDialogFooter>
219 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
220 <Button variant="destructive" onClick={() => confirm(true)}>
221 Revert files too
222 </Button>
223 <Button onClick={() => confirm(false)}>Revert and keep changes</Button>
224 </AlertDialogFooter>
225 </AlertDialogPopup>
226 </AlertDialog>
227 );
228}