import { measureElement, useProgress, useTitle, type DOMElement } from "@alchemy.run/sigil"; import { useLayoutEffect, useRef, useState } from "@alchemy.run/sigil/react"; import { useAlchemyStatus, useTerminalSize } from "../ui/index.ts"; import type { PlanTree, PlanTreeState } from "./PlanTree.ts"; export const usePlanPresentation = (options: { readonly tree: PlanTree; readonly state: PlanTreeState; readonly busy: boolean; readonly collapsible: boolean; readonly hasFooter: boolean; }) => { const { tree, state, busy, collapsible, hasFooter } = options; const { mode } = tree; const { label, expanded, viewport, outcome } = state; const { completed, failures, total } = tree.progress(); const collapsed = collapsible && !expanded; const settled = !busy && completed >= total; useProgress( mode === "apply" ? { state: outcome === "failure" || failures > 0 ? "error" : busy ? "normal" : "inactive", value: total === 0 ? undefined : (completed / total) * 100, } : { state: "inactive" }, ); const titleProgress = settled || total === 0 ? "" : ` ${completed}/${total}`; const titleDetail = tree.titleDetail ? ` ยท ${tree.titleDetail}` : ""; useTitle(mode === "apply" ? `${label}${titleProgress}${titleDetail}` : undefined); // OSC 7501: working while applying, then the outcome, which stays visible // in the terminal after the CLI exits. const failed = outcome === "failure" || failures > 0; useAlchemyStatus( mode !== "apply" ? undefined : busy ? { state: "working", progress: total === 0 ? undefined : (completed / total) * 100, msg: `${label}${titleDetail}`, } : { state: failed ? "error" : "done", msg: `${label}${titleDetail}` }, ); const beforeRef = useRef(null); const summaryRef = useRef(null); const controlsRef = useRef(null); const afterRef = useRef(null); const [chromeRows, setChromeRows] = useState(); const { rows: terminalRows } = useTerminalSize(); const showControls = mode === "apply" || (!collapsed && (collapsible || hasFooter)); useLayoutEffect(() => { if (collapsed || viewport !== "virtual") return; const height = (ref: { readonly current: DOMElement | null }) => ref.current === null ? 0 : measureElement(ref.current).height; const measured = height(beforeRef) + height(summaryRef) + height(controlsRef) + height(afterRef) + 1 + (showControls ? 1 : 0); setChromeRows((current) => (current === measured ? current : measured)); }); const lineBudget = viewport === "virtual" ? Math.max(1, terminalRows - (mode === "apply" ? 3 : 0) - (chromeRows ?? 8)) : Number.POSITIVE_INFINITY; return { progress: { completed, failures, total }, collapsed, showControls, lineBudget, refs: { beforeRef, summaryRef, controlsRef, afterRef }, } as const; };