|
|
@@ -0,0 +1,352 @@
|
|
|
+import { useCallback, useEffect, useState } from 'react'
|
|
|
+import { useTranslation } from 'react-i18next'
|
|
|
+import { motion } from 'framer-motion'
|
|
|
+import { Check, Pencil, Plus, ShieldOff, Trash2, X } from 'lucide-react'
|
|
|
+import { Button } from '@/components/ui/button'
|
|
|
+import { Input } from '@/components/ui/input'
|
|
|
+import {
|
|
|
+ Select,
|
|
|
+ SelectContent,
|
|
|
+ SelectItem,
|
|
|
+ SelectTrigger,
|
|
|
+ SelectValue,
|
|
|
+} from '@/components/ui/select'
|
|
|
+import { useNetPulse } from '@/data/DataProvider'
|
|
|
+import { cn } from '@/lib/utils'
|
|
|
+
|
|
|
+interface AlertException {
|
|
|
+ id: number
|
|
|
+ kind: string
|
|
|
+ host: string
|
|
|
+ target: string
|
|
|
+ comment: string
|
|
|
+ expiresAt: number
|
|
|
+ createdAt: number
|
|
|
+ hits: number
|
|
|
+ lastHit: number
|
|
|
+}
|
|
|
+
|
|
|
+interface Draft {
|
|
|
+ kind: string
|
|
|
+ host: string
|
|
|
+ target: string
|
|
|
+ comment: string
|
|
|
+ expires: string
|
|
|
+}
|
|
|
+
|
|
|
+const ANY = '__any__'
|
|
|
+const HOST_LIST_ID = 'alert-exception-hosts'
|
|
|
+
|
|
|
+const emptyDraft = (): Draft => ({ kind: '', host: '', target: '', comment: '', expires: '' })
|
|
|
+
|
|
|
+function dateValue(ms: number): string {
|
|
|
+ if (!ms) return ''
|
|
|
+ const d = new Date(ms)
|
|
|
+ const pad = (n: number) => String(n).padStart(2, '0')
|
|
|
+ return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
|
|
|
+}
|
|
|
+
|
|
|
+function endOfDay(value: string): number {
|
|
|
+ if (!value) return 0
|
|
|
+ const [ys, ms, ds] = value.split('-')
|
|
|
+ const y = Number(ys) || 1970
|
|
|
+ const m = Number(ms) || 1
|
|
|
+ const d = Number(ds) || 1
|
|
|
+ return new Date(y, m - 1, d, 23, 59, 59).getTime()
|
|
|
+}
|
|
|
+
|
|
|
+export function AlertExceptions() {
|
|
|
+ const { t, i18n } = useTranslation()
|
|
|
+ const { routers, isDemo } = useNetPulse()
|
|
|
+ const [items, setItems] = useState<AlertException[]>([])
|
|
|
+ const [loading, setLoading] = useState(true)
|
|
|
+ const [error, setError] = useState('')
|
|
|
+ const [draft, setDraft] = useState<Draft | null>(null)
|
|
|
+ const [editId, setEditId] = useState<number | null>(null)
|
|
|
+ const [saving, setSaving] = useState(false)
|
|
|
+
|
|
|
+ const fetchItems = useCallback(async () => {
|
|
|
+ try {
|
|
|
+ const res = await fetch('/api/alerts/exceptions')
|
|
|
+ if (!res.ok) {
|
|
|
+ setError(t('alerts.exceptions.loadError'))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ setItems(await res.json())
|
|
|
+ setError('')
|
|
|
+ } catch {
|
|
|
+ setError(t('alerts.exceptions.loadError'))
|
|
|
+ } finally {
|
|
|
+ setLoading(false)
|
|
|
+ }
|
|
|
+ }, [t])
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (isDemo) {
|
|
|
+ setLoading(false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ void fetchItems()
|
|
|
+ }, [fetchItems, isDemo])
|
|
|
+
|
|
|
+ const rawTypes = t('alerts.types', { returnObjects: true })
|
|
|
+ const typeOptions =
|
|
|
+ rawTypes && typeof rawTypes === 'object'
|
|
|
+ ? Object.keys(rawTypes as Record<string, unknown>)
|
|
|
+ .map((slug) => ({ slug, label: t(`alerts.types.${slug}.title`) }))
|
|
|
+ .sort((a, b) => a.label.localeCompare(b.label))
|
|
|
+ : []
|
|
|
+
|
|
|
+ const startCreate = () => { setDraft(emptyDraft()); setEditId(null); setError('') }
|
|
|
+ const startEdit = (x: AlertException) => {
|
|
|
+ setDraft({ kind: x.kind, host: x.host, target: x.target, comment: x.comment, expires: dateValue(x.expiresAt) })
|
|
|
+ setEditId(x.id)
|
|
|
+ setError('')
|
|
|
+ }
|
|
|
+ const cancel = () => { setDraft(null); setEditId(null); setError('') }
|
|
|
+
|
|
|
+ const save = async () => {
|
|
|
+ if (!draft || saving) return
|
|
|
+ setSaving(true)
|
|
|
+ try {
|
|
|
+ const body = JSON.stringify({
|
|
|
+ kind: draft.kind,
|
|
|
+ host: draft.host.trim(),
|
|
|
+ target: draft.target.trim(),
|
|
|
+ comment: draft.comment.trim(),
|
|
|
+ expiresAt: endOfDay(draft.expires),
|
|
|
+ })
|
|
|
+ const res = await fetch(editId ? `/api/alerts/exceptions/${editId}` : '/api/alerts/exceptions', {
|
|
|
+ method: editId ? 'PUT' : 'POST',
|
|
|
+ headers: { 'Content-Type': 'application/json' },
|
|
|
+ body,
|
|
|
+ })
|
|
|
+ if (!res.ok) {
|
|
|
+ setError(t('alerts.exceptions.saveError'))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ cancel()
|
|
|
+ await fetchItems()
|
|
|
+ } catch {
|
|
|
+ setError(t('alerts.exceptions.saveError'))
|
|
|
+ } finally {
|
|
|
+ setSaving(false)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const remove = async (id: number) => {
|
|
|
+ try {
|
|
|
+ const res = await fetch(`/api/alerts/exceptions/${id}`, { method: 'DELETE' })
|
|
|
+ if (!res.ok) {
|
|
|
+ setError(t('alerts.exceptions.saveError'))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ await fetchItems()
|
|
|
+ } catch {
|
|
|
+ setError(t('alerts.exceptions.saveError'))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const extraKind = draft?.kind && !typeOptions.some((o) => o.slug === draft.kind) ? draft.kind : ''
|
|
|
+ const typeLabel = (kind: string) => (kind ? t(`alerts.types.${kind}.title`) : t('alerts.exceptions.anyType'))
|
|
|
+ const dateLabel = (ms: number) => new Date(ms).toLocaleDateString(i18n.language)
|
|
|
+ const lastHitLabel = (ms: number) => new Date(ms).toLocaleString(i18n.language)
|
|
|
+
|
|
|
+ // La demo pública no tiene backend de escritura: la sección no se muestra
|
|
|
+ // para no enseñar un error de carga.
|
|
|
+ if (isDemo) return null
|
|
|
+ if (loading) return <p className="text-caption text-text-muted">{t('common.loading')}</p>
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div className="space-y-4">
|
|
|
+ <div className="flex items-center justify-between gap-3">
|
|
|
+ <div className="min-w-0">
|
|
|
+ <h3 className="text-sm font-semibold text-text-primary">{t('alerts.exceptions.title')}</h3>
|
|
|
+ <p className="mt-0.5 text-caption text-text-muted">{t('alerts.exceptions.description')}</p>
|
|
|
+ </div>
|
|
|
+ {!draft && (
|
|
|
+ <Button size="sm" variant="outline" onClick={startCreate} className="shrink-0 gap-1.5">
|
|
|
+ <Plus className="h-3.5 w-3.5" />
|
|
|
+ {t('alerts.exceptions.add')}
|
|
|
+ </Button>
|
|
|
+ )}
|
|
|
+ </div>
|
|
|
+
|
|
|
+ {error && <p className="text-caption text-danger" role="alert">{error}</p>}
|
|
|
+
|
|
|
+ {items.length === 0 && !draft && (
|
|
|
+ <p className="py-4 text-center text-caption text-text-muted">{t('alerts.exceptions.empty')}</p>
|
|
|
+ )}
|
|
|
+
|
|
|
+ {items.map((x) => {
|
|
|
+ const expired = x.expiresAt > 0 && x.expiresAt <= Date.now()
|
|
|
+ return (
|
|
|
+ <div
|
|
|
+ key={x.id}
|
|
|
+ className={cn(
|
|
|
+ 'flex items-start gap-3 rounded-xl border border-border bg-elevated p-3',
|
|
|
+ expired && 'opacity-60',
|
|
|
+ )}
|
|
|
+ >
|
|
|
+ <span className={cn(
|
|
|
+ 'flex h-9 w-9 shrink-0 items-center justify-center rounded-lg',
|
|
|
+ expired ? 'bg-danger/10 text-danger' : 'bg-accent/10 text-accent',
|
|
|
+ )}>
|
|
|
+ <ShieldOff className="h-4 w-4" strokeWidth={1.75} />
|
|
|
+ </span>
|
|
|
+ <div className="min-w-0 flex-1">
|
|
|
+ <div className="flex flex-wrap items-center gap-1.5">
|
|
|
+ <span className="text-sm font-medium text-text-primary">{typeLabel(x.kind)}</span>
|
|
|
+ <span className="rounded-md bg-surface px-1.5 py-0.5 text-[11px] text-text-muted">
|
|
|
+ {x.host || t('alerts.exceptions.anyHost')}
|
|
|
+ </span>
|
|
|
+ {x.target && (
|
|
|
+ <span className="rounded-md bg-surface px-1.5 py-0.5 text-[11px] text-text-muted">
|
|
|
+ {x.target}
|
|
|
+ </span>
|
|
|
+ )}
|
|
|
+ {expired && (
|
|
|
+ <span className="rounded-md bg-danger/10 px-1.5 py-0.5 text-[10px] font-bold uppercase tracking-[0.05em] text-danger">
|
|
|
+ {t('alerts.exceptions.expired')}
|
|
|
+ </span>
|
|
|
+ )}
|
|
|
+ </div>
|
|
|
+ {x.comment && <p className="mt-0.5 truncate text-caption text-text-muted">{x.comment}</p>}
|
|
|
+ <p className="mt-1 text-caption text-text-muted">
|
|
|
+ {t('alerts.exceptions.hits', { count: x.hits })}
|
|
|
+ {' · '}
|
|
|
+ {x.expiresAt > 0
|
|
|
+ ? t('alerts.exceptions.expiresOn', { date: dateLabel(x.expiresAt) })
|
|
|
+ : t('alerts.exceptions.never')}
|
|
|
+ {x.lastHit > 0 && ` · ${t('alerts.exceptions.lastHit', { date: lastHitLabel(x.lastHit) })}`}
|
|
|
+ </p>
|
|
|
+ </div>
|
|
|
+ <Button
|
|
|
+ size="icon"
|
|
|
+ variant="ghost"
|
|
|
+ onClick={() => startEdit(x)}
|
|
|
+ aria-label={t('alerts.exceptions.edit')}
|
|
|
+ className="h-8 w-8 text-text-muted hover:text-accent"
|
|
|
+ >
|
|
|
+ <Pencil className="h-3.5 w-3.5" />
|
|
|
+ </Button>
|
|
|
+ <Button
|
|
|
+ size="icon"
|
|
|
+ variant="ghost"
|
|
|
+ onClick={() => void remove(x.id)}
|
|
|
+ aria-label={t('alerts.exceptions.delete')}
|
|
|
+ className="h-8 w-8 text-text-muted hover:text-danger"
|
|
|
+ >
|
|
|
+ <Trash2 className="h-3.5 w-3.5" />
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+ })}
|
|
|
+
|
|
|
+ {draft && (
|
|
|
+ <motion.div
|
|
|
+ initial={{ opacity: 0, y: -8 }}
|
|
|
+ animate={{ opacity: 1, y: 0 }}
|
|
|
+ className="rounded-xl border border-accent/30 bg-accent-soft/20 p-4"
|
|
|
+ >
|
|
|
+ <h4 className="mb-3 text-sm font-semibold text-text-primary">
|
|
|
+ {editId ? t('alerts.exceptions.edit') : t('alerts.exceptions.add')}
|
|
|
+ </h4>
|
|
|
+ <div className="grid gap-3 sm:grid-cols-2">
|
|
|
+ <div>
|
|
|
+ <label htmlFor="exception-kind" className="mb-1 block text-caption font-medium text-text-secondary">
|
|
|
+ {t('alerts.exceptions.kind')}
|
|
|
+ </label>
|
|
|
+ <Select
|
|
|
+ value={draft.kind || ANY}
|
|
|
+ onValueChange={(v) => setDraft({ ...draft, kind: v === ANY ? '' : v })}
|
|
|
+ >
|
|
|
+ <SelectTrigger id="exception-kind" size="sm" className="h-8">
|
|
|
+ <SelectValue />
|
|
|
+ </SelectTrigger>
|
|
|
+ <SelectContent>
|
|
|
+ <SelectItem value={ANY}>{t('alerts.exceptions.anyType')}</SelectItem>
|
|
|
+ {extraKind && <SelectItem value={extraKind}>{extraKind}</SelectItem>}
|
|
|
+ {typeOptions.map((o) => (
|
|
|
+ <SelectItem key={o.slug} value={o.slug}>{o.label}</SelectItem>
|
|
|
+ ))}
|
|
|
+ </SelectContent>
|
|
|
+ </Select>
|
|
|
+ </div>
|
|
|
+ <div>
|
|
|
+ <label htmlFor="exception-host" className="mb-1 block text-caption font-medium text-text-secondary">
|
|
|
+ {t('alerts.exceptions.host')}
|
|
|
+ </label>
|
|
|
+ <Input
|
|
|
+ id="exception-host"
|
|
|
+ list={HOST_LIST_ID}
|
|
|
+ value={draft.host}
|
|
|
+ onChange={(e) => setDraft({ ...draft, host: e.target.value })}
|
|
|
+ placeholder={t('alerts.exceptions.hostPlaceholder')}
|
|
|
+ className="h-8 text-sm"
|
|
|
+ />
|
|
|
+ <datalist id={HOST_LIST_ID}>
|
|
|
+ {routers.map((r) => (
|
|
|
+ <option key={r.id} value={r.id}>{r.name}</option>
|
|
|
+ ))}
|
|
|
+ </datalist>
|
|
|
+ </div>
|
|
|
+ <div>
|
|
|
+ <label htmlFor="exception-target" className="mb-1 block text-caption font-medium text-text-secondary">
|
|
|
+ {t('alerts.exceptions.target')}
|
|
|
+ </label>
|
|
|
+ <Input
|
|
|
+ id="exception-target"
|
|
|
+ value={draft.target}
|
|
|
+ onChange={(e) => setDraft({ ...draft, target: e.target.value })}
|
|
|
+ placeholder={t('alerts.exceptions.targetPlaceholder')}
|
|
|
+ className="h-8 text-sm"
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ <div>
|
|
|
+ <label htmlFor="exception-expires" className="mb-1 block text-caption font-medium text-text-secondary">
|
|
|
+ {t('alerts.exceptions.expires')}
|
|
|
+ </label>
|
|
|
+ <Input
|
|
|
+ id="exception-expires"
|
|
|
+ type="date"
|
|
|
+ value={draft.expires}
|
|
|
+ onChange={(e) => setDraft({ ...draft, expires: e.target.value })}
|
|
|
+ className="h-8 text-sm"
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ <div className="sm:col-span-2">
|
|
|
+ <label htmlFor="exception-comment" className="mb-1 block text-caption font-medium text-text-secondary">
|
|
|
+ {t('alerts.exceptions.comment')}
|
|
|
+ </label>
|
|
|
+ <Input
|
|
|
+ id="exception-comment"
|
|
|
+ value={draft.comment}
|
|
|
+ onChange={(e) => setDraft({ ...draft, comment: e.target.value })}
|
|
|
+ placeholder={t('alerts.exceptions.commentPlaceholder')}
|
|
|
+ maxLength={280}
|
|
|
+ className="h-8 text-sm"
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ <p className="mt-2 text-caption text-text-muted">{t('alerts.exceptions.hint')}</p>
|
|
|
+ <div className="mt-4 flex justify-end gap-2">
|
|
|
+ <Button size="sm" variant="ghost" onClick={cancel} disabled={saving} className="gap-1.5">
|
|
|
+ <X className="h-3.5 w-3.5" />
|
|
|
+ {t('common.cancel')}
|
|
|
+ </Button>
|
|
|
+ <Button
|
|
|
+ size="sm"
|
|
|
+ onClick={() => void save()}
|
|
|
+ disabled={saving || (!draft.kind && !draft.host.trim() && !draft.target.trim())}
|
|
|
+ className="gap-1.5"
|
|
|
+ >
|
|
|
+ <Check className="h-3.5 w-3.5" />
|
|
|
+ {editId ? t('alerts.exceptions.save') : t('alerts.exceptions.add')}
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+ </motion.div>
|
|
|
+ )}
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+}
|