/** @jsxRuntime automatic */ /** @jsxImportSource @alchemy.run/sigil */ import { useEffect, useMemo, useState, useSyncExternalStore } from "@alchemy.run/sigil/react"; import * as Cause from "effect/Cause"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import type * as Fiber from "effect/Fiber"; import * as Option from "effect/Option"; import type { JSX } from "react"; import { Screen, type ScreenController } from "../../../Interaction.ts"; import type { InvalidStatePath, StateStoreError } from "../../../State/index.ts"; import { theme } from "../../../Util/Theme.ts"; import { formatYamlLines, matchYamlKey } from "../../PropertyDiff.ts"; import { Box, KeyBar, Spinner, Text, TextField, Viewport, useBorderStyle, useCliEnvironment, useKeyGlyphs, useTerminalInput, useTerminalSize, } from "../ui/index.ts"; export type StateFileRef = | { readonly kind: "resource"; readonly stack: string; readonly stage: string; readonly fqn: string; } | { readonly kind: "output"; readonly stack: string; readonly stage: string; }; export type StateExplorerError = InvalidStatePath | StateStoreError; export interface StateExplorerSource { readonly backend: string; readonly listStacks: Effect.Effect, StateExplorerError>; readonly listStages: (stack: string) => Effect.Effect, StateExplorerError>; readonly listResources: ( stack: string, stage: string, ) => Effect.Effect, StateExplorerError>; readonly readFile: (file: StateFileRef) => Effect.Effect; readonly deleteNodes: ( nodes: ReadonlyArray, ) => Effect.Effect; } export type StateBrowserNode = | { readonly kind: "stack"; readonly id: string; readonly name: string; readonly path: string; readonly stack: string; } | { readonly kind: "stage"; readonly id: string; readonly name: string; readonly path: string; readonly stack: string; readonly stage: string; } | { readonly kind: "namespace"; readonly id: string; readonly name: string; readonly path: string; readonly stack: string; readonly stage: string; readonly children: ReadonlyArray; } | { readonly kind: "resource"; readonly id: string; readonly name: string; readonly path: string; readonly file: StateFileRef & { readonly kind: "resource" }; } | { readonly kind: "output"; readonly id: string; readonly name: string; readonly path: string; readonly file: StateFileRef & { readonly kind: "output" }; }; type LoadState = | { readonly status: "idle" } | { readonly status: "loading" } | { readonly status: "ready"; readonly value: Value } | { readonly status: "error"; readonly message: string }; interface ExplorerSnapshot { readonly root: LoadState>; readonly children: ReadonlyMap>>; readonly files: ReadonlyMap>; } const errorMessage = (error: StateExplorerError) => error._tag === "InvalidStatePath" ? `${error.path}: ${error.reason}` : error.message; const causeMessage = (cause: Cause.Cause) => Option.match(Cause.findErrorOption(cause), { onSome: errorMessage, onNone: () => { const defect = Cause.squash(cause); return defect instanceof Error ? defect.message : String(defect); }, }); /** Mutable async cache: every state-store read is initiated by a selection. */ export class StateExplorerStore { private state: ExplorerSnapshot = { root: { status: "idle" }, children: new Map(), files: new Map(), }; private readonly listeners = new Set<() => void>(); private readonly fibers = new Set>(); constructor( readonly source: StateExplorerSource, private readonly services: Context.Context = Context.empty(), ) {} readonly subscribe = (listener: () => void) => { this.listeners.add(listener); return () => this.listeners.delete(listener); }; readonly snapshot = () => this.state; private commit(next: ExplorerSnapshot) { this.state = next; for (const listener of this.listeners) listener(); } /** * Fork the effect in the CLI's ambient services and hand its exit to the * caller. A fiber the store no longer tracks — dropped by `refresh` or * `dispose` — is stale, and its result is discarded even if it managed to * complete before the interrupt landed. */ private run( effect: Effect.Effect, handlers: { readonly onSuccess: (value: A) => void; readonly onFailure: (message: string) => void; }, ) { const fiber = Effect.runForkWith(this.services)(effect); this.fibers.add(fiber); fiber.addObserver((exit) => { if (!this.fibers.delete(fiber)) return; if (Exit.isSuccess(exit)) handlers.onSuccess(exit.value); else if (!Cause.hasInterrupts(exit.cause)) handlers.onFailure(causeMessage(exit.cause)); }); } private interruptAll() { for (const fiber of this.fibers) fiber.interruptUnsafe(); this.fibers.clear(); } readonly loadRoot = () => { if (this.state.root.status !== "idle") return; this.commit({ ...this.state, root: { status: "loading" } }); this.run(this.source.listStacks, { onSuccess: (stacks) => { this.commit({ ...this.state, root: { status: "ready", value: [...stacks].sort().map((stack) => ({ kind: "stack", id: `stack:${stack}`, name: stack, path: stack, stack, })), }, }); }, onFailure: (message) => { this.commit({ ...this.state, root: { status: "error", message } }); }, }); }; readonly refresh = () => { this.interruptAll(); this.commit({ root: { status: "idle" }, children: new Map(), files: new Map(), }); this.loadRoot(); }; /** * Re-list only what a deletion could have changed — the parent listing of * each target — and drop cached files under it. Every other cached column * survives, so the explorer keeps its position instead of collapsing to * the stack list the way `refresh` does. */ readonly invalidate = (targets: ReadonlyArray) => { const children = new Map(this.state.children); const files = new Map(this.state.files); const parents = new Map(); for (const target of targets) { for (const id of files.keys()) { if (id.slice(id.indexOf(":") + 1).startsWith(`${target.path}/`)) files.delete(id); } if (target.kind === "stack") { for (const id of children.keys()) { if (id === `stack:${target.stack}` || id.startsWith(`stage:${target.stack}/`)) children.delete(id); } parents.set("root", undefined); } else if (target.kind === "stage") { children.delete(target.id); parents.set(`stack:${target.stack}`, { kind: "stack", id: `stack:${target.stack}`, name: target.stack, path: target.stack, stack: target.stack, }); } else if (target.kind !== "output") { const { stack, stage } = target.kind === "resource" ? target.file : target; parents.set(`stage:${stack}/${stage}`, { kind: "stage", id: `stage:${stack}/${stage}`, name: stage, path: `${stack}/${stage}`, stack, stage, }); } } for (const id of parents.keys()) { if (id !== "root") children.delete(id); } this.commit({ ...this.state, root: parents.has("root") ? { status: "idle" } : this.state.root, children, files, }); for (const parent of parents.values()) { if (parent === undefined) this.loadRoot(); else this.loadChildren(parent); } }; readonly loadChildren = (node: StateBrowserNode) => { if (node.kind === "namespace" || node.kind === "resource" || node.kind === "output") { return; } if ((this.state.children.get(node.id)?.status ?? "idle") !== "idle") return; const children = new Map(this.state.children); children.set(node.id, { status: "loading" }); this.commit({ ...this.state, children }); const request = node.kind === "stack" ? Effect.map(this.source.listStages(node.stack), (stages) => [...stages].sort().map((stage): StateBrowserNode => ({ kind: "stage", id: `stage:${node.stack}/${stage}`, name: stage, path: `${node.stack}/${stage}`, stack: node.stack, stage, })), ) : Effect.map(this.source.listResources(node.stack, node.stage!), (fqns) => buildStageNodes(node.stack, node.stage!, fqns), ); this.run(request, { onSuccess: (value) => { const next = new Map(this.state.children); next.set(node.id, { status: "ready", value }); this.commit({ ...this.state, children: next }); }, onFailure: (message) => { const next = new Map(this.state.children); next.set(node.id, { status: "error", message }); this.commit({ ...this.state, children: next }); }, }); }; readonly loadFile = (node: StateBrowserNode) => { if (node.kind !== "resource" && node.kind !== "output") return; if ((this.state.files.get(node.id)?.status ?? "idle") !== "idle") return; const files = new Map(this.state.files); files.set(node.id, { status: "loading" }); this.commit({ ...this.state, files }); this.run(this.source.readFile(node.file), { onSuccess: (value) => { const next = new Map(this.state.files); next.set(node.id, { status: "ready", value }); this.commit({ ...this.state, files: next }); }, onFailure: (message) => { const next = new Map(this.state.files); next.set(node.id, { status: "error", message }); this.commit({ ...this.state, files: next }); }, }); }; readonly deleteNodes = ( targets: ReadonlyArray, handlers: { readonly onSuccess: () => void; readonly onFailure: (message: string) => void; }, ) => this.run(this.source.deleteNodes(targets), handlers); readonly dispose = () => { this.interruptAll(); this.listeners.clear(); }; } interface MutableNamespace { readonly name: string; readonly path: string; readonly children: Map; readonly files: StateBrowserNode[]; } /** Convert listed FQNs to namespace columns without reading any state values. */ export const buildStageNodes = ( stack: string, stage: string, fqns: ReadonlyArray, ): ReadonlyArray => { const root: MutableNamespace = { name: "", path: `${stack}/${stage}`, children: new Map(), files: [], }; for (const fqn of [...fqns].sort()) { const parts = fqn.split("/").filter(Boolean); let current = root; for (const part of parts.slice(0, -1)) { let child = current.children.get(part); if (child === undefined) { child = { name: part, path: `${current.path}/${part}`, children: new Map(), files: [], }; current.children.set(part, child); } current = child; } const name = parts.at(-1) ?? fqn; current.files.push({ kind: "resource", id: `resource:${stack}/${stage}/${fqn}`, name, path: `${stack}/${stage}/${fqn}`, file: { kind: "resource", stack, stage, fqn }, }); } const freeze = (namespace: MutableNamespace): StateBrowserNode[] => [ ...[...namespace.children.values()] .sort((a, b) => a.name.localeCompare(b.name)) .map((child): StateBrowserNode => ({ kind: "namespace", id: `namespace:${child.path}`, name: child.name, path: child.path, stack, stage, children: freeze(child), })), ...namespace.files, ]; return [ ...freeze(root), { kind: "output", id: `output:${stack}/${stage}`, name: "output", path: `${stack}/${stage}/output`, file: { kind: "output", stack, stage }, }, ]; }; const nodeStyle: Record< StateBrowserNode["kind"], { readonly color: string; readonly icon: string; readonly asciiIcon: string } > = { stack: { color: theme.color.brand, icon: "◆", asciiIcon: "#" }, stage: { color: theme.color.warning, icon: "●", asciiIcon: "@" }, namespace: { color: theme.color.accent, icon: "▸", asciiIcon: ">" }, resource: { color: theme.color.info, icon: "▪", asciiIcon: "-" }, output: { color: theme.color.success, icon: "◇", asciiIcon: "=" }, }; const childrenFor = ( node: StateBrowserNode, state: ExplorerSnapshot, ): LoadState> | undefined => node.kind === "namespace" ? { status: "ready", value: node.children } : node.kind === "stack" || node.kind === "stage" ? (state.children.get(node.id) ?? { status: "idle" }) : undefined; interface BrowserColumn { readonly id: string; readonly title: string; readonly state: LoadState>; } const buildColumns = ( state: ExplorerSnapshot, selection: ReadonlyArray, ): BrowserColumn[] => { const columns: BrowserColumn[] = [{ id: "root", title: "Stacks", state: state.root }]; let current = state.root; for (let depth = 0; depth < selection.length; depth++) { if (current.status !== "ready") break; const selected = current.value.find((node) => node.id === selection[depth]); if (selected === undefined) break; const children = childrenFor(selected, state); if (children === undefined) break; columns.push({ id: selected.id, title: selected.kind === "stack" ? "Stages" : selected.kind === "stage" ? "State" : selected.name, state: children, }); current = children; } return columns; }; type ColumnProps = { readonly column: BrowserColumn; readonly selected: string | undefined; readonly focused: boolean; readonly height: number; readonly query: string; readonly marked: ReadonlySet; }; function Column({ column, selected, focused, height, query, marked }: ColumnProps) { const { unicode } = useCliEnvironment(); const borderStyle = useBorderStyle(); if (column.state.status === "idle" || column.state.status === "loading") { return ; } if (column.state.status === "error") { return {column.state.message}; } const needle = query.trim().toLowerCase(); const items = needle === "" ? column.state.value : column.state.value.filter((node) => node.path.toLowerCase().includes(needle)); const selectedIndex = Math.max( 0, items.findIndex((node) => node.id === selected), ); return ( node.id} empty={No matches.} renderItem={(node) => { const active = node.id === selected; const style = nodeStyle[node.kind]; return ( {marked.has(node.id) ? unicode ? "● " : "* " : `${unicode ? style.icon : style.asciiIcon} `} {node.name} {(node.kind === "stack" || node.kind === "stage" || node.kind === "namespace") && ( {unicode ? "›" : ">"} )} ); }} /> ); } type YamlLineProps = { readonly line: string }; function YamlLine({ line }: YamlLineProps) { const key = matchYamlKey(line); if (key !== undefined) { return ( {key.indent} {key.key} ); } return ( ); } type YamlValueProps = { readonly value: string }; function YamlValue({ value }: YamlValueProps) { const trimmed = value.trimStart(); const color = /^(["'])/.test(trimmed) ? theme.color.success : /^(true|false)/.test(trimmed) ? theme.color.accent : /^-?\d/.test(trimmed) ? theme.color.warning : trimmed.startsWith("null") ? theme.color.danger : undefined; return {value}; } type PreviewProps = { readonly node: StateBrowserNode | undefined; readonly state: LoadState | undefined; readonly lines: ReadonlyArray | undefined; readonly offset: number; readonly height: number; }; function Preview({ node, state, lines = [], offset, height }: PreviewProps) { if (node === undefined || (node.kind !== "resource" && node.kind !== "output")) { return Select a state file to preview it.; } if (state === undefined || state.status === "idle" || state.status === "loading") { return ; } if (state.status === "error") return {state.message}; if (state.value === undefined) { return No stored value.; } const bodyHeight = Math.max(1, height - 2); const start = Math.max(0, Math.min(offset, Math.max(0, lines.length - bodyHeight))); return ( {node.path} {lines.slice(start, start + bodyHeight).map((line, index) => ( ))} ); } function StateExplorer({ store, controller, }: { readonly store: StateExplorerStore; readonly controller: ScreenController; }): JSX.Element { const state = useSyncExternalStore(store.subscribe, store.snapshot); const [selection, setSelection] = useState>([]); const [columnIndex, setColumnIndex] = useState(0); const [query, setQuery] = useState(""); const [searching, setSearching] = useState(false); const [previewOffset, setPreviewOffset] = useState(0); const [previewFocused, setPreviewFocused] = useState(false); const [marked, setMarked] = useState>(new Map()); const [deletion, setDeletion] = useState< | { readonly status: "confirm" | "deleting"; readonly targets: ReadonlyArray; } | { readonly status: "error"; readonly message: string; readonly targets: ReadonlyArray; } >(); const [notice, setNotice] = useState(); const { columns: terminalColumns, rows: terminalRows } = useTerminalSize(); const borderStyle = useBorderStyle(); const keys = useKeyGlyphs(); useEffect(() => { store.loadRoot(); return store.dispose; }, [store]); const columns = useMemo(() => buildColumns(state, selection), [selection, state]); const markedIds = useMemo(() => new Set(marked.keys()), [marked]); const activeColumn = Math.min(columnIndex, columns.length - 1); const column = columns[activeColumn]!; const filteredItems = column.state.status !== "ready" ? [] : query.trim() === "" ? column.state.value : column.state.value.filter((node) => node.path.toLowerCase().includes(query.trim().toLowerCase()), ); const selectedId = selection[activeColumn]; const selectedIndex = filteredItems.findIndex((node) => node.id === selectedId); const selected = filteredItems[selectedIndex]; const lastSelected = (() => { for (let index = columns.length - 1; index >= 0; index--) { const candidate = columns[index]; if (candidate?.state.status !== "ready") continue; const node = candidate.state.value.find((item) => item.id === selection[index]); if (node !== undefined) return node; } return undefined; })(); const file = lastSelected?.kind === "resource" || lastSelected?.kind === "output" ? lastSelected : undefined; const fileState = file === undefined ? undefined : state.files.get(file.id); // The delete prompt replaces the key bar (a margin row + the keys) with a // bordered panel: border, header line, one line per target (capped, with // a "+N more" line), and its own key bar. KeyBar is a full-width block, // so it gets its own row rather than sharing one with the header. const promptTargets = deletion === undefined ? [] : deletion.targets.slice(0, MAX_PROMPT_TARGETS); const promptOverflow = deletion === undefined ? 0 : deletion.targets.length - promptTargets.length; const footerHeight = deletion === undefined ? 2 : 3 + promptTargets.length + (promptOverflow > 0 ? 1 : 0); const contentHeight = Math.max(6, terminalRows - 8 - footerHeight); const previewLines = useMemo( () => fileState?.status === "ready" && fileState.value !== undefined ? formatYamlLines(fileState.value) : undefined, [fileState], ); const maxPreviewOffset = previewLines === undefined ? 0 : Math.max(0, previewLines.length - Math.max(1, contentHeight - 2)); const choose = (index: number, node: StateBrowserNode) => { setSelection((current) => [...current.slice(0, index), node.id]); setPreviewOffset(0); if (node.kind === "resource" || node.kind === "output") store.loadFile(node); else store.loadChildren(node); }; const move = (delta: number) => { if (filteredItems.length === 0) return; const next = selectedIndex < 0 ? delta < 0 ? filteredItems.length - 1 : 0 : Math.max(0, Math.min(filteredItems.length - 1, selectedIndex + delta)); choose(activeColumn, filteredItems[next]!); }; const beginDelete = () => { const targets = marked.size > 0 ? [...marked.values()] : selected ? [selected] : []; if (targets.length === 0) return; if (targets.some((node) => node.kind === "output")) { setDeletion({ status: "error", message: "output cannot be deleted independently", targets, }); return; } setDeletion({ status: "confirm", targets }); }; const confirmDelete = () => { if (deletion === undefined || deletion.status === "deleting") return; const targets = deletion.targets; const deletes = (node: StateBrowserNode) => targets.some((target) => node.id === target.id || node.path.startsWith(`${target.path}/`)); // The shallowest column whose selected node is going away: the cursor // moves there, onto the next surviving sibling (or the previous one). // Deeper selection is dropped; everything above it is untouched. const landing = (() => { for (let index = 0; index < columns.length; index++) { const column = columns[index]!; if (column.state.status !== "ready") return undefined; const items = column.state.value; const position = items.findIndex((node) => node.id === selection[index]); if (position < 0 || !deletes(items[position]!)) continue; const fallback = items.slice(position + 1).find((node) => !deletes(node)) ?? items.slice(0, position).findLast((node) => !deletes(node)); return { index, fallback }; } return undefined; })(); setDeletion({ status: "deleting", targets }); store.deleteNodes(targets, { onSuccess: () => { setDeletion(undefined); setMarked(new Map()); if (landing !== undefined) { setSelection((current) => landing.fallback === undefined ? current.slice(0, landing.index) : [...current.slice(0, landing.index), landing.fallback.id], ); setColumnIndex(landing.index); setPreviewOffset(0); setPreviewFocused(false); } setNotice( `Deleted state at ${targets.map((target) => `${target.path}${target.kind === "resource" ? "" : "/"}`).join(", ")}`, ); store.invalidate(targets); }, onFailure: (message) => setDeletion({ status: "error", message, targets }), }); }; useTerminalInput( (input, key) => { if (deletion !== undefined) { if (deletion.status === "deleting") return; if (key.escape || input === "n" || input === "q") setDeletion(undefined); else if (deletion.status === "confirm" && input === "y") confirmDelete(); return; } if (input === "q") return controller.submit(undefined); if (key.escape) { if (query !== "") setQuery(""); else controller.submit(undefined); return; } if (!key.ctrl && !key.meta && input === "/") { setSearching(true); setPreviewFocused(false); return; } if (!key.ctrl && !key.meta && input === "r") { setSelection([]); setColumnIndex(0); setPreviewFocused(false); setNotice(undefined); store.refresh(); return; } if (input === " " && selected !== undefined) { setMarked((current) => { const next = new Map(current); if (next.has(selected.id)) next.delete(selected.id); else next.set(selected.id, selected); return next; }); return; } if (input === "d") { beginDelete(); return; } if (key.tab) { if (file !== undefined) setPreviewFocused((current) => !current); return; } if (previewFocused) { if (key.left) setPreviewFocused(false); else if (key.up) setPreviewOffset((value) => Math.max(0, value - 1)); else if (key.down) setPreviewOffset((value) => Math.min(maxPreviewOffset, value + 1)); else if (key.pageUp) setPreviewOffset((value) => Math.max(0, value - contentHeight)); else if (key.pageDown) setPreviewOffset((value) => Math.min(maxPreviewOffset, value + contentHeight)); else if (key.home) setPreviewOffset(0); else if (key.end) setPreviewOffset(maxPreviewOffset); return; } if (key.up || input === "k") move(-1); else if (key.down || input === "j") move(1); else if (key.home && filteredItems[0]) choose(activeColumn, filteredItems[0]); else if (key.end && filteredItems.at(-1)) choose(activeColumn, filteredItems.at(-1)!); else if (key.left && activeColumn > 0) setColumnIndex(activeColumn - 1); else if (key.right || key.enter) { if (selected === undefined) return; choose(activeColumn, selected); if (selected.kind === "resource" || selected.kind === "output") { setPreviewFocused(true); } else { setColumnIndex(activeColumn + 1); } } }, { active: !searching }, ); const previewWidth = file === undefined ? 0 : Math.max(34, Math.floor(terminalColumns * 0.42)); const columnWidth = Math.max(20, Math.min(28, Math.floor((terminalColumns - previewWidth) / 2))); const availableForColumns = Math.max(columnWidth, terminalColumns - previewWidth - 2); const visibleColumnCount = Math.max(1, Math.floor(availableForColumns / (columnWidth + 1))); const visibleStart = Math.max(0, columns.length - visibleColumnCount); const visibleColumns = columns.slice(visibleStart); const currentPath = selected?.path ?? "/"; const displayPath = selected === undefined || selected.kind === "resource" || selected.kind === "output" ? currentPath : `${currentPath}/`; return ( // Pinned to the terminal width: without it the root sizes to its content, // and the layout visibly narrows whenever the full-width key bar is // swapped out for the delete panel. ● state · {displayPath} {store.source.backend} {notice === undefined ? null : ( ✓ {notice} )} {searching ? ( / setSearching(false)} onCancel={() => setSearching(false)} /> ) : query === "" ? null : ( filter · {query} )} {visibleColumns.map((item, offset) => { const index = visibleStart + offset; return ( 0} borderRight={false} borderTop={false} borderBottom={false} borderColor={ !previewFocused && index === activeColumn ? theme.color.accent : theme.color.muted } > {item.title.toUpperCase()} ); })} {file === undefined ? null : ( PREVIEW )} {deletion === undefined ? ( ) : ( {deletion.status === "deleting" ? ( ) : deletion.status === "error" ? ( {deletion.message} ) : ( Delete state records? · Cloud resources will not be deleted. )} {promptTargets.map((target) => ( {" "} {target.path} {target.kind === "resource" ? "" : "/"} ))} {promptOverflow > 0 ? {` +${promptOverflow} more`} : null} )} ); } const MAX_PROMPT_TARGETS = 4; type DeletionActionProps = { status: "confirm" | "deleting" | "error"; }; function DeletionAction({ status }: DeletionActionProps) { if (status === "deleting") return null; if (status === "error") return ; return ( ); } export const stateExplorerScreen = ( source: StateExplorerSource, services?: Context.Context, ) => { const store = new StateExplorerStore(source, services); return Screen.make("state explorer", (controller) => ( )); };