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

View File

@@ -142,8 +142,8 @@ export function NetworkMetrics() {
error,
isLoading,
} = useSWR<NetworkData>("/api/network", fetcher, {
refreshInterval: 53000,
revalidateOnFocus: false,
refreshInterval: 15000,
revalidateOnFocus: true,
revalidateOnReconnect: true,
})

View File

@@ -149,7 +149,7 @@ export function StorageOverview() {
useEffect(() => {
fetchStorageData()
const interval = setInterval(fetchStorageData, 60000)
const interval = setInterval(fetchStorageData, 30000)
return () => clearInterval(interval)
}, [])

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")