This commit is contained in:
薇薇安 2026-01-22 22:03:25 +08:00
parent 1c1580b344
commit c581174747
2 changed files with 21 additions and 49 deletions

View File

@ -4,7 +4,7 @@ import { api } from '../services/api'
import { import {
setAccountId, setAccountId,
setAccounts, setAccounts,
selectFirstActiveAccount, selectFirstAccount,
selectAccountId, selectAccountId,
selectAccounts, selectAccounts,
selectCurrentUser, selectCurrentUser,
@ -38,9 +38,9 @@ const AccountSelector = ({ onChanged }) => {
user_id: item.user_id user_id: item.user_id
})) }))
dispatch(setAccounts(accountsList)) dispatch(setAccounts(accountsList))
// active // disabled
if (accountsList.length > 0) { if (accountsList.length > 0) {
dispatch(selectFirstActiveAccount()) dispatch(selectFirstAccount())
} }
}) })
.catch(() => dispatch(setAccounts([]))) .catch(() => dispatch(setAccounts([])))
@ -66,9 +66,9 @@ const AccountSelector = ({ onChanged }) => {
user_id: item.user_id user_id: item.user_id
})) }))
dispatch(setAccounts(accountsList)) dispatch(setAccounts(accountsList))
// active // disabled
if (accountsList.length > 0) { if (accountsList.length > 0) {
dispatch(selectFirstActiveAccount()) dispatch(selectFirstAccount())
} }
}) })
.catch(() => dispatch(setAccounts([]))) .catch(() => dispatch(setAccounts([])))
@ -115,28 +115,19 @@ const AccountSelector = ({ onChanged }) => {
return return
} }
// active //
const currentAccount = options.find((a) => a.id === accountId) const currentAccount = options.find((a) => a.id === accountId)
if (currentAccount) { if (currentAccount) {
// disabled active // disabled
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))
}
}
return return
} }
// active // disabled
const firstActive = options.find((a) => String(a?.status || 'active') === 'active') const firstAccount = options[0]
if (firstActive) { if (firstAccount) {
dispatch(setAccountId(parseInt(String(firstActive.id || ''), 10))) dispatch(setAccountId(parseInt(String(firstAccount.id || ''), 10)))
} else { } else {
// active accountId // accountId
dispatch(setAccountId(null)) dispatch(setAccountId(null))
} }
}, [optionsKey, accountId, dispatch]) }, [optionsKey, accountId, dispatch])
@ -150,12 +141,7 @@ const AccountSelector = ({ onChanged }) => {
onChange={(e) => { onChange={(e) => {
const v = parseInt(e.target.value, 10) const v = parseInt(e.target.value, 10)
if (Number.isFinite(v) && v > 0) { if (Number.isFinite(v) && v > 0) {
// disabled // disabled
const selectedAccount = options.find((a) => a.id === v)
if (selectedAccount && String(selectedAccount?.status || 'active') === 'disabled') {
// disabled
return
}
dispatch(setAccountId(v)) dispatch(setAccountId(v))
} else { } else {
dispatch(setAccountId(null)) dispatch(setAccountId(null))
@ -166,24 +152,11 @@ const AccountSelector = ({ onChanged }) => {
> >
{options.length === 0 ? ( {options.length === 0 ? (
<option value="">{isAdmin && !effectiveUserId ? '请先选择用户' : '暂无账号'}</option> <option value="">{isAdmin && !effectiveUserId ? '请先选择用户' : '暂无账号'}</option>
) : accountId === null ? (
<>
<option value="">请选择账号</option>
{options.map((a) => {
const isDisabled = String(a?.status || 'active') === 'disabled'
return (
<option key={a.id} value={a.id} disabled={isDisabled}>
#{a.id} {a.name || 'account'}
{isDisabled ? '(已禁用)' : ''}
</option>
)
})}
</>
) : ( ) : (
options.map((a) => { options.map((a) => {
const isDisabled = String(a?.status || 'active') === 'disabled' const isDisabled = String(a?.status || 'active') === 'disabled'
return ( return (
<option key={a.id} value={a.id} disabled={isDisabled}> <option key={a.id} value={a.id}>
#{a.id} {a.name || 'account'} #{a.id} {a.name || 'account'}
{isDisabled ? '(已禁用)' : ''} {isDisabled ? '(已禁用)' : ''}
</option> </option>

View File

@ -103,12 +103,11 @@ const appSlice = createSlice({
// 账号列表会在组件中异步加载这里不自动切换accountId // 账号列表会在组件中异步加载这里不自动切换accountId
// 等待账号列表加载完成后再切换 // 等待账号列表加载完成后再切换
}, },
// 切换用户后账号列表加载完成自动选择第一个active账号 // 切换用户后账号列表加载完成自动选择第一个账号不管是否disabled
// 如果没有 active 账号,不改变当前 accountId保持为 null 或之前的值) selectFirstAccount: (state) => {
selectFirstActiveAccount: (state) => { const firstAccount = state.accounts[0]
const firstActive = state.accounts.find((a) => String(a?.status || 'active') === 'active') if (firstAccount) {
if (firstActive) { const nextAccountId = parseInt(String(firstAccount.id || ''), 10)
const nextAccountId = parseInt(String(firstActive.id || ''), 10)
if (Number.isFinite(nextAccountId) && nextAccountId > 0) { if (Number.isFinite(nextAccountId) && nextAccountId > 0) {
state.accountId = nextAccountId state.accountId = nextAccountId
try { try {
@ -124,7 +123,7 @@ const appSlice = createSlice({
} }
} }
} else { } else {
// 如果没有 active 账号,清空 accountId // 如果没有账号,清空 accountId
state.accountId = null state.accountId = null
try { try {
localStorage.removeItem(ACCOUNT_ID_STORAGE_KEY) localStorage.removeItem(ACCOUNT_ID_STORAGE_KEY)
@ -143,7 +142,7 @@ export const {
setAccounts, setAccounts,
setUsers, setUsers,
switchUser, switchUser,
selectFirstActiveAccount, selectFirstAccount,
} = appSlice.actions } = appSlice.actions
// Selectors // Selectors