import { useEffect, useState } from 'react'; import { objectId, page, pretty, record, request, session, tenantPath, type RecordValue, type Session, type Tenant } from './api'; import { AdminControl } from './management'; import { allowed } from './safety'; import { CatalogCheckNotice } from './catalog-check'; import { ErrorBox, errorText, useResource } from './resource'; import { NavIcon } from './navigation'; import { RecoveryReview, recoveryAsset, recoveryTenant } from './recovery-review'; import { EchoProbeReview } from './echo-probe-review'; import { EchoCompletionReview } from './echo-completion-review'; import { SyncAlignmentReview } from './sync-alignment-review'; import { SystemNonstartReview } from './system-nonstart-review'; const unknown = 'Nicht gemeldet'; const number = new Intl.NumberFormat('de-DE', { maximumFractionDigits: 1 }); const date = new Intl.DateTimeFormat('de-DE', { dateStyle: 'medium', timeStyle: 'short' }); function label(value: unknown) { return typeof value === 'string' && value.trim() ? value : unknown; } function time(value: unknown) { const t = typeof value === 'string' ? Date.parse(value) : NaN; return Number.isFinite(t) ? date.format(t) : unknown; } function gib(v: unknown) { return typeof v === 'number' && Number.isFinite(v) && v >= 0 ? number.format(v / 2 ** 30) : null; } function capacity(free: unknown, total: unknown) { const f = gib(free), t = gib(total); return f === null && t === null ? unknown : `${f ?? '–'} / ${t ?? '–'} GiB`; } function cpu(d: RecordValue) { const model = label(d.cpu_model), count = d.cpu_logical_count; const threads = typeof count === 'number' && count > 0 ? `${count} log. CPU${count === 1 ? '' : 's'}` : ''; return { primary: model === unknown ? threads || unknown : model, secondary: model === unknown ? '' : threads }; } const states: Record = { online: 'Online', offline: 'Offline', never_seen: 'Ohne Erstkontakt', revoked: 'Widerrufen', archived: 'Archiviert', replaced: 'Ersetzt', unverified: 'Identität unbestätigt' }; function Presence({ d }: { d: RecordValue }) { const state = typeof d.lifecycle === 'string' && d.lifecycle !== 'current' ? d.lifecycle : String(d.status); return ( {states[state] ?? 'Unbekannt'} ); } function Storage({ d }: { d: RecordValue }) { const free = d.disk_free_bytes, total = d.disk_total_bytes; return ( <> {capacity(free, total)} {typeof free === 'number' && typeof total === 'number' && free >= 0 && free <= total && total > 0 && ( )} ); } type Target = { customer: string; ids: string[] }; type Props = { detailPath: string; auth: Session; customers: Tenant[]; filter: string; onFilter: (id: string) => void; selection: Target; onSelection: (s: Target) => void; onCreateOrder: () => void; onEnroll: () => void; onOperation: (customer: string, id: string) => void; }; function devicePath(customer: string, asset: string) { return tenantPath(customer, '/devices/' + encodeURIComponent(asset)); } function DeviceDetail({ customer, asset, agent, auth, back, openOperation, changed }: { customer: string; asset: string; agent: string; auth: Session; back: () => void; openOperation: (id: string) => void; changed: () => void; }) { const path = devicePath(customer, asset); const detail = useResource( async (signal) => record(await request(path + '?agent_id=' + encodeURIComponent(agent), { signal })), [customer, asset, agent], true ); const membership = useResource( async (signal) => session(await request('/auth/session', { signal, tenantId: customer })), [customer, auth.principal.id] ); const [busy, setBusy] = useState(false), [error, setError] = useState(null); const d = detail.data; const scoped = membership.data; const volumes = Array.isArray(d?.volumes) ? d.volumes.map((v) => record(v)) : []; const jobs = Array.isArray(d?.recent_jobs) ? d.recent_jobs.map((v) => record(v)) : null; const facts = d && [ ['Kunde', label(d.customer_name)], ['Betriebssystem', label(d.os)], ['RAM', gib(d.ram_total_bytes) === null ? unknown : gib(d.ram_total_bytes) + ' GiB'], ['CPU', [cpu(d).primary, cpu(d).secondary].filter(Boolean).join(' · ')], ['Speicher frei / gesamt', capacity(d.disk_free_bytes, d.disk_total_bytes)], ['Letzter Kontakt', time(d.last_contact_at)], ['Inventar erhoben', time(d.observed_at)], ['Agentversion', label(d.agent_version)] ]; return (
{!d && !detail.error &&

Inventar wird geladen …

} {d && ( <>

{label(d.hostname)}

Geräteinformationen

{facts?.map(([k, v]) => (
{k}
{v}
))}
{volumes.length > 0 && (

Laufwerke

{volumes.map((v, i) => ( ))}
Laufwerk Speicher frei / gesamt
{label(v.name)} {capacity(v.free_bytes, v.total_bytes)}
)} {jobs && (

Letzte Aufträge

{jobs.length === 0 ? (

Keine Aufträge für dieses Gerät.

) : (
    {jobs.map((j) => (
  • {label(j.state)}
  • ))}
)}
)}
Technische Daten
{pretty(d)}
{scoped && allowed(scoped, 'devices:revoke') && (
Gerät verwalten {['archived', 'current'].includes(String(d.lifecycle)) && ( )} {d.lifecycle === 'current' && ( )}
)}
{scoped && customer === recoveryTenant && asset === recoveryAsset && allowed(scoped, 'inventory:read') && (
Entwicklungsabnahmen
)} )}
); } export function Devices({ detailPath, auth, customers, filter, onFilter, selection, onSelection, onEnroll, onCreateOrder, onOperation }: Props) { const [search, setSearch] = useState(''), [query, setQuery] = useState(''), [status, setStatus] = useState(''), [history, setHistory] = useState(false); const [routes, setRoutes] = useState([]); const [customerId, assetId, agentId] = detailPath.split('/'); const target = customerId && assetId && agentId ? { customer: customerId, asset: assetId, agent: agentId } : null; const back = () => { window.location.hash = 'geraete'; }; const cursor = routes[routes.length - 1]; useEffect(() => { const timer = setTimeout(() => { setQuery(search.trim()); setRoutes([]); }, 200); return () => clearTimeout(timer); }, [search]); const list = useResource( async (signal) => { if (target) return null; const params = new URLSearchParams({ view: history ? 'history' : 'current', limit: '40' }); if (query) params.set('q', query); if (status && !history) params.set('status', status); if (cursor) params.set('cursor', cursor); const path = filter ? tenantPath(filter, '/devices') : '/api/v1/devices'; return page(await request(path + '?' + params, { signal })); }, [filter, query, history, status, cursor, detailPath], true ); function setView(historic: boolean) { setRoutes([]); setHistory(historic); onSelection({ customer: '', ids: [] }); } function select(d: RecordValue, checked: boolean) { const asset = objectId(d, 'asset'), customer = objectId(d, 'tenant'); if (checked && selection.ids.length && customer !== selection.customer) return; const ids = checked ? [...selection.ids, asset] : selection.ids.filter((id) => id !== asset); onSelection({ customer: ids.length ? customer : '', ids }); } if (target) return ( onOperation(target.customer, id)} changed={() => { back(); setRoutes([]); onSelection({ customer: '', ids: [] }); list.reload(); }} /> ); return ( <>

Geräte

{allowed(auth, 'devices:enroll') && ( )}
{!history && ( )}
{history && (

Ausgemusterte Geräte und Registrierungen ohne bestätigte Identität.

)} {!list.data && !list.error &&

Geräte werden geladen …

} {list.data && ( <>
{!history && ( )} {['Hostname', 'OS', 'Status', 'Speicher frei / gesamt', 'RAM', 'CPU', 'Kunde'].map( (h) => ( ) )} {list.data.items.map((d) => { const asset = objectId(d, 'asset'), agent = objectId(d, 'agent'), customer = objectId(d, 'tenant'), model = cpu(d); const show = () => { window.location.hash = ['geraete', customer, asset, agent] .map(encodeURIComponent) .join('/'); }; const another = selection.ids.length > 0 && selection.customer !== customer; return ( {!history && ( )} ); })}
Ziel auswählen {h}
e.stopPropagation()}> select(d, e.target.checked)} /> {label(d.os)} {d.status === 'offline' && d.lifecycle === 'current' && ( Zuletzt {time(d.last_contact_at)} )} {gib(d.ram_total_bytes) === null ? unknown : gib(d.ram_total_bytes) + ' GiB'} {model.primary} {model.secondary && {model.secondary}} {label(d.customer_name)}
{!list.data.items.length && (

{query || filter || status ? 'Keine passenden Geräte' : 'Keine Geräte in dieser Ansicht'}

{(query || filter || status) && ( )}
)}
{list.data.items.length} {list.data.items.length === 1 ? 'Gerät' : 'Geräte'} {cursor || list.data.next_cursor ? ' auf dieser Seite' : ''} · Stand{' '} {time(list.data.as_of)} {(cursor || list.data.next_cursor) && (
)}
)}
{!!selection.ids.length && (
{selection.ids.length} {selection.ids.length === 1 ? 'Gerät' : 'Geräte'} ausgewählt {customers.find((c) => c.tenant_id === selection.customer)?.name}
)} ); }