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 {
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 ? (
<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) => {
const isDisabled = String(a?.status || 'active') === 'disabled'
return (
<option key={a.id} value={a.id} disabled={isDisabled}>
<option key={a.id} value={a.id}>
#{a.id} {a.name || 'account'}
{isDisabled ? '(已禁用)' : ''}
</option>

View File

@ -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