mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-11-17 20:22:20 -05:00
use shared schedma editortoritor
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import Form from '@rjsf/core';
|
||||
import validator from '@rjsf/validator-ajv8';
|
||||
import SchemaConfigurationForm from './SchemaConfigurationForm.jsx';
|
||||
|
||||
function request(socket, event, payload = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -12,57 +11,12 @@ function request(socket, event, payload = {}) {
|
||||
});
|
||||
}
|
||||
|
||||
function label(key) { return key.replace(/([a-z])([A-Z])/g, '$1 $2').replace(/^./, (c) => c.toUpperCase()); }
|
||||
|
||||
function fieldUI(schema, path = []) {
|
||||
const ui = { 'ui:title': path.length ? label(path.at(-1)) : '', 'ui:options': { path } };
|
||||
if (schema.type === 'string') ui['ui:emptyValue'] = '';
|
||||
for (const [key, child] of Object.entries(schema.properties || {})) ui[key] = fieldUI(child, [...path, key]);
|
||||
return ui;
|
||||
}
|
||||
|
||||
function FieldLayout({ id, label: title, children, errors, help, description, schema, uiSchema, registry }) {
|
||||
const path = uiSchema?.['ui:options']?.path || [];
|
||||
const object = schema.type === 'object';
|
||||
return <div className={object ? 'min-w-0' : 'space-y-2 py-3 border-b border-slate-700/50'}>
|
||||
{!object && <div className="flex items-center justify-between gap-3">
|
||||
<label htmlFor={id} className="font-medium text-sm">{title}</label>
|
||||
<button type="button" className="text-xs text-sky-300 hover:underline disabled:opacity-40"
|
||||
disabled={registry.formContext.busy || !registry.formContext.online}
|
||||
onClick={() => registry.formContext.applyField(path)}>Apply to all rovers</button>
|
||||
</div>}
|
||||
{description}{children}{errors}{help}
|
||||
</div>;
|
||||
}
|
||||
|
||||
function ObjectLayout({ title, properties, uiSchema, registry }) {
|
||||
const path = uiSchema?.['ui:options']?.path || [];
|
||||
const content = <div className="space-y-3">{properties.map((property) => <div key={property.name}>{property.content}</div>)}
|
||||
{path.length === 2 && path[0] === 'media' && path[1] !== 'rtspPort'
|
||||
&& <MediaProfiles {...registry.formContext} category={path[1]} />}
|
||||
</div>;
|
||||
if (!path.length) return content;
|
||||
return <details className="rounded-lg border border-slate-700 bg-slate-900/40 p-4" open={path[0] === 'media'}>
|
||||
<summary className="cursor-pointer font-semibold mb-3">{title}</summary>{content}
|
||||
</details>;
|
||||
}
|
||||
|
||||
// Required boolean properties may be false; HTML required checkboxes may not.
|
||||
function BooleanInput({ id, value, disabled, readonly, onChange }) {
|
||||
return <input id={id} type="checkbox" checked={Boolean(value)} disabled={disabled || readonly}
|
||||
onChange={(event) => onChange(event.target.checked)} className="h-5 w-5 accent-sky-500" />;
|
||||
}
|
||||
const widgets = { CheckboxWidget: BooleanInput };
|
||||
|
||||
const templates = { FieldTemplate: FieldLayout, ObjectFieldTemplate: ObjectLayout };
|
||||
|
||||
function RoverForm({ socket, record, online }) {
|
||||
const [draft, setDraft] = useState(() => structuredClone(record.config));
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [message, setMessage] = useState('');
|
||||
const [results, setResults] = useState([]);
|
||||
const schema = structuredClone(record.schema);
|
||||
const uiSchema = fieldUI(schema);
|
||||
for (const category of ['video', 'audioCapture', 'audioPlayback']) {
|
||||
const property = schema.properties?.media?.properties?.[category]?.properties?.profile;
|
||||
const profiles = record.profiles?.[category] || [];
|
||||
@@ -71,9 +25,7 @@ function RoverForm({ socket, record, online }) {
|
||||
const choices = profiles.map((profile) => ({ id: profile.id, name: profile.name }));
|
||||
const current = draft.media?.[category]?.profile;
|
||||
if (current && !choices.some((entry) => entry.id === current)) choices.push({ id: current, name: `${current} (unavailable)` });
|
||||
property.enum = choices.map((entry) => entry.id);
|
||||
uiSchema.media[category].profile['ui:widget'] = 'select';
|
||||
uiSchema.media[category].profile['ui:enumNames'] = choices.map((entry) => entry.name);
|
||||
property.oneOf = choices.map((entry) => ({ const: entry.id, title: entry.name }));
|
||||
}
|
||||
}
|
||||
async function save(path) {
|
||||
@@ -99,16 +51,13 @@ function RoverForm({ socket, record, online }) {
|
||||
{record.updateStatus && <p role="status">Update: {record.updateStatus.state} — {record.updateStatus.message}</p>}
|
||||
<p role="status">{message}</p>
|
||||
{results.length > 0 && <ul>{results.map((result) => <li key={result.roverId}>{result.roverId}: {result.success ? 'Saved' : result.error}</li>)}</ul>}
|
||||
<fieldset disabled={busy || !online} className="space-y-2">
|
||||
<Form schema={schema} uiSchema={uiSchema} templates={templates} widgets={widgets} noHtml5Validate
|
||||
formContext={{ socket, record, online, busy, configDirty: JSON.stringify(draft) !== JSON.stringify(record.config), applyField: save }}
|
||||
className="rover-config-form" formData={draft} validator={validator}
|
||||
onChange={({ formData }) => setDraft(formData)} onSubmit={() => save()}>
|
||||
<div className="sticky bottom-0 bg-slate-900 border-t border-slate-700 p-3 mt-4">
|
||||
<button type="submit" className="button-dark">Save configuration to this rover</button>
|
||||
</div>
|
||||
</Form>
|
||||
</fieldset>
|
||||
<SchemaConfigurationForm schema={schema} value={draft} onChange={setDraft} disabled={busy || !online}
|
||||
renderFieldAction={(path) => <button type="button" className="button-dark text-xs" disabled={busy || !online}
|
||||
onClick={() => save(path)}>Apply to all</button>}
|
||||
renderObjectFooter={(path) => path.length === 2 && path[0] === 'media' && record.profiles?.[path[1]]
|
||||
? <MediaProfiles socket={socket} record={record} online={online && !busy} category={path[1]}
|
||||
configDirty={JSON.stringify(draft) !== JSON.stringify(record.config)} /> : null} />
|
||||
<button type="button" className="button-dark text-xs" disabled={busy || !online} onClick={() => save()}>Save to this rover</button>
|
||||
</div>;
|
||||
}
|
||||
|
||||
@@ -128,10 +77,10 @@ function MediaProfiles({ socket, record, online, category, configDirty }) {
|
||||
} catch (error) { setMessage(error.message); }
|
||||
finally { setBusy(false); }
|
||||
}
|
||||
return <div className="space-y-3 pt-3">
|
||||
return <div className="space-y-0.5 text-xs">
|
||||
{(record.profiles?.[category] || []).map((entry) => <p key={entry.id}>{entry.name}: {entry.description}</p>)}
|
||||
<p>Status: {record.mediaStatus?.[category] || (online ? 'Waiting for status' : 'Offline')}</p>
|
||||
<details className="rounded border border-slate-600 p-3">
|
||||
<details className="space-y-0.5">
|
||||
<summary className="cursor-pointer">Create or edit custom profile</summary>
|
||||
{configDirty && <p className="text-sm text-amber-300">Save configuration changes before saving a custom profile, which restarts the rover.</p>}
|
||||
<fieldset disabled={!online || busy || configDirty}>
|
||||
@@ -200,8 +149,8 @@ export default function RoverConfigurationEditor({ socket, selected, onSelect })
|
||||
}, [socket]);
|
||||
const id = selected || Object.keys(records)[0] || '';
|
||||
const record = records[id];
|
||||
return <div id="fleet-configuration" className="space-y-4 scroll-mt-4">
|
||||
<h2 className="text-lg font-semibold">Configure rover</h2>
|
||||
return <div id="fleet-configuration" className="space-y-0.5 scroll-mt-4">
|
||||
<h2 className="text-sm font-semibold">Configure rover</h2>
|
||||
{error && <p role="alert">{error}</p>}
|
||||
<label>Rover <select className="bg-slate-900 text-slate-100" value={id} onChange={(event) => onSelect(event.target.value)}>
|
||||
{!id && <option value="">No management connections</option>}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { createContext, useContext, useMemo } from 'react';
|
||||
import Form from '@rjsf/core';
|
||||
import validator from '@rjsf/validator-ajv8';
|
||||
import CardFrame from '../../components/CardFrame/index.jsx';
|
||||
import '../styles.css';
|
||||
|
||||
/*
|
||||
Structural colors repeat only after six levels, which is deeper than the
|
||||
@@ -77,6 +78,8 @@ function buildUiSchema(schema, path = '') {
|
||||
};
|
||||
}
|
||||
|
||||
if (schema.type === 'string') placeholder['ui:emptyValue'] = '';
|
||||
if (schema.oneOf?.every((option) => Object.hasOwn(option, 'const'))) placeholder['ui:widget'] = 'select';
|
||||
if (schema.type !== 'object' || !schema.properties) return placeholder;
|
||||
return Object.fromEntries(Object.entries(schema.properties).map(([key, childSchema]) => [
|
||||
key,
|
||||
@@ -142,6 +145,8 @@ function ConfigurationFieldTemplate({
|
||||
required,
|
||||
schema,
|
||||
style,
|
||||
fieldPathId,
|
||||
registry,
|
||||
}) {
|
||||
if (hidden) return <div className="hidden">{children}</div>;
|
||||
|
||||
@@ -161,6 +166,7 @@ function ConfigurationFieldTemplate({
|
||||
<label htmlFor={id} className="configuration-key-label">
|
||||
{label}{required ? <span className="ml-0.25 text-sky-300">*</span> : null}
|
||||
</label>
|
||||
{registry.formContext.renderFieldAction?.(fieldPathId.path)}
|
||||
</div>
|
||||
<div className="configuration-value">
|
||||
{/* Descriptions belong with the editable value rather than inside the
|
||||
@@ -175,7 +181,7 @@ function ConfigurationFieldTemplate({
|
||||
);
|
||||
}
|
||||
|
||||
function ConfigurationObjectTemplate({ description, fieldPathId, properties, title }) {
|
||||
function ConfigurationObjectTemplate({ description, fieldPathId, properties, title, registry }) {
|
||||
const visibleProperties = properties.filter((property) => !property.hidden);
|
||||
const propertyLines = visibleProperties.map((property) => property.content);
|
||||
const topLevel = fieldPathId.path.length === 1;
|
||||
@@ -226,6 +232,7 @@ function ConfigurationObjectTemplate({ description, fieldPathId, properties, tit
|
||||
{description ? <div className="configuration-branch-description">{description}</div> : null}
|
||||
<ConfigurationLayerDepthContext.Provider value={layer.depth + 1}>
|
||||
<div className="configuration-children">{propertyLines}</div>
|
||||
{registry.formContext.renderObjectFooter?.(fieldPathId.path)}
|
||||
</ConfigurationLayerDepthContext.Provider>
|
||||
</CardFrame>
|
||||
);
|
||||
@@ -304,7 +311,7 @@ function ConfigurationArrayTemplate({ canAdd, disabled, fieldPathId, items, onAd
|
||||
);
|
||||
}
|
||||
|
||||
export default function SchemaConfigurationForm({ schema, value, onChange, configuredSecrets, secretOperations, setSecretOperation }) {
|
||||
export default function SchemaConfigurationForm({ schema, value, onChange, configuredSecrets, secretOperations, setSecretOperation, disabled = false, renderFieldAction, renderObjectFooter }) {
|
||||
const uiSchema = useMemo(() => buildUiSchema(schema), [schema]);
|
||||
const widgets = useMemo(() => ({ SecretWidget }), []);
|
||||
const templates = useMemo(() => ({
|
||||
@@ -317,11 +324,14 @@ export default function SchemaConfigurationForm({ schema, value, onChange, confi
|
||||
configuredSecrets,
|
||||
secretOperations,
|
||||
setSecretOperation,
|
||||
}), [configuredSecrets, secretOperations, setSecretOperation]);
|
||||
renderFieldAction,
|
||||
renderObjectFooter,
|
||||
}), [configuredSecrets, secretOperations, setSecretOperation, renderFieldAction, renderObjectFooter]);
|
||||
|
||||
return (
|
||||
<div className="configuration-schema-form">
|
||||
<Form
|
||||
disabled={disabled}
|
||||
schema={schema}
|
||||
uiSchema={uiSchema}
|
||||
formData={value}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* Administration Application Styles
|
||||
* Purpose: Styles the schema-generated configuration tree used only by /admin.
|
||||
* Scope: Keeps route-specific hierarchy and RJSF integration out of the shared global stylesheet.
|
||||
* Purpose: Styles the schema-generated configuration tree shared by server and rover configuration.
|
||||
* Scope: Keeps configuration hierarchy and RJSF integration out of the shared global stylesheet.
|
||||
*/
|
||||
|
||||
/* Reuse the shared theme without emitting a second copy of Tailwind. */
|
||||
|
||||
@@ -92,21 +92,3 @@ body::-webkit-scrollbar,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Schema controls share the surrounding fleet UI's input treatment. */
|
||||
.rover-config-form input:not([type="checkbox"]),
|
||||
.rover-config-form select,
|
||||
.rover-config-form textarea {
|
||||
width: 100%;
|
||||
min-width: 0;
|
||||
padding: 0.5rem 0.75rem;
|
||||
border: 1px solid #475569;
|
||||
border-radius: 0.375rem;
|
||||
background: #0f172a;
|
||||
color: #e2e8f0;
|
||||
}
|
||||
.rover-config-form input:focus-visible,
|
||||
.rover-config-form select:focus-visible,
|
||||
.rover-config-form textarea:focus-visible { outline: 2px solid #38bdf8; outline-offset: 2px; }
|
||||
.rover-config-form .text-danger,
|
||||
.rover-config-form .error-detail { color: #fca5a5; }
|
||||
|
||||
Reference in New Issue
Block a user