'use client'; import { useState } from 'react'; import { PNum } from '@/components/ui/primitives'; import { adminFetch } from '@/lib/admin-fetch'; import { fmtInt, fmtPct } from '@/lib/format'; import type { AdminTarget } from '@/lib/types'; import { AdminPage, ErrorNote, Field, Panel, Toast, btnCls, btnPrimary, inputCls, useAdmin } from './shared'; const EMPTY: Partial = { target_id: '', name: '', hostname: '', category: 'cloud', provider: '', service_id: '', country: 'US', region: 'na-east', importance: 3, tier: 2 }; const CATEGORIES = ['dns', 'cdn', 'cloud', 'search', 'messaging', 'social', 'finance', 'government', 'news', 'developer', 'ai', 'streaming', 'commerce', 'infrastructure']; export function Targets() { const { data, err, reload } = useAdmin<{ targets: AdminTarget[] }>('/targets'); const [q, setQ] = useState(''); const [editing, setEditing] = useState | null>(null); const [isNew, setIsNew] = useState(false); const [msg, setMsg] = useState(null); const [busy, setBusy] = useState(false); const [actErr, setActErr] = useState(null); const rows = (data?.targets ?? []).filter((t) => !q || t.hostname.includes(q) || t.name.toLowerCase().includes(q.toLowerCase()) || t.target_id.includes(q)); const run = async (fn: () => Promise, ok: string) => { setBusy(true); setActErr(null); try { await fn(); setMsg(ok); setEditing(null); reload(); } catch (e) { setActErr(String(e)); } finally { setBusy(false); setTimeout(() => setMsg(null), 3000); } }; return ( setQ(e.target.value)} placeholder="filter…" className={inputCls} aria-label="Filter" /> } > {editing && (
{ e.preventDefault(); const body = { ...editing, importance: Number(editing.importance), tier: Number(editing.tier) }; void run(() => (isNew ? adminFetch('/targets', { method: 'POST', body }) : adminFetch(`/targets/${encodeURIComponent(editing.target_id!)}`, { method: 'PATCH', body })), isNew ? 'Target created' : 'Target updated'); }} > setEditing({ ...editing, target_id: e.target.value })} className={`${inputCls} num w-full`} /> setEditing({ ...editing, name: e.target.value })} className={`${inputCls} w-full`} /> setEditing({ ...editing, hostname: e.target.value })} className={`${inputCls} num w-full`} /> setEditing({ ...editing, provider: e.target.value })} className={`${inputCls} w-full`} /> setEditing({ ...editing, service_id: e.target.value })} className={`${inputCls} num w-full`} /> setEditing({ ...editing, country: e.target.value.toUpperCase() })} className={`${inputCls} num w-full`} /> setEditing({ ...editing, region: e.target.value })} className={`${inputCls} num w-full`} /> setEditing({ ...editing, importance: Number(e.target.value) })} className={`${inputCls} num w-full`} /> setEditing({ ...editing, tier: Number(e.target.value) })} className={`${inputCls} num w-full`} />
)}
{rows.map((t) => ( ))}
Target Hostname Category Anchor Imp. Tier Pressure OK 1h Enabled
{t.name} {t.target_id} {t.hostname} {t.category} {t.country} · {t.region} {t.importance} {t.tier} {fmtPct(t.ok_ratio_1h, 1)}

{fmtInt(rows.length)} targets

); }