| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- /**
- * DeviceFacetPanel - NETPULSE-ONE-LEGION.
- *
- * An agent row the fleet declared `facet` of this device is not a box:
- * it is another face of the host (the RKE2 agent on the legion machine).
- * The server drops it from routers[]/topology and hangs a DeviceFacet
- * pointer on the parent device; this panel is where the folded agent's
- * truth lives - vitals straight from the agent row plus its workload
- * census (the #2227 WorkloadsPanel, reused untouched).
- *
- * When the agent stops pushing, the device carries the badge (no ghost
- * box returns); the panel keeps its last good census visible and says
- * so, mirroring the anti-blink rule of the push contract.
- */
- import { useEffect, useState } from 'react'
- import { useTranslation } from 'react-i18next'
- import { Activity } from 'lucide-react'
- import type { DeviceFacet, Router, WorkloadsInfo } from '@/data/types'
- import { fetchJson } from '@/lib/utils'
- import { SectionHeader } from '@/components/SectionHeader'
- import { StatusPill } from '@/components/StatusPill'
- import { WorkloadsPanel } from '@/components/routers/WorkloadsPanel'
- import { fmtUptime } from '@/i18n'
- interface FacetDetailResponse {
- router?: Router
- workloads?: WorkloadsInfo
- }
- /** One vitals chip; null values (pusher without the section) are skipped,
- * never painted as zero - the #441 law for external units. */
- function Vital({ label, value }: { label: string; value: string | null }) {
- if (value === null) return null
- return (
- <span className="inline-flex items-center gap-1.5 rounded-full bg-elevated px-2.5 py-1 text-caption text-text-secondary">
- <span className="uppercase tracking-wide text-text-muted">{label}</span>
- <span className="font-mono text-mono-sm text-text-primary">{value}</span>
- </span>
- )
- }
- export function DeviceFacetPanel({ facet }: { facet: DeviceFacet }) {
- const { t } = useTranslation()
- const [detail, setDetail] = useState<FacetDetailResponse | null>(null)
- useEffect(() => {
- let dead = false
- void fetchJson<FacetDetailResponse>(`/api/routers/${encodeURIComponent(facet.routerId)}`).then((res) => {
- if (!dead && res.ok) setDetail(res.data)
- })
- return () => {
- dead = true
- }
- }, [facet.routerId])
- const r = detail?.router
- // Prefer the live detail status; fall back to the facet pointer until
- // the fetch lands so the header never flickers the wrong state.
- const online = r ? r.status === 'online' : facet.online
- return (
- <div className="border-t border-border" data-testid="device-facet-panel">
- <div className="px-4 py-4 md:px-5">
- <div className="flex flex-wrap items-center justify-between gap-2">
- <div className="flex items-center gap-2">
- <Activity className="h-4 w-4 text-text-muted" strokeWidth={1.75} aria-hidden="true" />
- <SectionHeader title={facet.label} />
- </div>
- <StatusPill
- tone={online ? 'ok' : 'warn'}
- label={online ? t('devices.facet.online', { label: facet.label }) : t('devices.facet.down', { label: facet.label })}
- />
- </div>
- <div className="mt-3 flex flex-wrap items-center gap-2">
- <Vital label="Uptime" value={r ? fmtUptime(r.uptime) : null} />
- <Vital label="CPU" value={r && r.cpu !== null ? `${Math.round(r.cpu)}%` : null} />
- <Vital label="RAM" value={r && r.ram !== null ? `${Math.round(r.ram)}%` : null} />
- <Vital label="Temp" value={r && r.temp !== null ? `${Math.round(r.temp)}°C` : null} />
- {facet.workloadsKnown && (
- <Vital label="Workloads" value={String(facet.workloadCount ?? 0)} />
- )}
- </div>
- </div>
- {detail?.workloads && <WorkloadsPanel info={detail.workloads} />}
- </div>
- )
- }
|