Media page and picker filter Everything / Your own / Built-in; built-in items are marked and can't be renamed or deleted. The Devices page offers the playlists for a signage screen (with its orientation and rotation). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
4.2 KiB
React
94 lines
4.2 KiB
React
// 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:<app>.config.<key>.{label,hint,choices.<value>,none} and section headings from
|
|
// modules:<app>.config_groups.<group>. 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 <Switch key={f.key} checked={!!v} onChange={on => set(f, on)} label={label} description={hint} data-config={f.key} />
|
|
}
|
|
if (f.choices?.length) {
|
|
return (
|
|
<Field key={f.key} label={label} hint={hint}>
|
|
<Select value={v == null ? '' : String(v)} onChange={e => set(f, e.target.value)} data-config={f.key}
|
|
options={f.choices.map(c => ({ value: String(c), label: t(`${base(f.key)}.choices.${c}`, { defaultValue: String(c) }) }))} />
|
|
</Field>
|
|
)
|
|
}
|
|
if (f.type === 'ref') {
|
|
return (
|
|
<Field key={f.key} label={label} hint={hint}>
|
|
<Select value={v == null ? '' : String(v)} onChange={e => 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 })]} />
|
|
</Field>
|
|
)
|
|
}
|
|
if (f.type === 'color') {
|
|
return (
|
|
<Field key={f.key} label={label} hint={hint}>
|
|
<input type="color" value={v || '#000000'} onChange={e => set(f, e.target.value)} data-config={f.key}
|
|
className="h-10 w-16 rounded-control border border-line bg-surface cursor-pointer" />
|
|
</Field>
|
|
)
|
|
}
|
|
return (
|
|
<Field key={f.key} label={label} hint={hint}>
|
|
<Input type={f.type === 'int' ? 'number' : 'text'} value={v ?? ''} min={f.min ?? undefined} max={f.max ?? undefined}
|
|
onChange={e => set(f, e.target.value)} data-config={f.key} />
|
|
</Field>
|
|
)
|
|
}
|
|
|
|
const groups = configGroups(fields)
|
|
return (
|
|
<div className="grid gap-5" data-testid="device-config">
|
|
{groups.map(g => (
|
|
<section key={g.group ?? '_'} className="grid gap-4" data-config-group={g.group ?? ''}>
|
|
{g.group && groups.length > 1 && g.fields.length > 1 && (
|
|
<h3 className="text-label font-semibold text-ink-2 uppercase tracking-wide">
|
|
{t(`modules:${appKey}.config_groups.${g.group}`, { defaultValue: g.group })}
|
|
</h3>
|
|
)}
|
|
{g.fields.map(control)}
|
|
</section>
|
|
))}
|
|
</div>
|
|
)
|
|
}
|