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

ChatOverlayHosts.tsx

228 lines7.4 KB

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