import { useEffect, useState } from "react"; import { toast } from "sonner"; import { api } from "../../api/client.js"; import { ConfirmDialog } from "../common/ConfirmDialog.js"; import { GaugeCard } from "../Monitor/GaugeCard.js"; import { fmt } from "../../utils/units.js"; import type { WireViewFlashProgress, WireViewFirmwareState, WireViewInfo, WireViewSample, } from "../../types.js"; interface Props { info: WireViewInfo | null; sample: WireViewSample | null; history: WireViewSample[]; flash: WireViewFlashProgress; } // "v05 (TG-WV-PRO2-FW_20260902_0741)" — version padded like the official app. function formatFw(version: number | null, build: string | null): string { const v = version !== null ? `v${String(version).padStart(2, "0")}` : "?"; return build ? `${v} (${build})` : v; } // Per-pin current rating of the 12 VHPWR connector (A). The bar's // full scale is 10 A (rating + headroom); red at/above the 9.2 A rating. const PIN_RATED_A = 9.2; const PIN_BAR_MAX_A = 10; const FAULT_NAMES: Record = { 0: "Chip over-temperature", 1: "Sensor over-temperature", 2: "Over-current (OCP)", 3: "Wire over-current", 4: "Over-power (OPP)", 5: "Current imbalance", }; function decodeFaults(mask: number): string[] { return Object.entries(FAULT_NAMES) .filter(([bit]) => mask & (1 << Number(bit))) .map(([, name]) => name); } function pluck(history: WireViewSample[], key: keyof WireViewSample): number[] { return history.map((s) => (s[key] as number | null) ?? 0); } function FaultBadge({ label, mask, onClear, clearing, }: { label: string; mask: number; onClear?: () => void; clearing?: boolean; }) { const faults = decodeFaults(mask); return (
0 ? "bg-red-500/10 border-red-500/40" : "bg-zinc-900 border-zinc-800" }`} >
{label}
{mask !== 0 && onClear && ( )} 0 ? "text-red-400" : "text-zinc-600" }`} > 0x{mask.toString(16).toUpperCase().padStart(4, "0")}
{faults.length > 0 ? (
{faults.map((f) => ( {f} ))}
) : (
No faults
)}
); } function PinCard({ index, voltage, current, power, }: { index: number; voltage: number; current: number; power: number; }) { const loadPct = Math.min(100, (current / PIN_BAR_MAX_A) * 100); const barColor = current >= PIN_RATED_A ? "#f87171" : loadPct >= 75 ? "#fbbf24" : "#34d399"; return (
Pin {index + 1} {current.toFixed(1)} / {PIN_RATED_A} A
V
{voltage.toFixed(2)}
A
{current.toFixed(2)}
W
{power.toFixed(1)}
); } function InfoItem({ label, value, }: { label: string; value: string | null | undefined; }) { if (!value) return null; return (
{label} {value}
); } export function WireViewPanel({ info, sample, history, flash }: Props) { const [clearing, setClearing] = useState<"status" | "log" | null>(null); const [firmware, setFirmware] = useState(null); const [firmwareError, setFirmwareError] = useState(null); const [confirming, setConfirming] = useState( null, ); const [starting, setStarting] = useState(false); const flashActive = flash.state === "bootloader" || flash.state === "waiting" || flash.state === "flashing"; async function refreshFirmware() { try { setFirmware(await api.wireviewFirmware()); setFirmwareError(null); } catch (err) { setFirmwareError( err instanceof Error ? err.message : "Failed to load firmware state", ); } } const hasSample = sample !== null; // Fetch the firmware check when a device appears (the panel early-returns // without a sample, so no reset is needed when the device goes away). useEffect(() => { // setState calls happen after the await, not synchronously in the effect // body (rule false-positive on async fetch). // eslint-disable-next-line react-hooks/set-state-in-effect if (hasSample) refreshFirmware(); }, [hasSample]); // After a flash the device reboots and re-enumerates; re-check once it is // back so the verdict reflects the new firmware. useEffect(() => { if (flash.state !== "done" && flash.state !== "failed") return; const t = setTimeout(() => { refreshFirmware(); }, 3000); return () => clearTimeout(t); }, [flash.state]); async function startFlash(override: boolean) { setConfirming(null); setStarting(true); try { await api.wireviewFlashFirmware(override); toast.success("Firmware update started"); } catch (err) { toast.error( err instanceof Error ? err.message : "Failed to start firmware update", ); } finally { setStarting(false); } } async function handleClear(which: "status" | "log") { setClearing(which); try { // Keep-mask semantics: 0x0000 clears, 0xFFFF keeps. await api.wireviewClearFault( which === "status" ? 0x0000 : 0xffff, which === "log" ? 0x0000 : 0xffff, ); toast.success( which === "status" ? "Fault status cleared" : "Fault log cleared", ); } catch (err) { toast.error( err instanceof Error ? err.message : "Failed to clear fault", ); } finally { setClearing(null); } } if (!sample) { return (
WireView Pro II not connected
Plug in the device — the tab appears automatically once it is detected.
); } const psuCap = sample.psu_capability_w > 0 ? sample.psu_capability_w : 600; return (
{/* ── Header ─────────────────────────────────────────────────── */}
{info?.device_name ?? "WireView Pro II"}
{info?.hw_rev ? `HW ${info.hw_rev}` : ""} {info?.firmware_version ? ` · FW ${info.firmware_version}` : ""} PSU {psuCap} W {info?.transport === "hwmon" ? `hwmon: ${info.port}` : `serial: ${info?.port ?? ""}`}
{/* ── Main gauges ────────────────────────────────────────────── */}
12 VHPWR Connector
{/* ── Per-pin ────────────────────────────────────────────────── */}
Per-Pin Readings
{sample.pins.map((pin, i) => ( ))}
{/* ── Temperatures ───────────────────────────────────────────── */}
Temperatures
{/* ── Faults + device info ───────────────────────────────────── */}
handleClear("status")} clearing={clearing === "status"} /> handleClear("log")} clearing={clearing === "log"} />
Device
{/* ── Firmware ─────────────────────────────────────────── */}
Firmware
Device {firmware?.device.connected ? formatFw(firmware.device.version, firmware.device.build) : "not connected"}
Bundled image {firmware?.image.available ? formatFw(firmware.image.version, firmware.image.build) : firmware?.image.error ?? "missing"}
{firmwareError && (
{firmwareError}
)} {firmware && (
{(() => { const v = firmware.verdict; const text = v === "newer" ? { cls: "text-emerald-400", msg: "Newer firmware available." } : v === "same" ? { cls: "text-zinc-400", msg: "Device already runs this firmware build." } : v === "unknown" ? { cls: "text-zinc-400", msg: "Firmware comparison unknown (no build date)." } : v === "downgrade" ? { cls: "text-amber-400", msg: "Bundled firmware is older than the device firmware." } : v === "product_mismatch" ? { cls: "text-red-400", msg: "The bundled firmware does not match this device." } : v === "too_old_for_noctua" ? { cls: "text-red-400", msg: "The bundled firmware predates Noctua Edition support." } : { cls: "text-zinc-500", msg: "" }; return text.msg ? (
{text.msg}
) : null; })()} {firmware.device.connected && firmware.device.transport !== "serial" && (
Flashing requires the serial transport (wireviewd owns the port in hwmon mode).
)} {firmware.dfu_util === null && (
dfu-util not installed — install the "dfu-util" package to enable flashing.
)} {flashActive || starting ? (
{flash.status || "Starting…"} {Math.round(flash.progress * 100)}%
) : ( firmware.device.connected && firmware.device.transport === "serial" && firmware.image.available && firmware.dfu_util !== null && firmware.verdict !== "product_mismatch" && firmware.verdict !== "too_old_for_noctua" && ( ) )}
)}
{/* Confirm dialogs */} {confirming !== null && firmware && ( startFlash(confirming === "override")} onCancel={() => setConfirming(null)} /> )}
); }