/**
* 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 (
{label}{value}
)
}
export function DeviceFacetPanel({ facet }: { facet: DeviceFacet }) {
const { t } = useTranslation()
const [detail, setDetail] = useState(null)
useEffect(() => {
let dead = false
void fetchJson(`/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 (