|
@@ -0,0 +1,175 @@
|
|
|
|
|
+/**
|
|
|
|
|
+ * DeviceNameOrderCard — preferencia global de fuentes de nombre de cliente
|
|
|
|
|
+ * (#2163 fase 2). El servidor resuelve el nombre de un cliente con la primera
|
|
|
|
|
+ * fuente que tenga algo que proponer; este orden decide cuáles van antes.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Solo se pueden reordenar las fuentes que no son nombres escritos por el
|
|
|
|
|
+ * usuario: el nombre manual y el alias de una MAC conocida mandan siempre. Un
|
|
|
|
|
+ * cliente con su propia fuente elegida (ficha del cliente) no se ve afectado.
|
|
|
|
|
+ */
|
|
|
|
|
+import { useCallback, useEffect, useState } from 'react'
|
|
|
|
|
+import { useTranslation } from 'react-i18next'
|
|
|
|
|
+import { ArrowDown, ArrowUp, Loader2, RotateCcw } from 'lucide-react'
|
|
|
|
|
+import { useNetPulse } from '@/data/DataProvider'
|
|
|
|
|
+import { fetchJson } from '@/lib/utils'
|
|
|
|
|
+
|
|
|
|
|
+/** Orden por defecto del servidor cuando no hay preferencia guardada. */
|
|
|
|
|
+const DEFAULT_ORDER = ['reservation', 'dhcp', 'glinet', 'mdns', 'rdns'] as const
|
|
|
|
|
+
|
|
|
|
|
+type Source = (typeof DEFAULT_ORDER)[number]
|
|
|
|
|
+
|
|
|
|
|
+/** Una fuente que el usuario no conoce no debe colarse en la lista. */
|
|
|
|
|
+function normalize(list: unknown): Source[] {
|
|
|
|
|
+ if (!Array.isArray(list)) return []
|
|
|
|
|
+ const seen = new Set<string>()
|
|
|
|
|
+ const out: Source[] = []
|
|
|
|
|
+ for (const item of list) {
|
|
|
|
|
+ if (typeof item !== 'string') continue
|
|
|
|
|
+ if (!(DEFAULT_ORDER as readonly string[]).includes(item) || seen.has(item)) continue
|
|
|
|
|
+ seen.add(item)
|
|
|
|
|
+ out.push(item as Source)
|
|
|
|
|
+ }
|
|
|
|
|
+ return out
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function DeviceNameOrderCard() {
|
|
|
|
|
+ const { t } = useTranslation()
|
|
|
|
|
+ const { isDemo } = useNetPulse()
|
|
|
|
|
+ const [saved, setSaved] = useState<Source[]>([])
|
|
|
|
|
+ const [order, setOrder] = useState<Source[]>([...DEFAULT_ORDER])
|
|
|
|
|
+ const [loading, setLoading] = useState(true)
|
|
|
|
|
+ const [busy, setBusy] = useState(false)
|
|
|
|
|
+ const [error, setError] = useState('')
|
|
|
|
|
+
|
|
|
|
|
+ const load = useCallback(async () => {
|
|
|
|
|
+ const res = await fetchJson<{ sources: string[] }>('/api/settings/nameorder')
|
|
|
|
|
+ if (res.ok && res.data) {
|
|
|
|
|
+ const list = normalize(res.data.sources)
|
|
|
|
|
+ setSaved(list)
|
|
|
|
|
+ // Sin preferencia guardada, la tarjeta muestra el orden por defecto:
|
|
|
|
|
+ // es lo que el servidor aplica y el punto del que se parte.
|
|
|
|
|
+ setOrder(list.length > 0 ? list : [...DEFAULT_ORDER])
|
|
|
|
|
+ setError('')
|
|
|
|
|
+ } else {
|
|
|
|
|
+ setError(t('settings.nameOrder.loadError'))
|
|
|
|
|
+ }
|
|
|
|
|
+ setLoading(false)
|
|
|
|
|
+ }, [t])
|
|
|
|
|
+
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ if (isDemo) {
|
|
|
|
|
+ setLoading(false)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ void load()
|
|
|
|
|
+ }, [isDemo, load])
|
|
|
|
|
+
|
|
|
|
|
+ const move = (index: number, delta: number) => {
|
|
|
|
|
+ setOrder((prev) => {
|
|
|
|
|
+ const next = [...prev]
|
|
|
|
|
+ const target = index + delta
|
|
|
|
|
+ if (target < 0 || target >= next.length) return prev
|
|
|
|
|
+ const a = next[index]
|
|
|
|
|
+ const b = next[target]
|
|
|
|
|
+ if (a === undefined || b === undefined) return prev
|
|
|
|
|
+ next[index] = b
|
|
|
|
|
+ next[target] = a
|
|
|
|
|
+ return next
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const save = async (list: Source[]) => {
|
|
|
|
|
+ setBusy(true)
|
|
|
|
|
+ setError('')
|
|
|
|
|
+ try {
|
|
|
|
|
+ const res = await fetchJson<{ sources: string[] }>('/api/settings/nameorder', {
|
|
|
|
|
+ method: 'PUT',
|
|
|
|
|
+ headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
+ body: JSON.stringify({ sources: list }),
|
|
|
|
|
+ })
|
|
|
|
|
+ if (!res.ok) {
|
|
|
|
|
+ setError(t('settings.nameOrder.saveError'))
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ const stored = normalize(res.data?.sources)
|
|
|
|
|
+ setSaved(stored)
|
|
|
|
|
+ setOrder(stored.length > 0 ? stored : [...DEFAULT_ORDER])
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ setError(t('settings.nameOrder.saveError'))
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ setBusy(false)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const dirty = order.join(',') !== (saved.length > 0 ? saved.join(',') : DEFAULT_ORDER.join(','))
|
|
|
|
|
+
|
|
|
|
|
+ return (
|
|
|
|
|
+ <section className="h-full rounded-2xl border border-border bg-surface p-5">
|
|
|
|
|
+ <h2 className="font-display text-h2 text-text-primary">{t('settings.nameOrder.title')}</h2>
|
|
|
|
|
+ <p className="mt-0.5 text-sm text-text-secondary">{t('settings.nameOrder.caption')}</p>
|
|
|
|
|
+
|
|
|
|
|
+ {loading ? (
|
|
|
|
|
+ <div className="mt-4 flex items-center gap-2 text-caption text-text-muted">
|
|
|
|
|
+ <Loader2 className="h-3.5 w-3.5 animate-spin" strokeWidth={1.75} />
|
|
|
|
|
+ {t('common.loading')}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ <>
|
|
|
|
|
+ <ol className="mt-4 space-y-1.5">
|
|
|
|
|
+ {order.map((source, i) => (
|
|
|
|
|
+ <li
|
|
|
|
|
+ key={source}
|
|
|
|
|
+ className="flex items-center gap-2 rounded-lg border border-border bg-canvas px-3 py-1.5"
|
|
|
|
|
+ >
|
|
|
|
|
+ <span className="w-4 shrink-0 font-mono text-caption text-text-muted">{i + 1}</span>
|
|
|
|
|
+ <span className="min-w-0 flex-1 truncate text-sm text-text-primary">
|
|
|
|
|
+ {t(`devices.detail.source.${source}`)}
|
|
|
|
|
+ </span>
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={() => move(i, -1)}
|
|
|
|
|
+ disabled={busy || i === 0}
|
|
|
|
|
+ aria-label={t('settings.nameOrder.up')}
|
|
|
|
|
+ className="flex h-6 w-6 items-center justify-center rounded border border-border text-text-muted transition-colors hover:text-accent disabled:opacity-40"
|
|
|
|
|
+ >
|
|
|
|
|
+ <ArrowUp className="h-3 w-3" strokeWidth={2} />
|
|
|
|
|
+ </button>
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={() => move(i, 1)}
|
|
|
|
|
+ disabled={busy || i === order.length - 1}
|
|
|
|
|
+ aria-label={t('settings.nameOrder.down')}
|
|
|
|
|
+ className="flex h-6 w-6 items-center justify-center rounded border border-border text-text-muted transition-colors hover:text-accent disabled:opacity-40"
|
|
|
|
|
+ >
|
|
|
|
|
+ <ArrowDown className="h-3 w-3" strokeWidth={2} />
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </li>
|
|
|
|
|
+ ))}
|
|
|
|
|
+ </ol>
|
|
|
|
|
+ {error && <p className="mt-2 text-caption text-danger">{error}</p>}
|
|
|
|
|
+ <div className="mt-3 flex items-center gap-2">
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={() => void save(order)}
|
|
|
|
|
+ disabled={busy || !dirty}
|
|
|
|
|
+ className="inline-flex h-8 items-center gap-1.5 rounded-lg bg-accent px-3 text-sm font-medium text-canvas transition-opacity hover:opacity-90 disabled:opacity-40"
|
|
|
|
|
+ >
|
|
|
|
|
+ {busy && <Loader2 className="h-3.5 w-3.5 animate-spin" strokeWidth={1.75} />}
|
|
|
|
|
+ {t('settings.nameOrder.save')}
|
|
|
|
|
+ </button>
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={() => void save([])}
|
|
|
|
|
+ disabled={busy || saved.length === 0}
|
|
|
|
|
+ className="inline-flex h-8 items-center gap-1.5 rounded-lg border border-border px-3 text-sm font-medium text-text-secondary transition-colors hover:text-text-primary disabled:opacity-40"
|
|
|
|
|
+ >
|
|
|
|
|
+ <RotateCcw className="h-3.5 w-3.5" strokeWidth={1.75} />
|
|
|
|
|
+ {t('settings.nameOrder.reset')}
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <p className="mt-2 text-caption text-text-muted">{t('settings.nameOrder.hint')}</p>
|
|
|
|
|
+ </>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </section>
|
|
|
|
|
+ )
|
|
|
|
|
+}
|