The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 104 lines 3.13 KB
Raw
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 as AlchemyPlan } from "../../../Plan.ts";
7 import { theme } from "../../../Util/Theme.ts";
8 import {
9 ChoiceGroup,
10 Box,
11 KeyBar,
12 Text,
13 useGlyphs,
14 useKeyGlyphs,
15 useTerminalInput,
16 } from "../ui/index.ts";
17 import { Plan, PlanTree } from "./PlanView.tsx";
18
19 export interface ApprovePlanProps {
20 plan: AlchemyPlan;
21 detailed?: boolean;
22 controller: ScreenController<boolean>;
23 }
24
25 /**
26 * Plan approval prompt: the plan tree followed by an operation-specific
27 * action and Cancel. Escape cancels; Ctrl+C is handled centrally by the
28 * screen runner.
29 */
30 export function ApprovePlan(props: ApprovePlanProps): JSX.Element {
31 const { plan, detailed = false, controller } = props;
32 // Destruction is the risky path: require an explicit move to Destroy before
33 // Enter can remove or orphan resources. Deploy approvals remain action-first.
34 const [approved, setApproved] = useState(!plan.destroy);
35 const action = plan.destroy ? "Destroy" : "Deploy";
36 const glyphs = useGlyphs();
37 const keys = useKeyGlyphs();
38 const tree = useMemo(
39 () =>
40 new PlanTree(plan, {
41 detailed,
42 mode: "review",
43 label: action,
44 }),
45 [plan, detailed, action],
46 );
47 const complete = (answer: boolean) => {
48 const verdict = (
49 <Text color={answer ? theme.color.success : theme.color.danger}>
50 {answer ? glyphs.success : glyphs.error} {answer ? action : "Cancelled"}
51 </Text>
52 );
53 // On approval the plan disappears — the apply progress that follows
54 // shows every row again. A declined plan stays on screen (complete,
55 // unscrolled) so the user can review what they just turned down.
56 if (!answer) tree.setViewport("full");
57 const summary = answer ? (
58 verdict
59 ) : (
60 <Box flexDirection="column">
61 <Plan tree={tree} />
62 {verdict}
63 </Box>
64 );
65 controller.submit(answer, summary);
66 };
67
68 useTerminalInput((input, key) => {
69 if (key.enter) complete(approved);
70 else if (key.escape) controller.cancel();
71 else if (key.ctrl || key.meta) return;
72 else if (input.toLowerCase() === "y") complete(true);
73 else if (input.toLowerCase() === "n") complete(false);
74 });
75
76 const prompt = (
77 <Box flexDirection="column">
78 <Text bold>{action}?</Text>
79 <ChoiceGroup
80 value={approved}
81 choices={[
82 { value: true, label: action },
83 { value: false, label: "Cancel" },
84 ]}
85 onChange={setApproved}
86 />
87 <KeyBar
88 keys={[
89 [keys.upDown, "scroll plan"],
90 [keys.leftRight, "choose"],
91 [keys.enter, "confirm"],
92 [keys.escape, "cancel"],
93 ]}
94 />
95 </Box>
96 );
97
98 return <Plan tree={tree} footer={prompt} />;
99 }
100
101 export const approvePlanScreen = (plan: AlchemyPlan, detailed = false) =>
102 Screen.make<boolean>("plan approval", (controller) => (
103 <ApprovePlan plan={plan} detailed={detailed} controller={controller} />
104 ));