/** @jsxRuntime automatic */ /** @jsxImportSource @alchemy.run/sigil */ import { useMemo, useState } from "@alchemy.run/sigil/react"; import type { JSX } from "react"; import { Screen, type ScreenController } from "../../../Interaction.ts"; import type { Plan as AlchemyPlan } from "../../../Plan.ts"; import { theme } from "../../../Util/Theme.ts"; import { ChoiceGroup, Box, KeyBar, Text, useAlchemyStatus, useGlyphs, useKeyGlyphs, useTerminalInput, } from "../ui/index.ts"; import { Plan, PlanTree } from "./PlanView.tsx"; export interface ApprovePlanProps { plan: AlchemyPlan; detailed?: boolean; controller: ScreenController; } /** * Plan approval prompt: the plan tree followed by an operation-specific * action and Cancel. Escape cancels; Ctrl+C is handled centrally by the * screen runner. */ export function ApprovePlan(props: ApprovePlanProps): JSX.Element { const { plan, detailed = false, controller } = props; // Destruction is the risky path: require an explicit move to Destroy before // Enter can remove or orphan resources. Deploy approvals remain action-first. const [approved, setApproved] = useState(!plan.destroy); const action = plan.destroy ? "Destroy" : "Deploy"; const glyphs = useGlyphs(); const keys = useKeyGlyphs(); useAlchemyStatus({ state: "blocked", kind: "permission", msg: `${action}?` }); const tree = useMemo( () => new PlanTree(plan, { detailed, mode: "review", label: action, }), [plan, detailed, action], ); const complete = (answer: boolean) => { const verdict = ( {answer ? glyphs.success : glyphs.error} {answer ? action : "Cancelled"} ); // On approval the plan disappears — the apply progress that follows // shows every row again. A declined plan stays on screen (complete, // unscrolled) so the user can review what they just turned down. if (!answer) tree.setViewport("full"); const summary = answer ? ( verdict ) : ( {verdict} ); controller.submit(answer, summary); }; useTerminalInput((input, key) => { if (key.enter) complete(approved); else if (key.escape) controller.cancel(); else if (key.ctrl || key.meta) return; else if (input.toLowerCase() === "y") complete(true); else if (input.toLowerCase() === "n") complete(false); }); const prompt = ( {action}? ); return ; } export const approvePlanScreen = (plan: AlchemyPlan, detailed = false) => Screen.make("plan approval", (controller) => ( ));