import { useState, useEffect, useMemo } from "react"; import { useTranslation } from "@termix/plugin-sdk/frontend"; import { Button, Input, Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from "@termix/plugin-sdk/ui"; import { extractSnippetInputs, resolveSnippetContent, type SnippetHostContext, } from "../shared/variables.js"; /** Enough of a snippet to fill in its inputs. */ export interface VariablesDialogSnippet { name: string; content: string; } /** * Shown before running a snippet that contains $INPUT_n placeholders: * collects a value per placeholder and previews the fully resolved command * (host vars and inputs) before handing the result back to the caller. */ export function SnippetVariablesDialog({ snippet, host, onCancel, onConfirm, }: { snippet: VariablesDialogSnippet; host: SnippetHostContext | null; onCancel: () => void; onConfirm: ( resolvedContent: string, inputValues: Record, ) => void; }) { const { t } = useTranslation(); const inputs = useMemo( () => extractSnippetInputs(snippet.content), [snippet.content], ); const [values, setValues] = useState>({}); useEffect(() => { setValues({}); }, [snippet]); const preview = resolveSnippetContent(snippet.content, host, values); return ( !v && onCancel()}> {t("variablesDialogTitle", { name: snippet.name })} {t("variablesDialogDescription")}
{inputs.map((input) => (
setValues((prev) => ({ ...prev, [input.key]: e.target.value, })) } />
))}
{preview}
); }