DeviceFacetPanel.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * DeviceFacetPanel - NETPULSE-ONE-LEGION.
  3. *
  4. * An agent row the fleet declared `facet` of this device is not a box:
  5. * it is another face of the host (the RKE2 agent on the legion machine).
  6. * The server drops it from routers[]/topology and hangs a DeviceFacet
  7. * pointer on the parent device; this panel is where the folded agent's
  8. * truth lives - vitals straight from the agent row plus its workload
  9. * census (the #2227 WorkloadsPanel, reused untouched).
  10. *
  11. * When the agent stops pushing, the device carries the badge (no ghost
  12. * box returns); the panel keeps its last good census visible and says
  13. * so, mirroring the anti-blink rule of the push contract.
  14. */
  15. import { useEffect, useState } from 'react'
  16. import { useTranslation } from 'react-i18next'
  17. import { Activity } from 'lucide-react'
  18. import type { DeviceFacet, Router, WorkloadsInfo } from '@/data/types'
  19. import { fetchJson } from '@/lib/utils'
  20. import { SectionHeader } from '@/components/SectionHeader'
  21. import { StatusPill } from '@/components/StatusPill'
  22. import { WorkloadsPanel } from '@/components/routers/WorkloadsPanel'
  23. import { fmtUptime } from '@/i18n'
  24. interface FacetDetailResponse {
  25. router?: Router
  26. workloads?: WorkloadsInfo
  27. }
  28. /** One vitals chip; null values (pusher without the section) are skipped,
  29. * never painted as zero - the #441 law for external units. */
  30. function Vital({ label, value }: { label: string; value: string | null }) {
  31. if (value === null) return null
  32. return (
  33. <span className="inline-flex items-center gap-1.5 rounded-full bg-elevated px-2.5 py-1 text-caption text-text-secondary">
  34. <span className="uppercase tracking-wide text-text-muted">{label}</span>
  35. <span className="font-mono text-mono-sm text-text-primary">{value}</span>
  36. </span>
  37. )
  38. }
  39. export function DeviceFacetPanel({ facet }: { facet: DeviceFacet }) {
  40. const { t } = useTranslation()
  41. const [detail, setDetail] = useState<FacetDetailResponse | null>(null)
  42. useEffect(() => {
  43. let dead = false
  44. void fetchJson<FacetDetailResponse>(`/api/routers/${encodeURIComponent(facet.routerId)}`).then((res) => {
  45. if (!dead && res.ok) setDetail(res.data)
  46. })
  47. return () => {
  48. dead = true
  49. }
  50. }, [facet.routerId])
  51. const r = detail?.router
  52. // Prefer the live detail status; fall back to the facet pointer until
  53. // the fetch lands so the header never flickers the wrong state.
  54. const online = r ? r.status === 'online' : facet.online
  55. return (
  56. <div className="border-t border-border" data-testid="device-facet-panel">
  57. <div className="px-4 py-4 md:px-5">
  58. <div className="flex flex-wrap items-center justify-between gap-2">
  59. <div className="flex items-center gap-2">
  60. <Activity className="h-4 w-4 text-text-muted" strokeWidth={1.75} aria-hidden="true" />
  61. <SectionHeader title={facet.label} />
  62. </div>
  63. <StatusPill
  64. tone={online ? 'ok' : 'warn'}
  65. label={online ? t('devices.facet.online', { label: facet.label }) : t('devices.facet.down', { label: facet.label })}
  66. />
  67. </div>
  68. <div className="mt-3 flex flex-wrap items-center gap-2">
  69. <Vital label="Uptime" value={r ? fmtUptime(r.uptime) : null} />
  70. <Vital label="CPU" value={r && r.cpu !== null ? `${Math.round(r.cpu)}%` : null} />
  71. <Vital label="RAM" value={r && r.ram !== null ? `${Math.round(r.ram)}%` : null} />
  72. <Vital label="Temp" value={r && r.temp !== null ? `${Math.round(r.temp)}°C` : null} />
  73. {facet.workloadsKnown && (
  74. <Vital label="Workloads" value={String(facet.workloadCount ?? 0)} />
  75. )}
  76. </div>
  77. </div>
  78. {detail?.workloads && <WorkloadsPanel info={detail.workloads} />}
  79. </div>
  80. )
  81. }