-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFlowNodeConfigField.tsx
More file actions
233 lines (226 loc) · 9.07 KB
/
Copy pathFlowNodeConfigField.tsx
File metadata and controls
233 lines (226 loc) · 9.07 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* FlowNodeConfigField — renders one scalar config control for a flow node,
* driven by a `FlowConfigField` descriptor. Bridges descriptor "kind" to the
* shared inspector field primitives and writes back to `node.config[key]`.
*/
import * as React from 'react';
import type { FlowConfigField } from './flow-node-config';
import { t } from '../i18n';
import {
InspectorNumberField,
InspectorSelectField,
InspectorCheckboxField,
} from './_shared';
import { Label } from '@object-ui/components';
import { FlowKeyValueField } from './FlowKeyValueField';
import { FlowStringListField } from './FlowStringListField';
import { FlowObjectListField } from './FlowObjectListField';
import { FlowReferenceField, type FlowReferenceContext } from './FlowReferenceField';
import { validateExpressionClient } from './expression-validate';
import { VariableTextInput } from './VariableTextInput';
import type { ScopeGroup } from './useFlowScope';
import { findUnknownRefs, scopeRoots, describeUnknownRefs } from './flow-ref-check';
export interface FlowNodeConfigFieldProps {
field: FlowConfigField;
value: unknown;
onCommit: (value: unknown) => void;
disabled?: boolean;
locale?: string;
/** Draft + node context so `reference` fields can resolve their options. */
context?: FlowReferenceContext;
/** In-scope variable references for the data-picker (#1934). */
scopeGroups?: ScopeGroup[];
/** #3447: approval-expression picker groups (current/trigger/vars roots). */
approvalScopeGroups?: ScopeGroup[];
}
export function FlowNodeConfigField({ field, value, onCommit, disabled, locale, context, scopeGroups, approvalScopeGroups }: FlowNodeConfigFieldProps) {
const refMode: 'expression' | 'template' =
field.refMode ?? (field.kind === 'expression' ? 'expression' : 'template');
const control = (() => {
switch (field.kind) {
case 'reference':
return (
<FlowReferenceField
field={field}
value={value}
onCommit={(v) => onCommit(v)}
disabled={disabled}
context={context}
/>
);
case 'keyValue':
return (
<FlowKeyValueField
label={field.label}
value={value}
onCommit={(v) => onCommit(v)}
disabled={disabled}
addLabel={t('engine.inspector.flowNode.kv.add', locale)}
keyLabel={t('engine.inspector.flowNode.kv.key', locale)}
valueLabel={t('engine.inspector.flowNode.kv.value', locale)}
removeLabel={t('engine.inspector.flowNode.kv.remove', locale)}
emptyLabel={t('engine.inspector.flowNode.kv.empty', locale)}
scopeGroups={scopeGroups}
/>
);
case 'stringList':
return (
<FlowStringListField
label={field.label}
value={value}
onCommit={(v) => onCommit(v)}
disabled={disabled}
addLabel={t('engine.inspector.flowNode.list.add', locale)}
itemLabel={t('engine.inspector.flowNode.list.item', locale)}
removeLabel={t('engine.inspector.flowNode.list.remove', locale)}
emptyLabel={t('engine.inspector.flowNode.list.empty', locale)}
/>
);
case 'numberList':
return (
<FlowStringListField
label={field.label}
// Stored as number[]; the list editor works in strings, so show each
// number as text and coerce back to number[] on commit (dropping
// blanks / non-numbers). Keeps the backend contract strict (number[])
// rather than persisting string values the schema would reject.
value={Array.isArray(value) ? (value as unknown[]).map((n) => String(n)) : value}
onCommit={(v) => {
if (v == null) return onCommit(undefined);
const nums = v.map((s) => Number(String(s).trim())).filter((n) => Number.isFinite(n));
onCommit(nums.length ? nums : undefined);
}}
disabled={disabled}
addLabel={t('engine.inspector.flowNode.list.add', locale)}
itemLabel={t('engine.inspector.flowNode.list.item', locale)}
removeLabel={t('engine.inspector.flowNode.list.remove', locale)}
emptyLabel={t('engine.inspector.flowNode.list.empty', locale)}
/>
);
case 'objectList':
return (
<FlowObjectListField
label={field.label}
columns={field.columns ?? []}
value={value}
onCommit={(v) => onCommit(v)}
disabled={disabled}
addLabel={t('engine.inspector.flowNode.list.add', locale)}
removeLabel={t('engine.inspector.flowNode.list.remove', locale)}
emptyLabel={t('engine.inspector.flowNode.list.empty', locale)}
itemLabel={t('engine.inspector.flowNode.list.item', locale)}
context={context}
scopeGroups={scopeGroups}
approvalScopeGroups={approvalScopeGroups}
/>
);
case 'number':
return (
<InspectorNumberField
label={field.label}
value={typeof value === 'number' ? value : value != null && value !== '' ? Number(value) : undefined}
placeholder={field.placeholder}
onCommit={(v) => onCommit(v)}
disabled={disabled}
/>
);
case 'boolean':
return (
<InspectorCheckboxField
label={field.label}
value={value === true}
onCommit={(v) => onCommit(v)}
disabled={disabled}
/>
);
case 'select':
return (
<InspectorSelectField
label={field.label}
value={value != null ? String(value) : ''}
options={field.options ?? []}
onCommit={(v) => onCommit(v)}
disabled={disabled}
/>
);
case 'textarea':
return (
<div className="space-y-1">
<Label className="text-xs text-muted-foreground">{field.label}</Label>
<VariableTextInput
multiline
rows={4}
mode={refMode}
value={value != null ? String(value) : ''}
onValueChange={(v) => onCommit(v)}
groups={scopeGroups ?? []}
placeholder={field.placeholder}
disabled={disabled}
/>
</div>
);
case 'expression':
case 'text':
default:
return (
<div className="space-y-1">
<Label className="text-xs text-muted-foreground">{field.label}</Label>
<VariableTextInput
mode={refMode}
mono={field.kind === 'expression'}
value={value != null ? String(value) : ''}
onValueChange={(v) => onCommit(v)}
groups={scopeGroups ?? []}
placeholder={field.placeholder}
disabled={disabled}
/>
</div>
);
}
})();
// ADR-0032 — surface a malformed condition (e.g. the `{record.x}` brace-in-CEL
// mistake) inline, with the same corrective message the build/agent emit. Only
// for expression fields in a *predicate* mode — an expression field flagged
// `refMode: 'template'` (e.g. a loop/map collection authored as `{leadList}`)
// is an `interpolate()` single-brace template where `{var}` is legal, so the
// CEL brace-trap must be gated off or it false-positives on every `{…}`.
const isTemplate = refMode === 'template';
const exprIssue =
field.kind === 'expression' && !isTemplate ? validateExpressionClient('predicate', value) : null;
// #1934 — pair the picker with a gentle, scope-aware "unknown reference"
// warning: CEL for predicate expression fields, `{…}` holes for template
// fields (including an expression field in template mode). Skipped for
// free-form code (refMode 'expression' on a textarea, e.g. a script body) and
// when scope is unknown. The brace error above takes precedence.
const scopeRole: 'predicate' | 'template' | null =
field.kind === 'expression'
? isTemplate
? 'template'
: 'predicate'
: refMode === 'template' && (field.kind === 'text' || field.kind === 'textarea')
? 'template'
: null;
const unknownRefs =
!exprIssue && scopeRole && scopeGroups && scopeGroups.length > 0
? findUnknownRefs(value, scopeRole, scopeRoots(scopeGroups.flatMap((g) => g.refs)))
: [];
return (
<div className="space-y-1">
{control}
{exprIssue && (
<p className="text-[11px] leading-snug text-destructive" role="alert">
{exprIssue.message}
</p>
)}
{!exprIssue && unknownRefs.length > 0 && (
<p className="text-[11px] leading-snug text-amber-600 dark:text-amber-400" role="note">
{describeUnknownRefs(unknownRefs, locale)}
</p>
)}
{field.help && !exprIssue && unknownRefs.length === 0 && (
<p className="text-[11px] leading-snug text-muted-foreground">{field.help}</p>
)}
</div>
);
}