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