import { useCallback, useEffect, useRef, useState } from "react"; import { listen } from "@tauri-apps/api/event"; import { cancelAwsDeployment } from "../../../lib/tauri-commands"; import { awsErrorMessage, toAwsError } from "../../../lib/aws-error"; import { AWS_DEPLOY_PROGRESS_EVENT, type AwsError, type DeployProgress as ProgressPayload, type TofuEvent, type TofuOutcome, } from "../../../lib/types"; import Button from "../../ui/Button"; import Modal from "../../ui/Modal"; import StatusIndicator from "../../ui/StatusIndicator"; /** * What a run does. Decisions about a run (such as offering an admin bundle * when AWS refuses it) key on this, never on its label: a follow of an * existing environment's deploy is labelled like a new deploy. */ export type RunKind = "deploy" | "follow" | "repair" | "update" | "destroy" | "onboard" | "push" | "boundary"; /** One OpenTofu run this view is showing: a start, or a follow of one already running. */ export interface DeployRun { stateKey: string; region: string; kind: RunKind; label: string; /** Null until the starting command resolves. */ opId: string | null; events: TofuEvent[]; done: boolean; cancellable: boolean; result: TofuOutcome | null; } type Subscription = { unlisten: (() => void) | null }; /** Runs are tracked per region and state key: the same key exists in every region. */ export const runKey = (region: string, stateKey: string) => `${region}:${stateKey}`; /** * Runs keyed by `runKey(region, stateKey)`. `start` subscribes to `aws-deploy-progress` before * invoking, keeps every event for the state key it started until the op id * is known (the backend may emit just before the invoke resolves), then keeps * only that op's events. Resolves to the command's error, or null once started. */ export function useDeployRuns( onDone?: (run: { stateKey: string; region: string; result: TofuOutcome | null }) => void, ) { const [runs, setRuns] = useState>({}); const subs = useRef(new Map()); const mounted = useRef(true); const onDoneRef = useRef(onDone); onDoneRef.current = onDone; useEffect(() => { mounted.current = true; const all = subs.current; return () => { mounted.current = false; for (const s of all.values()) s.unlisten?.(); all.clear(); }; }, []); const start = useCallback( async ( stateKey: string, region: string, kind: RunKind, label: string, run: () => Promise, ): Promise => { const k = runKey(region, stateKey); subs.current.get(k)?.unlisten?.(); const sub: Subscription = { unlisten: null }; subs.current.set(k, sub); const current = () => mounted.current && subs.current.get(k) === sub; const stop = () => { sub.unlisten?.(); sub.unlisten = null; if (subs.current.get(k) === sub) subs.current.delete(k); }; let opId: string | null = null; const early: ProgressPayload[] = []; const apply = (p: ProgressPayload) => { setRuns((prev) => { const r = prev[k]; if (!r || r.opId !== p.op_id || r.done) return prev; return { ...prev, [k]: { ...r, events: p.events.length ? [...r.events, ...p.events] : r.events, done: p.done, cancellable: p.done ? false : p.cancellable, result: p.result ?? r.result, }, }; }); if (p.done) { stop(); onDoneRef.current?.({ stateKey, region, result: p.result ?? null }); } }; setRuns((prev) => ({ ...prev, [k]: { stateKey, region, kind, label, opId: null, events: [], done: false, cancellable: false, result: null }, })); const unlisten = await listen(AWS_DEPLOY_PROGRESS_EVENT, ({ payload }) => { if (!current() || payload.state_key !== stateKey) return; if (opId === null) early.push(payload); else if (payload.op_id === opId) apply(payload); }); if (!current()) { unlisten(); return null; } sub.unlisten = unlisten; try { opId = await run(); } catch (e) { stop(); if (mounted.current) { setRuns((prev) => { const { [k]: _gone, ...rest } = prev; return rest; }); } return toAwsError(e); } if (!current()) return null; const id = opId; setRuns((prev) => (prev[k] ? { ...prev, [k]: { ...prev[k], opId: id } } : prev)); for (const p of early.splice(0)) if (p.op_id === id) apply(p); return null; }, [], ); /** `key` is a `runKey`. */ const dismiss = useCallback((key: string) => { setRuns((prev) => { if (!prev[key]?.done) return prev; const { [key]: _gone, ...rest } = prev; return rest; }); }, []); return { runs, start, dismiss }; } function EventLine({ e }: { e: TofuEvent }) { const isError = e.level === "error"; return (
  • {(e.resource || e.action) && ( {e.resource && {e.resource}} {e.action && {e.action}} )} {isError ? ( <> Error:
    {e.message}
    ) : ( {e.message} )}
  • ); } /** A run's events in order, its final result, and Cancel (after a confirmation). */ export default function DeployProgress({ run, connectionId, onDismiss, }: { run: DeployRun; connectionId: string; onDismiss?: () => void; }) { const [confirming, setConfirming] = useState(false); const [cancelling, setCancelling] = useState(false); const [cancelError, setCancelError] = useState(null); const listRef = useRef(null); useEffect(() => { const el = listRef.current; if (el) el.scrollTop = el.scrollHeight; }, [run.events.length]); const cancel = async () => { setCancelling(true); setCancelError(null); try { await cancelAwsDeployment(connectionId, run.region, run.stateKey); setConfirming(false); } catch (e) { setCancelError(toAwsError(e)); } finally { setCancelling(false); } }; const status = run.done ? null : run.opId === null ? { tone: "busy" as const, label: "Starting…" } : { tone: "busy" as const, label: "Running" }; return (
    {run.label} {status && }
    {!run.done && ( )} {run.done && onDismiss && ( )}
    {run.events.length > 0 && (
      {run.events.map((e, i) => ( ))}
    )} {run.done && run.result && (

    {run.result.ok ? ( ) : ( )}

    )} {cancelError && !confirming && (

    {awsErrorMessage(cancelError)}

    )} {confirming && ( setConfirming(false)} footer={ <> } >

    OpenTofu stops gracefully and saves its state. Anything it already created stays; Repair or Update finishes the job later.

    {cancelError && (

    {awsErrorMessage(cancelError)}

    )}
    )}
    ); }