diff --git a/frontend/src/components/GlobalConfig.jsx b/frontend/src/components/GlobalConfig.jsx index 43a21d0..3b1e5cb 100644 --- a/frontend/src/components/GlobalConfig.jsx +++ b/frontend/src/components/GlobalConfig.jsx @@ -38,6 +38,9 @@ const GlobalConfig = ({ currentUser }) => { 'ENTRY_MAX_DRIFT_PCT_RANGING', ]) + // 必须在所有使用之前定义 isAdmin + const isAdmin = (currentUser?.role || '') === 'admin' + // 预设方案配置(必须在函数定义之前,常量定义) const presets = { swing: { @@ -684,22 +687,52 @@ const GlobalConfig = ({ currentUser }) => { } } - // 必须在所有使用之前定义 isAdmin - const isAdmin = (currentUser?.role || '') === 'admin' - if (loading) { return
加载中...
} - // 简单计算:全局策略账号ID - const globalStrategyAccountId = configMeta?.global_strategy_account_id ? parseInt(String(configMeta.global_strategy_account_id), 10) : 1 + // 简单计算:全局策略账号ID(在 render 时计算) + const globalStrategyAccountId = configMeta?.global_strategy_account_id + ? parseInt(String(configMeta.global_strategy_account_id), 10) + : 1 const isGlobalStrategyAccount = isAdmin && currentAccountId === globalStrategyAccountId // 简单计算:当前预设(直接在 render 时计算,不使用 useMemo) let currentPreset = null - if (configs && Object.keys(configs).length > 0) { + if (configs && Object.keys(configs).length > 0 && presets) { try { - currentPreset = detectCurrentPreset() + // 直接内联检测逻辑,避免函数调用 + for (const [presetKey, preset] of Object.entries(presets)) { + let match = true + for (const [key, expectedValue] of Object.entries(preset.configs)) { + const currentConfig = configs[key] + if (!currentConfig) { + match = false + break + } + let currentValue = currentConfig.value + if (key.includes('PERCENT') || key.includes('PCT')) { + if (PCT_LIKE_KEYS.has(key)) { + currentValue = currentValue <= 0.05 ? currentValue * 100 : currentValue + } else { + currentValue = currentValue * 100 + } + } + if (typeof expectedValue === 'number' && typeof currentValue === 'number') { + if (Math.abs(currentValue - expectedValue) > 0.01) { + match = false + break + } + } else if (currentValue !== expectedValue) { + match = false + break + } + } + if (match) { + currentPreset = presetKey + break + } + } } catch (e) { console.error('detectCurrentPreset error:', e) } @@ -856,7 +889,7 @@ const GlobalConfig = ({ currentUser }) => {
当前方案: - {currentPreset ? presets[currentPreset].name : '自定义'} + {currentPreset && presets && presets[currentPreset] ? presets[currentPreset].name : '自定义'}
@@ -884,15 +917,20 @@ const GlobalConfig = ({ currentUser }) => {
{g.presetKeys - .filter((k) => presets[k]) + .filter((k) => presets && presets[k]) .map((k) => { - const preset = presets[k] - const meta = presetUiMeta[k] || { group: g.key, tag: '' } + const preset = presets && presets[k] ? presets[k] : null + if (!preset) return null + const meta = presetUiMeta && presetUiMeta[k] ? presetUiMeta[k] : { group: g.key, tag: '' } return ( ) - })} + }) + .filter(Boolean)}
))}