The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** @jsxRuntime automatic */ |
| 2 | /** @jsxImportSource @alchemy.run/sigil */ |
| 3 | import { useMemo, useState } from "@alchemy.run/sigil/react"; |
| 4 | import type { JSX } from "react"; |
| 5 | import { Screen, type ScreenController } from "../../../Interaction.ts"; |
| 6 | import type { Plan } from "../../../Plan.ts"; |
| 7 | import { PromptFrame, ChoiceGroup, useKeyGlyphs, useTerminalInput } from "../ui/index.ts"; |
| 8 | import type { PlanTreeData } from "./PlanTree.ts"; |
| 9 | import { Plan as PlanComponent, PlanTree } from "./PlanView.tsx"; |
| 10 | |
| 11 | export interface PlanDecisionChoice<Value> { |
| 12 | readonly value: Value; |
| 13 | readonly label: string; |
| 14 | } |
| 15 | |
| 16 | function PlanDecision<Value>(props: { |
| 17 | readonly plan: Plan | PlanTreeData; |
| 18 | readonly label?: string; |
| 19 | readonly message: string; |
| 20 | readonly choices: ReadonlyArray<PlanDecisionChoice<Value>>; |
| 21 | readonly initialValue: Value; |
| 22 | readonly controller: ScreenController<Value>; |
| 23 | }): JSX.Element { |
| 24 | const { plan, label = "Plan", message, choices, initialValue, controller } = props; |
| 25 | const tree = useMemo(() => new PlanTree(plan, { mode: "review", label }), [plan, label]); |
| 26 | const [selected, setSelected] = useState(initialValue); |
| 27 | const keys = useKeyGlyphs(); |
| 28 | |
| 29 | useTerminalInput((_input, key) => { |
| 30 | if (key.enter) controller.submit(selected); |
| 31 | else if (key.escape) controller.cancel(); |
| 32 | }); |
| 33 | |
| 34 | return ( |
| 35 | <PlanComponent |
| 36 | tree={tree} |
| 37 | footer={ |
| 38 | <PromptFrame |
| 39 | message={message} |
| 40 | keys={[ |
| 41 | [keys.upDown, "scroll plan"], |
| 42 | [keys.leftRight, "choose"], |
| 43 | [keys.enter, "confirm"], |
| 44 | [keys.escape, "cancel"], |
| 45 | ]} |
| 46 | > |
| 47 | <ChoiceGroup choices={choices} value={selected} onChange={setSelected} /> |
| 48 | </PromptFrame> |
| 49 | } |
| 50 | /> |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | export const planDecisionScreen = <Value,>(options: { |
| 55 | readonly plan: Plan | PlanTreeData; |
| 56 | readonly label?: string; |
| 57 | readonly message: string; |
| 58 | readonly choices: ReadonlyArray<PlanDecisionChoice<Value>>; |
| 59 | readonly initialValue: Value; |
| 60 | }) => |
| 61 | Screen.make<Value>("plan decision", (controller) => ( |
| 62 | <PlanDecision {...options} controller={controller} /> |
| 63 | )); |