import { type ReactNode } from 'react'; import { objectId, record, type RecordValue } from './api'; const states: Record = { pending_approval: 'Wartet auf Genehmigung', approved: 'Genehmigt', rejected: 'Abgelehnt', expired: 'Abgelaufen', revoked: 'Widerrufen', queued: 'Eingereiht', delivered: 'Zugestellt', offered: 'Angeboten', accepted: 'Angenommen', running: 'Läuft', verifying: 'Ergebnis wird geprüft', awaiting_reboot: 'Wartet auf Neustart', succeeded: 'Erfolgreich', partially_succeeded: 'Teilweise erfolgreich', failed: 'Fehlgeschlagen', cancelled: 'Abgebrochen', cancel_requested: 'Abbruch angefordert', unknown: 'Wirkung unklar', closed_unknown: 'Mit unklarer Wirkung abgeschlossen', needs_attention: 'Prüfung erforderlich' }; const actions: Record = { run_script: 'Freies Skript', refresh_inventory: 'Inventar aktualisieren', query_logs: 'Protokolle abrufen', set_service_state: 'Dienst verwalten', ensure_package: 'Paket installieren', apply_updates: 'Updates installieren', apt_native_acceptance: 'Paketinstallation prüfen', reboot: 'Gerät neu starten', update_agent: 'Agent aktualisieren' }; const formatter = new Intl.DateTimeFormat('de-DE', { dateStyle: 'short', timeStyle: 'short' }); export function display(value: unknown, fallback = 'Unbekannt') { return typeof value === 'string' && value.trim() ? value : fallback; } export function when(value: unknown) { const ms = Date.parse(String(value)); return Number.isFinite(ms) ? formatter.format(ms) : 'Unbekannt'; } export function statusText(value: unknown) { return states[String(value)] ?? display(value); } export function actionTitle(value: unknown) { return actions[String(value)] ?? display(value, 'Auftrag'); } export function orderTitle(row: RecordValue) { return display(row.title, actionTitle(row.action)); } export function targets(row: RecordValue): RecordValue[] { return Array.isArray(row.targets) ? row.targets.map(v => record(v)) : []; } export function deviceName(row: RecordValue) { return display(row.hostname, display(row.asset_id, 'Gerät unbekannt')); } export function statusOf(row: RecordValue) { return row.approval_state === 'approved' && row.execution_state ? row.execution_state : row.approval_state; } export function identityName(value: unknown) { if (value === 'system') return 'SYSTEM (Administratorrechte)'; if (value === 'root') return 'root (Administratorrechte)'; return value === 'agent_service' ? 'Agent-Dienstkonto' : display(value); } export function Requester({ id, ownId }: { id: unknown; ownId?: string }) { const name = display(id); if (name.startsWith('local:') || name.startsWith('mcp:')) return {name === ownId ? 'Mein Konto' : name.startsWith('mcp:') ? 'MCP-Client' : 'Lokales Konto'} · {name.slice(-8)} ; return <>{name}; } export function Status({ value }: { value: unknown }) { return {statusText(value)}; } export function Fact({ label, children }: { label: string; children: ReactNode }) { return
{label}
{children}
; } export function ApprovalDecision({ order, ownId }: { order: RecordValue; ownId?: string }) { const decision = order.decision && typeof order.decision === 'object' ? record(order.decision) : null; if (typeof decision?.principal !== 'string' || typeof decision.at !== 'string') { return {order.approval_state === 'expired' ? 'Automatisch abgelaufen' : 'Entscheidung nicht belegt'}; } return <> {decision.testsimulation === true && Simulierter Testentscheid} ; } export function OrderRows({ items, customerName, decisionList, decisionHistory, ownId, open }: { items: RecordValue[]; customerName?: string; decisionList?: boolean; decisionHistory?: boolean; ownId?: string; open: (row: RecordValue) => void }) { return
{decisionHistory && } {items.map(row => { const all = targets(row); const devices = all.slice(0, 2).map(deviceName).join(', '); const revision = row.script_revision ? record(row.script_revision) : null; return {decisionHistory && } ; })}
AuftragKundeGeräte{decisionList ? 'Eingereicht von' : 'Erstellt'}StatusEntschieden von und am
{revision && Version {String(revision.version)}} {display(row.customer_name, customerName)} {devices || (typeof row.planned_job_count === 'number' ? String(row.planned_job_count) : 'Unbekannt')}{all.length > 2 && ' und '+(all.length-2)+' weitere'} {decisionList ? : when(row.created_at)}
; } export function PageButtons({ pages, next, change }: { pages: string[]; next: string | null; change: (pages: string[]) => void }) { if (!next && !pages.length) return null; return
; }