diff --git a/changes/affinity-dialog-keep-draft.json b/changes/affinity-dialog-keep-draft.json new file mode 100644 index 0000000..6f0c6b3 --- /dev/null +++ b/changes/affinity-dialog-keep-draft.json @@ -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." +} diff --git a/frontend/src/components/AffinityDialog.tsx b/frontend/src/components/AffinityDialog.tsx index 3ad63a7..df8cc9e 100644 --- a/frontend/src/components/AffinityDialog.tsx +++ b/frontend/src/components/AffinityDialog.tsx @@ -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 = ({ const tr = (k: I18nKey, v?: Record) => 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(null) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [err, setErr] = useState('') + const [reloadSeq, setReloadSeq] = useState(0) const [mode, setMode] = useState('auto') const [selected, setSelected] = useState(allIndices) @@ -98,6 +101,7 @@ export const AffinityDialog: React.FC = ({ useEffect(() => { let live = true + setStatus(null) setLoading(true) setErr('') api.GetVMAffinity(driverID, vmName) @@ -107,7 +111,7 @@ export const AffinityDialog: React.FC = ({ 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 = ({ 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 = ({ } const toggleCPU = (idx: number) => { + if (locked) return setMode('custom') setErr('') setSelected((prev) => { @@ -161,6 +169,7 @@ export const AffinityDialog: React.FC = ({ } 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 = ({ 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 = { @@ -199,6 +208,7 @@ export const AffinityDialog: React.FC = ({ 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 = ({ + )} + + )}
@@ -370,7 +389,7 @@ export const AffinityDialog: React.FC = ({