Files
Reims-Go/frontend/src/components/SchemaForm.tsx
T
NwflowerandClaude Opus 5.5 50c03a0835 perf(frontend): load the settings and plugin texts on demand again
App.tsx imported a one-line helper from SchemaForm, which pulled both text
domains into the startup bundle. The i18n test now follows the real import
graph instead of a hand-kept file list, so it catches this. npm test runs
every test file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 21:38:17 -07:00

170 lines
5.2 KiB
TypeScript

import React from 'react'
import '../i18n/domains/plugins.gen'
import '../i18n/domains/settings.gen'
import { locText, t } from '../i18n'
import { Locale, SettingProperty, SettingsSchema } from '../types'
import { NumberField, PopupSelect, Segmented, SetGroup, SetRow, Switch } from '../ui/settings'
interface Props {
locale: Locale
schema: SettingsSchema
values: Record<string, unknown>
scopeFilter?: 'app' | 'vm'
groupFilter?: string
onChange: (next: Record<string, unknown>, patch: Record<string, unknown>) => void
hideGroupHeader?: boolean
}
const SECRET_MASK = '••••••••'
const FORBIDDEN_KEYS = new Set(['__proto__', 'constructor', 'prototype'])
/** Lazy-loaded JSON Schema form renderer supporting x-secret, x-effect, and x-scope. */
export const SchemaForm: React.FC<Props> = ({
locale, schema, values, scopeFilter, groupFilter, onChange, hideGroupHeader,
}) => {
const propsMap = schema.properties ?? {}
const keys = (schema['x-order'] && schema['x-order'].length > 0
? schema['x-order'].filter((k) => !FORBIDDEN_KEYS.has(k) && Object.prototype.hasOwnProperty.call(propsMap, k))
: Object.keys(propsMap).filter((k) => !FORBIDDEN_KEYS.has(k))
).filter((k) => {
const prop = propsMap[k]
const effScope = prop['x-scope'] || schema['x-scope'] || 'app'
const effGroup = prop['x-group'] || schema['x-group'] || ''
if (scopeFilter && effScope !== scopeFilter && effScope !== 'both') return false
if (groupFilter && effGroup !== groupFilter) return false
return true
})
if (keys.length === 0) return null
const setKey = (k: string, v: unknown) => {
const next: Record<string, unknown> = {}
for (const [ek, ev] of Object.entries(values)) {
if (propsMap[ek]?.['x-secret'] && ev === SECRET_MASK) continue
next[ek] = ev
}
next[k] = v
onChange(next, { [k]: v })
}
const effectBadge = (prop: SettingProperty) => {
const eff = prop['x-effect']
if (!eff) return null
const label = t(locale, eff === 'immediate' ? 'effectImmediate' : 'effectNextStart')
return <span className={`badge rg-p-badge ${eff === 'immediate' ? 'badge-ok' : ''}`}>{label}</span>
}
const renderControl = (k: string, prop: SettingProperty) => {
const cur = values[k] ?? prop.default
const label = locText(prop.title, locale, k)
if (prop.type === 'boolean') {
return (
<Switch
label={label}
checked={Boolean(cur)}
onChange={(checked) => setKey(k, checked)}
/>
)
}
if (prop.enum && prop.enum.length > 0) {
const options = prop.enum.map((val, idx) => ({
value: val,
label: locText(prop['x-enumLabels']?.[idx], locale, val),
}))
if (options.length <= 3) {
return (
<Segmented
label={label}
value={String(cur ?? prop.enum[0])}
options={options}
onChange={(val) => setKey(k, val)}
/>
)
}
return (
<PopupSelect
label={label}
value={String(cur ?? prop.enum[0])}
options={options}
onChange={(val) => setKey(k, val)}
/>
)
}
if (prop.type === 'integer' || prop.type === 'number') {
const numVal = Number(cur ?? 0)
return (
<NumberField
label={label}
value={Number.isFinite(numVal) ? numVal : 0}
min={prop.minimum ?? 0}
max={prop.maximum ?? 65535}
step={prop.type === 'number' ? 0.1 : 1}
onChange={(n) => setKey(k, n)}
/>
)
}
if (prop['x-secret']) {
const isMasked = cur === SECRET_MASK
const hasSecret = isMasked || String(cur ?? '') !== ''
return (
<div className="input-group">
<input
key={`${k}:${hasSecret ? 'present' : 'empty'}`}
type="password"
className="input rg-p-input"
aria-label={label}
autoComplete="new-password"
placeholder={isMasked ? SECRET_MASK : undefined}
defaultValue={isMasked ? '' : String(cur ?? '')}
onBlur={(e) => {
const next = e.currentTarget.value
if (next !== (isMasked ? '' : String(cur ?? ''))) setKey(k, next)
}}
/>
{hasSecret && (
<button type="button" className="btn rg-p-button btn-ghost" onClick={() => setKey(k, '')}>
{t(locale, 'pluginClearSecret')}
</button>
)}
</div>
)
}
return (
<input
type="text"
className="input rg-p-input"
aria-label={label}
value={String(cur ?? '')}
onChange={(e) => setKey(k, e.target.value)}
/>
)
}
const rows = keys.map((k) => {
const prop = propsMap[k]
const title = locText(prop.title, locale, k)
const desc = locText(prop.description, locale)
const badge = effectBadge(prop)
return (
<SetRow
key={k}
title={title}
desc={badge || desc ? <span className="hstack">{badge}{desc ? <span>{desc}</span> : null}</span> : undefined}
control={renderControl(k, prop)}
/>
)
})
if (hideGroupHeader) return <>{rows}</>
return (
<SetGroup title={locText(schema.title, locale, t(locale, 'pluginSettings'))}>
{rows}
</SetGroup>
)
}