update virtual-machines.tsx

This commit is contained in:
MacRimi
2026-04-17 17:01:24 +02:00
parent dc8ebb651a
commit 5398211ab5
4 changed files with 86 additions and 6 deletions
+30
View File
@@ -423,6 +423,36 @@ export function VirtualMachines() {
}
}, [])
// Keep the open modal's VM in sync with the 5s poll of /api/vms so CPU/RAM/I-O
// values don't stay frozen at click-time while the user has the modal open.
useEffect(() => {
if (!selectedVM || !vmData) return
const updated = vmData.find((v) => v.vmid === selectedVM.vmid)
if (!updated) return
// Avoid unnecessary setState when no field changed (reference-equal shortcut first).
if (updated === selectedVM) return
setSelectedVM(updated)
}, [vmData])
// Faster per-VM live status poll that only runs while the modal is open.
// SWR disables polling when the key is null, so this is truly scoped to the modal.
const { data: liveVMStatus } = useSWR<VMData>(
selectedVM ? `/api/vms/${selectedVM.vmid}/status` : null,
fetcher,
{
refreshInterval: 2500,
revalidateOnFocus: true,
revalidateOnReconnect: true,
dedupingInterval: 1000,
},
)
useEffect(() => {
if (!liveVMStatus || !selectedVM) return
if (liveVMStatus.vmid !== selectedVM.vmid) return
setSelectedVM((prev) => (prev ? { ...prev, ...liveVMStatus } : prev))
}, [liveVMStatus])
const handleVMClick = async (vm: VMData) => {
setSelectedVM(vm)
setCurrentView("main")