a
This commit is contained in:
parent
1c1580b344
commit
c581174747
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user