update virtual-machines.tsx

This commit is contained in:
MacRimi
2026-04-17 17:36:57 +02:00
parent c7b49cfc4a
commit 03850d2958
5 changed files with 95 additions and 105 deletions
+6 -26
View File
@@ -295,10 +295,10 @@ export function VirtualMachines() {
isLoading,
mutate,
} = useSWR<VMData[]>("/api/vms", fetcher, {
refreshInterval: 5000,
refreshInterval: 2500,
revalidateOnFocus: true,
revalidateOnReconnect: true,
dedupingInterval: 2000,
dedupingInterval: 1000,
errorRetryCount: 2,
})
@@ -423,36 +423,16 @@ 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.
// Keep the open modal's VM in sync with the /api/vms poll so CPU/RAM/I-O values
// don't stay frozen at click-time. Single data source (/cluster/resources) shared
// with the list — no source mismatch, no flicker.
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
if (!updated || 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")