diff --git a/frontend/src/components/AccountSelector.jsx b/frontend/src/components/AccountSelector.jsx index 1d0ca9d..4fd6df4 100644 --- a/frontend/src/components/AccountSelector.jsx +++ b/frontend/src/components/AccountSelector.jsx @@ -4,7 +4,7 @@ import { api } from '../services/api' import { setAccountId, setAccounts, - selectFirstActiveAccount, + selectFirstAccount, selectAccountId, selectAccounts, selectCurrentUser, @@ -38,9 +38,9 @@ const AccountSelector = ({ onChanged }) => { user_id: item.user_id })) dispatch(setAccounts(accountsList)) - // 自动选择第一个active账号 + // 自动选择第一个账号(不管是否disabled) if (accountsList.length > 0) { - dispatch(selectFirstActiveAccount()) + dispatch(selectFirstAccount()) } }) .catch(() => dispatch(setAccounts([]))) @@ -66,9 +66,9 @@ const AccountSelector = ({ onChanged }) => { user_id: item.user_id })) dispatch(setAccounts(accountsList)) - // 自动选择第一个active账号 + // 自动选择第一个账号(不管是否disabled) if (accountsList.length > 0) { - dispatch(selectFirstActiveAccount()) + dispatch(selectFirstAccount()) } }) .catch(() => dispatch(setAccounts([]))) @@ -115,28 +115,19 @@ const AccountSelector = ({ onChanged }) => { return } - // 检查当前选中的账号是否在新列表中且是 active 的 + // 检查当前选中的账号是否在新列表中 const currentAccount = options.find((a) => a.id === accountId) if (currentAccount) { - // 如果当前账号是 disabled,需要切换到 active 账号 - if (String(currentAccount?.status || 'active') === 'disabled') { - const firstActive = options.find((a) => String(a?.status || 'active') === 'active') - if (firstActive) { - dispatch(setAccountId(parseInt(String(firstActive.id || ''), 10))) - } else { - // 如果没有 active 账号,清空 accountId - dispatch(setAccountId(null)) - } - } + // 账号在新列表中,保持选中(不管是否disabled) return } - // 如果当前账号不在新列表中,选择第一个 active 账号 - const firstActive = options.find((a) => String(a?.status || 'active') === 'active') - if (firstActive) { - dispatch(setAccountId(parseInt(String(firstActive.id || ''), 10))) + // 如果当前账号不在新列表中,选择第一个账号(不管是否disabled) + const firstAccount = options[0] + if (firstAccount) { + dispatch(setAccountId(parseInt(String(firstAccount.id || ''), 10))) } else { - // 如果没有 active 账号,清空 accountId + // 如果没有账号,清空 accountId dispatch(setAccountId(null)) } }, [optionsKey, accountId, dispatch]) @@ -150,12 +141,7 @@ const AccountSelector = ({ onChanged }) => { onChange={(e) => { const v = parseInt(e.target.value, 10) if (Number.isFinite(v) && v > 0) { - // 检查选中的账号是否是 disabled - const selectedAccount = options.find((a) => a.id === v) - if (selectedAccount && String(selectedAccount?.status || 'active') === 'disabled') { - // 如果选中的是 disabled 账号,不允许选择,保持当前值 - return - } + // 允许选择任何账号(包括disabled),由各个页面根据账号状态决定是否展示内容 dispatch(setAccountId(v)) } else { dispatch(setAccountId(null)) @@ -166,24 +152,11 @@ const AccountSelector = ({ onChanged }) => { > {options.length === 0 ? ( - ) : accountId === null ? ( - <> - - {options.map((a) => { - const isDisabled = String(a?.status || 'active') === 'disabled' - return ( - - ) - })} - ) : ( options.map((a) => { const isDisabled = String(a?.status || 'active') === 'disabled' return ( - diff --git a/frontend/src/store/appSlice.js b/frontend/src/store/appSlice.js index 0a9a483..c45b968 100644 --- a/frontend/src/store/appSlice.js +++ b/frontend/src/store/appSlice.js @@ -103,12 +103,11 @@ const appSlice = createSlice({ // 账号列表会在组件中异步加载,这里不自动切换accountId // 等待账号列表加载完成后再切换 }, - // 切换用户后,账号列表加载完成,自动选择第一个active账号 - // 如果没有 active 账号,不改变当前 accountId(保持为 null 或之前的值) - selectFirstActiveAccount: (state) => { - const firstActive = state.accounts.find((a) => String(a?.status || 'active') === 'active') - if (firstActive) { - const nextAccountId = parseInt(String(firstActive.id || ''), 10) + // 切换用户后,账号列表加载完成,自动选择第一个账号(不管是否disabled) + selectFirstAccount: (state) => { + const firstAccount = state.accounts[0] + if (firstAccount) { + const nextAccountId = parseInt(String(firstAccount.id || ''), 10) if (Number.isFinite(nextAccountId) && nextAccountId > 0) { state.accountId = nextAccountId try { @@ -124,7 +123,7 @@ const appSlice = createSlice({ } } } else { - // 如果没有 active 账号,清空 accountId + // 如果没有账号,清空 accountId state.accountId = null try { localStorage.removeItem(ACCOUNT_ID_STORAGE_KEY) @@ -143,7 +142,7 @@ export const { setAccounts, setUsers, switchUser, - selectFirstActiveAccount, + selectFirstAccount, } = appSlice.actions // Selectors