// Per-device settings of an app, rendered from its manifest (plan steps 3.1a, 3.2a): the // fields come from GET /api/platform/modules → device_app.config, the texts from // modules:.config..{label,hint,choices.,none} and section headings from // modules:.config_groups.. The backend validates them (validation.device_config). // Used by the pair and edit dialogs of DevicesPage. import { useEffect, useState } from 'react' import { useTranslation } from '@xenia/i18n' import { Field, Input, Select, Switch } from '@xenia/ui' import client from '../../api/client' import { configGroups, configValue, refOptions } from './devicesLogic' // Where a `ref` field's choices come from (the device list is passed in) const REF_URLS = { prep_zones: '/api/prep-zones', categories: '/api/products/categories', printers: '/api/system/printers', display_looks: '/api/display/looks', signage_playlists: '/api/signage/playlists' } export default function DeviceConfigFields({ appKey, fields = [], value, onChange, devices = [], selfId = null }) { const { t } = useTranslation('manager') const sources = [...new Set(fields.map(f => f.choices_from).filter(s => s && REF_URLS[s]))].join(',') const [lists, setLists] = useState({}) useEffect(() => { if (!sources) return undefined let alive = true for (const s of sources.split(',')) { client.get(REF_URLS[s]).then(r => { if (alive) setLists(l => ({ ...l, [s]: r.data })) }).catch(() => {}) } return () => { alive = false } }, [sources]) const zones = lists.prep_zones ?? [] const categories = lists.categories ?? [] const printers = lists.printers ?? [] const looks = lists.display_looks ?? [] const playlists = lists.signage_playlists ?? [] if (!fields.length) return null const base = key => `modules:${appKey}.config.${key}` const set = (f, raw) => onChange({ ...value, [f.key]: configValue(f, raw) }) const control = f => { const label = t(`${base(f.key)}.label`, { defaultValue: f.key }) const hint = t(`${base(f.key)}.hint`, { defaultValue: '' }) || undefined const v = value?.[f.key] if (f.type === 'bool') { return set(f, on)} label={label} description={hint} data-config={f.key} /> } if (f.choices?.length) { return ( set(f, e.target.value)} data-config={f.key} options={[{ value: '', label: t(`${base(f.key)}.none`, { defaultValue: t('devices.form.none') }) }, ...refOptions(f, { zones, categories, printers, looks, playlists, devices, selfId })]} /> ) } if (f.type === 'color') { return ( set(f, e.target.value)} data-config={f.key} className="h-10 w-16 rounded-control border border-line bg-surface cursor-pointer" /> ) } return ( set(f, e.target.value)} data-config={f.key} /> ) } const groups = configGroups(fields) return (
{groups.map(g => (
{g.group && groups.length > 1 && g.fields.length > 1 && (

{t(`modules:${appKey}.config_groups.${g.group}`, { defaultValue: g.group })}

)} {g.fields.map(control)}
))}
) }