File size: 6,882 Bytes
728039f
94039f2
0f6846e
94039f2
 
 
 
 
 
 
 
 
 
 
 
 
 
728039f
 
 
 
 
 
94039f2
1619c45
 
 
 
 
94039f2
 
 
 
 
 
 
 
72fe4f4
94039f2
 
 
 
72fe4f4
 
 
 
94039f2
728039f
 
94039f2
728039f
94039f2
 
 
 
 
 
 
72fe4f4
94039f2
 
 
 
 
 
 
 
 
 
 
72fe4f4
94039f2
 
 
 
 
 
 
 
 
 
 
72fe4f4
94039f2
 
 
 
72fe4f4
94039f2
 
 
 
1619c45
94039f2
 
 
72fe4f4
 
 
94039f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72fe4f4
94039f2
 
 
 
72fe4f4
94039f2
 
 
 
728039f
 
 
 
 
 
 
 
 
 
 
 
94039f2
 
 
 
 
 
 
 
1619c45
 
94039f2
 
 
 
 
 
 
 
 
728039f
72fe4f4
 
728039f
 
 
 
94039f2
72fe4f4
94039f2
1619c45
 
 
cadac03
 
 
94039f2
 
 
 
 
 
1619c45
0f6846e
1619c45
 
 
 
 
 
 
 
 
 
0f6846e
1619c45
 
 
 
94039f2
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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 `<select>` with a known option list. If the
 *     stored value isn't recognised, an extra disabled "(unknown)"
 *     option surfaces it so the user can see what's stuck rather than
 *     silently snapping to a default.
 *   - `number` / `int` — `<input type="number">` 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<string, unknown>;
  onChange: (patch: Record<string, unknown>) => 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 (
    <div className="nodrag mt-2 space-y-2">
      {spec.map((p) =>
        p.type === "select" ? (
          <SelectField
            key={p.key}
            spec={p}
            value={String(values[p.key] ?? "")}
            onChange={(v) => onChange({ [p.key]: v })}
            disabled={disabled}
          />
        ) : (
          <NumberField
            key={p.key}
            spec={p}
            value={
              typeof values[p.key] === "number"
                ? (values[p.key] as number)
                : Number(values[p.key])
            }
            onChange={(v) => onChange({ [p.key]: v })}
            disabled={disabled}
          />
        ),
      )}
    </div>
  );
}

function SelectField({
  spec,
  value,
  onChange,
  disabled = false,
}: {
  spec: Extract<NodeParamSpec, { type: "select" }>;
  value: string;
  onChange: (v: string) => void;
  disabled?: boolean;
}) {
  const known = spec.options.some((o) => o.value === value);
  return (
    <label className="block text-[10px] text-mute">
      <FieldLabel label={spec.label} hint={spec.hint} />
      <select
        value={known ? value : ""}
        onChange={(e) => onChange(e.target.value)}
        disabled={disabled}
        title={disabled ? "Locked while a run is in progress" : undefined}
        className="w-full text-[11px] bg-surface border border-edge rounded px-1.5 py-0.5 text-ink focus:outline-none focus:border-accent/60 disabled:opacity-50 disabled:cursor-not-allowed"
      >
        {!known && (
          <option value="" disabled>
            {value || "—"} (unknown)
          </option>
        )}
        {spec.options.map((o) => (
          <option key={o.value} value={o.value}>
            {o.label}
          </option>
        ))}
      </select>
    </label>
  );
}

function NumberField({
  spec,
  value,
  onChange,
  disabled = false,
}: {
  spec: Extract<NodeParamSpec, { type: "number" | "int" }>;
  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<string | null>(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 (
    <label className="block text-[10px] text-mute">
      <span className="flex items-baseline justify-between mb-0.5 gap-1">
        <FieldLabel label={spec.label} hint={spec.hint} />
        <span className="font-mono text-ink">
          {formatNum(safe, precision)}
        </span>
      </span>
      <input
        type="number"
        min={spec.min}
        max={spec.max}
        step={spec.step}
        value={draft ?? safe}
        disabled={disabled}
        title={disabled ? "Locked while a run is in progress" : undefined}
        onChange={(e) => setDraft(e.target.value)}
        onBlur={commit}
        onKeyDown={(e) => {
          if (e.key === "Enter") commit();
        }}
        className="w-full text-[11px] bg-surface border border-edge rounded px-1.5 py-0.5 text-ink font-mono focus:outline-none focus:border-accent/60 disabled:opacity-50 disabled:cursor-not-allowed"
      />
      {/* Range stays inline as a small mono cue (concrete bounds are
          worth glancing at every time). Long-form hint hides behind the
          ⓘ icon in FieldLabel above. */}
      {rangeText && (
        <span className="block mt-0.5 text-[10px] text-mute/70 font-mono text-right">
          range {rangeText}
        </span>
      )}
    </label>
  );
}

/** 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 (
    <span className="inline-flex items-center gap-1 min-w-0">
      <span className="truncate">{label}</span>
      {hint && <TipIcon hint={hint} />}
    </span>
  );
}

function formatNum(n: number, precision: number): string {
  if (!Number.isFinite(n)) return "—";
  return n.toFixed(precision);
}