import { Card } from '@/libs/frontend-ui-primitives' import { useUiMode } from './UiModeContext' import type { MissionControlSubsystemStatus } from '@/services/api/missionControl' import { formatRelativeAge } from '@/utils/format' function subsystemLabel(key: string): string { const labels: Record = { rpc_head: 'RPC head', tx_index: 'Transaction index', stats_summary: 'Stats summary', bridge_relay_monitoring: 'Bridge relay monitoring', freshness_queries: 'Freshness queries', } return labels[key] || key.replace(/_/g, ' ') } function normalizeStatus(status?: string | null): string { const normalized = String(status || '').toLowerCase() if (!normalized) return 'unknown' if (normalized === 'ok') return 'operational' return normalized } function statusClasses(status?: string | null): string { const normalized = normalizeStatus(status) if (['degraded', 'down', 'stale'].includes(normalized)) { return 'border-red-200 bg-red-50 text-red-700 dark:border-red-900/60 dark:bg-red-950/30 dark:text-red-200' } if (['warning', 'partial', 'paused'].includes(normalized)) { return 'border-amber-200 bg-amber-50 text-amber-800 dark:border-amber-900/60 dark:bg-amber-950/30 dark:text-amber-200' } if (normalized === 'operational') { return 'border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-900/60 dark:bg-emerald-950/30 dark:text-emerald-200' } return 'border-gray-200 bg-gray-50 text-gray-700 dark:border-gray-700 dark:bg-gray-900/50 dark:text-gray-200' } function compactMeta(subsystem: MissionControlSubsystemStatus): string { const parts = [ subsystem.source || null, subsystem.completeness || null, subsystem.confidence || null, ].filter(Boolean) return parts.length > 0 ? parts.join(' ยท ') : 'No supporting freshness metadata' } export default function SubsystemPosturePanel({ subsystems, title = 'Subsystem Posture', scopeLabel, preferredKeys, className = '', }: { subsystems?: Record | null title?: string scopeLabel?: string preferredKeys?: string[] className?: string }) { const { mode } = useUiMode() const orderedEntries = Object.entries(subsystems || {}) .filter(([key]) => !preferredKeys || preferredKeys.includes(key)) .sort(([leftKey], [rightKey]) => { const leftIndex = preferredKeys ? preferredKeys.indexOf(leftKey) : -1 const rightIndex = preferredKeys ? preferredKeys.indexOf(rightKey) : -1 if (leftIndex !== -1 || rightIndex !== -1) { return (leftIndex === -1 ? Number.MAX_SAFE_INTEGER : leftIndex) - (rightIndex === -1 ? Number.MAX_SAFE_INTEGER : rightIndex) } return leftKey.localeCompare(rightKey) }) if (orderedEntries.length === 0) { return null } const normalizedClassName = className ? ` ${className}` : '' return (
{scopeLabel || 'These subsystem signals come from the same backend freshness model used to explain chain and transaction visibility.'}
{orderedEntries.map(([key, subsystem]) => { const status = normalizeStatus(subsystem.status) return (
{subsystemLabel(key)}
{status}
{status}
{subsystem.updated_at ? `Updated ${formatRelativeAge(subsystem.updated_at)}` : 'Update time unavailable'}
{compactMeta(subsystem)}
{mode === 'guided' && subsystem.provenance ? (
Provenance: {subsystem.provenance.replace(/_/g, ' ')}
) : null}
) })}
) }