import { useState } from "react"; import type { NodeParamSpec } from "../lib/nodeCatalog"; import { TipIcon } from "./TipIcon"; /** * Inline parameter editor for a canvas node. * * Schema-driven: takes a `NodeParamSpec[]` from the node's catalog * entry and renders one labelled control per param. Exposes the * runtime values via the parent's `params` map and pushes edits back * through `onChange`. Supports two control types: * * - `select` — native `` with min/max/step * constraints. Edits are held as a local draft while typing and * committed (rounded for ints, clamped to the declared range) on * blur/Enter, so the stored value can never persist outside the * range — but typing "15" into a min-10 field works (audit S3: * the old per-keystroke clamp snapped the leading "1" to 10, and * an emptied field silently diverged from the stored value). * * Hints don't render inline — they live on a small ⓘ icon next to the * label, surfaced as a native browser tooltip on hover. Keeps the * block compact while still letting plain-language explanations be * arbitrarily long. * * `nodrag` on the wrapper is critical: without it, dragging a select * to make a choice or selecting text in the input would be intercepted * by React Flow as a node-move gesture. */ export function NodeParamEditor({ spec, values, onChange, disabled = false, }: { spec: NodeParamSpec[]; values: Record; onChange: (patch: Record) => void; /** Authoring lock (audit S2): true while a run is in progress — * every control renders disabled so params can't drift away from * the graph that is actually executing. */ disabled?: boolean; }) { // No border-t here: QNode's params section already draws the seam // above its header row — a second rule doubled it (audit S3). return (
{spec.map((p) => p.type === "select" ? ( onChange({ [p.key]: v })} disabled={disabled} /> ) : ( onChange({ [p.key]: v })} disabled={disabled} /> ), )}
); } function SelectField({ spec, value, onChange, disabled = false, }: { spec: Extract; value: string; onChange: (v: string) => void; disabled?: boolean; }) { const known = spec.options.some((o) => o.value === value); return ( ); } function NumberField({ spec, value, onChange, disabled = false, }: { spec: Extract; value: number; onChange: (v: number) => void; disabled?: boolean; }) { const safe = Number.isFinite(value) ? value : (spec.min ?? 0); const precision = spec.displayPrecision ?? (spec.type === "int" ? 0 : 2); // Draft-while-typing; null = field mirrors the stored value. const [draft, setDraft] = useState(null); const commit = () => { if (draft == null) return; setDraft(null); const raw = parseFloat(draft); if (!Number.isFinite(raw)) return; // unparseable → revert to stored let next = spec.type === "int" ? Math.round(raw) : raw; if (spec.min !== undefined) next = Math.max(spec.min, next); if (spec.max !== undefined) next = Math.min(spec.max, next); onChange(next); }; // Show range hint underneath if both bounds are set. const rangeText = spec.min !== undefined && spec.max !== undefined ? `${formatNum(spec.min, precision)} – ${formatNum(spec.max, precision)}` : null; return ( ); } /** Field label with a small ⓘ icon when the param spec carries a hint. * Tooltip behaviour lives in the shared `TipIcon` component. */ function FieldLabel({ label, hint, }: { label: string; hint?: string; }) { return ( {label} {hint && } ); } function formatNum(n: number, precision: number): string { if (!Number.isFinite(n)) return "—"; return n.toFixed(precision); }