-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathConfigTabContent.tsx
More file actions
340 lines (319 loc) · 10.9 KB
/
Copy pathConfigTabContent.tsx
File metadata and controls
340 lines (319 loc) · 10.9 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { useMemo } from 'react';
import { MultiAccordion } from '@clickhouse/click-ui';
import type * as t from '@/types';
import { SECTION_RENDERERS, SELF_CONTAINED_SECTION_RENDERERS } from './sections';
import { FieldRenderer, SingleFieldRenderer } from './FieldRenderer';
import { ConfigSection } from './ConfigSection';
import { CodeField } from './fields/CodeField';
import { isSectionDisabled } from '@/utils';
import { InfoBanner } from './InfoBanner';
import { hasDescendant } from './utils';
import { useLocalize } from '@/hooks';
/** Sections where the configured count should reflect record entries rather
* than leaf schema paths. Add section IDs here for any record-type section
* whose schema describes the value shape (not the entry keys). */
const RECORD_ENTRY_COUNT_SECTIONS = new Set(['mcpServers']);
function isSimpleScalar(f: t.SchemaField): boolean {
return !f.children?.length && !f.isArray && f.type !== 'record' && f.type !== 'object';
}
function countLeafFields(fields: t.SchemaField[], parentPath: string): string[] {
const paths: string[] = [];
for (const field of fields) {
const path = `${parentPath}.${field.key}`;
if (field.children && field.children.length > 0) {
paths.push(...countLeafFields(field.children, path));
} else {
paths.push(path);
}
}
return paths;
}
export function ConfigTabContent({
sections,
configValues,
editedValues,
onFieldChange,
onResetField,
profileMap,
previewMode,
previewScope,
previewChangedPaths,
resolvedValues,
permissions,
onProfileChange,
showChangedOnly,
readOnly,
configuredPaths,
dbOverridePaths,
touchedPaths,
pendingResets,
sectionPermissions,
schemaDefaults,
showConfiguredOnly,
baseRecordKeys,
onValidationError,
}: t.ConfigTabContentProps) {
const localize = useLocalize();
const fieldsDisabled = readOnly;
const getValue = (path: string, fallback: t.ConfigValue): t.ConfigValue => {
if (path in editedValues) {
if (editedValues[path] === undefined) return schemaDefaults?.[path] ?? fallback;
return editedValues[path];
}
if (resolvedValues && path in resolvedValues) {
return resolvedValues[path];
}
return fallback;
};
const filtering = showChangedOnly;
const sectionHasChanges = useMemo(() => {
if (!filtering || !previewChangedPaths) return null;
const pathSet = new Set(previewChangedPaths);
const result: Record<string, boolean> = {};
for (const section of sections) {
result[section.id] = section.sectionField
? pathSet.has(section.id)
: hasChangedDescendant(section.fields, section.id, pathSet);
}
return result;
}, [filtering, previewChangedPaths, sections]);
const sectionCounts = useMemo(() => {
return sections.map((s) => {
const key = s.schemaKey ?? s.id;
// Record-type sections (e.g. mcpServers): count entries in the record
// value rather than leaf schema paths, since the schema describes the
// *value* shape while configured paths include dynamic entry keys
// (mcpServers.cloudflare.type, not mcpServers.type).
if (RECORD_ENTRY_COUNT_SECTIONS.has(s.id) && configValues) {
const sectionValue = configValues[key];
if (sectionValue && typeof sectionValue === 'object' && !Array.isArray(sectionValue)) {
const count = Object.keys(sectionValue as Record<string, unknown>).length;
return { id: s.id, total: count, configured: count };
}
}
const leafPaths = s.sectionField ? [key] : countLeafFields(s.fields, key);
if (!configuredPaths || configuredPaths.size === 0) {
return { id: s.id, total: leafPaths.length, configured: 0 };
}
let configured = 0;
for (const p of leafPaths) {
if (configuredPaths.has(p) || hasDescendant(p, configuredPaths)) configured++;
}
return { id: s.id, total: leafPaths.length, configured };
});
}, [sections, configuredPaths, configValues]);
const countsById = useMemo(() => {
const map: Record<string, { total: number; configured: number }> = {};
for (const c of sectionCounts) {
map[c.id] = { total: c.total, configured: c.configured };
}
return map;
}, [sectionCounts]);
if (sections.length === 0) {
return (
<div className="py-6">
<CodeField
id="config-raw"
value={configValues}
onChange={(v) => onFieldChange('_root', v)}
/>
</div>
);
}
let visibleSections =
filtering && sectionHasChanges ? sections.filter((s) => sectionHasChanges[s.id]) : sections;
if (showConfiguredOnly && !previewMode) {
visibleSections = visibleSections.filter((s) => {
const counts = countsById[s.id];
return counts && counts.configured > 0;
});
}
if ((filtering || showConfiguredOnly) && visibleSections.length === 0) {
return (
<div className="flex items-center justify-center py-16 text-(--cui-color-text-muted)">
<span className="text-sm">
{localize(filtering ? 'com_scope_no_changed_in_tab' : 'com_config_no_fields')}
</span>
</div>
);
}
const renderSectionContent = (section: t.ConfigSectionConfig) => {
const dataKey = section.schemaKey ?? section.id;
const sectionValue = configValues?.[dataKey];
const sectionDisabled = isSectionDisabled(!!fieldsDisabled, sectionPermissions, dataKey);
const CustomSectionRenderer =
section.fields.length > 0 ? SECTION_RENDERERS[section.id] : undefined;
const fieldRendererProps: t.FieldRendererProps = {
fields: section.fields,
parentValue: sectionValue,
parentPath: dataKey,
getValue,
onChange: onFieldChange,
onResetField,
editedValues,
disabled: sectionDisabled,
profileMap,
previewMode,
previewScope,
previewChangedPaths,
resolvedValues,
permissions,
onProfileChange,
showChangedOnly,
configuredPaths,
dbOverridePaths,
touchedPaths,
pendingResets,
schemaDefaults,
showConfiguredOnly,
yamlBaseKeys: baseRecordKeys?.[dataKey],
onValidationError,
};
return (
<>
{section.bannerText && (
<div className="mb-4">
<InfoBanner text={section.bannerText} />
</div>
)}
{section.fields.length > 0 &&
(CustomSectionRenderer ? (
<CustomSectionRenderer {...fieldRendererProps} />
) : (
<FieldRenderer {...fieldRendererProps} />
))}
{section.fields.length === 0 && section.sectionField && (
<SingleFieldRenderer
field={section.sectionField}
value={sectionValue}
path={section.id}
getValue={getValue}
onChange={onFieldChange}
onResetField={onResetField}
disabled={sectionDisabled}
permissions={permissions}
onProfileChange={onProfileChange}
previewMode={previewMode}
previewScope={previewScope}
previewChangedPaths={previewChangedPaths}
resolvedValues={resolvedValues}
configuredPaths={configuredPaths}
dbOverridePaths={dbOverridePaths}
touchedPaths={touchedPaths}
pendingResets={pendingResets}
schemaDefaults={schemaDefaults}
showConfiguredOnly={showConfiguredOnly}
isSoleField={!section.sectionField.isArray && section.sectionField.type !== 'record'}
/>
)}
{section.fields.length === 0 && !section.sectionField && (
<CodeField
id={`${section.id}-raw`}
value={sectionValue}
onChange={(v) => onFieldChange(section.id, v)}
disabled={sectionDisabled}
/>
)}
</>
);
};
const isInlineSection = (section: t.ConfigSectionConfig): boolean =>
Boolean(
(section.sectionField && isSimpleScalar(section.sectionField)) ||
(section.fields.length === 1 && isSimpleScalar(section.fields[0])),
);
type SectionGroup =
| { kind: 'inline'; section: t.ConfigSectionConfig }
| { kind: 'accordion'; sections: t.ConfigSectionConfig[] }
| { kind: 'flat'; section: t.ConfigSectionConfig };
const groups = visibleSections.reduce<SectionGroup[]>((acc, section) => {
if (SELF_CONTAINED_SECTION_RENDERERS.has(section.id)) {
acc.push({ kind: 'flat', section });
return acc;
}
if (isInlineSection(section)) {
acc.push({ kind: 'inline', section });
return acc;
}
const last = acc[acc.length - 1];
if (last?.kind === 'accordion') {
last.sections.push(section);
} else {
acc.push({ kind: 'accordion', sections: [section] });
}
return acc;
}, []);
return (
<form
aria-label={localize('com_nav_configuration')}
onSubmit={(e) => e.preventDefault()}
className="flex flex-col gap-6 py-4"
>
{groups.map((group) => {
if (group.kind === 'flat') {
return (
<div key={group.section.id} id={`section-${group.section.id}`}>
{renderSectionContent(group.section)}
</div>
);
}
if (group.kind === 'inline') {
const { section } = group;
const counts = countsById[section.id];
return (
<ConfigSection
key={section.id}
sectionId={section.id}
title={localize(section.titleKey)}
description={section.descriptionKey ? localize(section.descriptionKey) : undefined}
learnMoreUrl={section.learnMoreUrl}
configuredCount={counts?.configured ?? 0}
totalCount={counts?.total ?? 0}
inline
showConfiguredOnly={showConfiguredOnly}
>
{renderSectionContent(section)}
</ConfigSection>
);
}
return (
<MultiAccordion
key={group.sections[0].id}
type="multiple"
showBorder
showCheck={false}
fillWidth
defaultValue={group.sections.map((s) => s.id)}
data-top-level-accordion
>
{group.sections.map((section) => (
<MultiAccordion.Item
key={section.id}
id={`section-${section.id}`}
data-section-id={`section-${section.id}`}
value={section.id}
title={localize(section.titleKey)}
>
{renderSectionContent(section)}
</MultiAccordion.Item>
))}
</MultiAccordion>
);
})}
</form>
);
}
function hasChangedDescendant(
fields: t.SchemaField[],
parentPath: string,
pathSet: Set<string>,
): boolean {
for (const field of fields) {
const path = `${parentPath}.${field.key}`;
if (pathSet.has(path)) return true;
if (field.children && field.children.length > 0) {
if (hasChangedDescendant(field.children, path, pathSet)) return true;
}
}
return false;
}