fix(ui): keep unsaved CPU affinity edits across host refreshes (#178)
* fix(ui): keep unsaved CPU affinity edits across host refreshes * fix(ui): offer retry button when AffinityDialog initial load fails
This commit is contained in:
2 files changed
+44
-18
No files matched your search
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"group": "fixes",
|
||||
"order": 0,
|
||||
"authors": ["reiasu2"],
|
||||
"zh": "修复在处理器核心弹窗中勾选核心、切换模式或调整 EcoQoS 开关后,未保存前因后台状态刷新自动跳回旧配置的问题。",
|
||||
"en": "Preserve unsaved core selections, mode changes, and EcoQoS toggles in the CPU cores dialog when background host refreshes occur."
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { AlertTriangle, Check, Cpu } from 'lucide-react'
|
||||
import React, { useEffect, useMemo, useState } from 'react'
|
||||
import React, { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import '../i18n/domains/settings.gen'
|
||||
import { I18nKey, parseAppError, t } from '../i18n'
|
||||
import { HostReport, Locale, LogicalCPUInfo, VMAffinityMode, VMAffinityStatus } from '../types'
|
||||
@@ -85,12 +85,15 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
const tr = (k: I18nKey, v?: Record<string, string | number>) => t(locale, k, v)
|
||||
const topology = useMemo(() => buildTopology(host), [host])
|
||||
const allIndices = useMemo(() => topology.map((c) => c.index), [topology])
|
||||
const allIndicesRef = useRef(allIndices)
|
||||
allIndicesRef.current = allIndices
|
||||
const hasSMT = useMemo(() => topology.some((c) => c.smt), [topology])
|
||||
|
||||
const [status, setStatus] = useState<VMAffinityStatus | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [err, setErr] = useState('')
|
||||
const [reloadSeq, setReloadSeq] = useState(0)
|
||||
|
||||
const [mode, setMode] = useState<VMAffinityMode>('auto')
|
||||
const [selected, setSelected] = useState<number[]>(allIndices)
|
||||
@@ -98,6 +101,7 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
|
||||
useEffect(() => {
|
||||
let live = true
|
||||
setStatus(null)
|
||||
setLoading(true)
|
||||
setErr('')
|
||||
api.GetVMAffinity(driverID, vmName)
|
||||
@@ -107,7 +111,7 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
setMode(st.mode || 'auto')
|
||||
const initial = st.mode === 'custom' && st.cpus?.length > 0
|
||||
? st.cpus
|
||||
: (st.effective_cpus?.length > 0 ? st.effective_cpus : allIndices)
|
||||
: (st.effective_cpus?.length > 0 ? st.effective_cpus : allIndicesRef.current)
|
||||
setSelected([...initial].sort((a, b) => a - b))
|
||||
setDisableEcoQoS(st.disable_eco_qos)
|
||||
})
|
||||
@@ -118,13 +122,16 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
if (live) setLoading(false)
|
||||
})
|
||||
return () => { live = false }
|
||||
}, [vmName, allIndices])
|
||||
}, [driverID, vmName, reloadSeq])
|
||||
|
||||
const locked = loading || saving || !status
|
||||
const loadFailed = !loading && !status && !!err
|
||||
const selectedSet = useMemo(() => new Set(selected), [selected])
|
||||
const effectiveVCPUs = status?.vcpus || vcpus
|
||||
const maskHex = useMemo(() => maskHexFromCPUs(selected), [selected])
|
||||
|
||||
const applyPreset = (nextMode: VMAffinityMode) => {
|
||||
if (locked) return
|
||||
setMode(nextMode)
|
||||
setErr('')
|
||||
switch (nextMode) {
|
||||
@@ -150,6 +157,7 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
}
|
||||
|
||||
const toggleCPU = (idx: number) => {
|
||||
if (locked) return
|
||||
setMode('custom')
|
||||
setErr('')
|
||||
setSelected((prev) => {
|
||||
@@ -161,6 +169,7 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
}
|
||||
|
||||
const pickCustomList = (list: number[]) => {
|
||||
if (locked) return
|
||||
setMode('custom')
|
||||
setErr('')
|
||||
setSelected([...new Set(list)].sort((a, b) => a - b))
|
||||
@@ -178,12 +187,12 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
return out
|
||||
}, [topology])
|
||||
|
||||
const modeOptions: { value: VMAffinityMode; label: string }[] = [
|
||||
{ value: 'auto', label: tr('affinityModeAuto') },
|
||||
{ value: 'all', label: tr('affinityModeAll') },
|
||||
...(host.has_hybrid_cores ? [{ value: 'pcores' as const, label: tr('affinityModePCores') }] : []),
|
||||
...((host.lpe_core_count ?? 0) > 0 ? [{ value: 'non_lpe' as const, label: tr('affinityModeNonLPE') }] : []),
|
||||
{ value: 'custom', label: tr('affinityModeCustom') },
|
||||
const modeOptions: { value: VMAffinityMode; label: string; disabled?: boolean }[] = [
|
||||
{ value: 'auto', label: tr('affinityModeAuto'), disabled: locked },
|
||||
{ value: 'all', label: tr('affinityModeAll'), disabled: locked },
|
||||
...(host.has_hybrid_cores ? [{ value: 'pcores' as const, label: tr('affinityModePCores'), disabled: locked }] : []),
|
||||
...((host.lpe_core_count ?? 0) > 0 ? [{ value: 'non_lpe' as const, label: tr('affinityModeNonLPE'), disabled: locked }] : []),
|
||||
{ value: 'custom', label: tr('affinityModeCustom'), disabled: locked },
|
||||
]
|
||||
|
||||
const modeHintKey: Record<VMAffinityMode, I18nKey> = {
|
||||
@@ -199,6 +208,7 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
const allSelected = selected.length === allIndices.length && allIndices.length > 0
|
||||
|
||||
const handleSave = async () => {
|
||||
if (locked) return
|
||||
if (emptyCustom) {
|
||||
setErr(tr('affinityNeedOne'))
|
||||
return
|
||||
@@ -254,7 +264,7 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
<button
|
||||
type="button"
|
||||
className={`affinity-chip${allSelected ? ' is-active' : ''}`}
|
||||
disabled={loading || saving}
|
||||
disabled={locked}
|
||||
onClick={() => pickCustomList(allSelected ? [allIndices[0]] : allIndices)}
|
||||
>
|
||||
{tr('affinitySelectAll')}
|
||||
@@ -263,7 +273,7 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
<button
|
||||
type="button"
|
||||
className="affinity-chip"
|
||||
disabled={loading || saving}
|
||||
disabled={locked}
|
||||
onClick={() => pickCustomList(physicalOnlyList)}
|
||||
>
|
||||
{tr('affinityPhysicalOnly')}
|
||||
@@ -273,7 +283,7 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
<button
|
||||
type="button"
|
||||
className="affinity-chip"
|
||||
disabled={loading || saving}
|
||||
disabled={locked}
|
||||
onClick={() => pickCustomList(allIndices.filter((i) => i !== 0))}
|
||||
>
|
||||
{tr('affinityExcludeCPU0')}
|
||||
@@ -283,7 +293,7 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
<button
|
||||
type="button"
|
||||
className="affinity-chip"
|
||||
disabled={loading || saving}
|
||||
disabled={locked}
|
||||
onClick={() => pickCustomList(topology.filter((c) => c.tier === 'p').map((c) => c.index))}
|
||||
>
|
||||
{tr('affinityOnlyPCores')}
|
||||
@@ -293,7 +303,7 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
<button
|
||||
type="button"
|
||||
className="affinity-chip"
|
||||
disabled={loading || saving}
|
||||
disabled={locked}
|
||||
onClick={() => pickCustomList(topology.filter((c) => c.tier !== 'lpe').map((c) => c.index))}
|
||||
>
|
||||
{tr('affinityAvoidLPE')}
|
||||
@@ -311,7 +321,7 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
type="button"
|
||||
role="checkbox"
|
||||
aria-checked={checked}
|
||||
disabled={loading || saving}
|
||||
disabled={locked}
|
||||
className={`affinity-cpu${checked ? ' is-checked' : ''}${cpu.tier === 'lpe' ? ' is-lpe' : ''}`}
|
||||
onClick={() => toggleCPU(cpu.index)}
|
||||
>
|
||||
@@ -357,12 +367,21 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
<Switch
|
||||
label={tr('affinityEcoQoS')}
|
||||
checked={disableEcoQoS}
|
||||
disabled={loading || saving}
|
||||
disabled={locked}
|
||||
onChange={setDisableEcoQoS}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{err && <div className="notice notice-error" role="alert">{err}</div>}
|
||||
{err && (
|
||||
<div className="notice notice-error" role="alert">
|
||||
<span className="notice-main">{err}</span>
|
||||
{loadFailed && (
|
||||
<Button onClick={() => setReloadSeq((n) => n + 1)}>
|
||||
{tr('installersRetry')}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="dialog-foot">
|
||||
@@ -370,7 +389,7 @@ export const AffinityDialog: React.FC<Props> = ({
|
||||
<Button
|
||||
variant="primary"
|
||||
busy={saving}
|
||||
disabled={loading || emptyCustom}
|
||||
disabled={loading || !status || emptyCustom}
|
||||
onClick={() => void handleSave()}
|
||||
>
|
||||
{status?.running ? tr('affinityApplyNow') : tr('affinitySave')}
|
||||
|
||||
Reference in new issue
Block a user