import React, { useEffect } from 'react' import { useDispatch, useSelector } from 'react-redux' import { api } from '../services/api' import { setAccountId, setAccounts, selectFirstAccount, selectAccountId, selectAccounts, selectCurrentUser, selectIsAdmin, } from '../store/appSlice' const AccountSelector = ({ onChanged }) => { const dispatch = useDispatch() const accountId = useSelector(selectAccountId) const accounts = useSelector(selectAccounts) const currentUser = useSelector(selectCurrentUser) const isAdmin = useSelector(selectIsAdmin) // 加载当前用户的账号列表 useEffect(() => { const load = () => { api.getAccounts() .then((list) => { const accountsList = Array.isArray(list) ? list : [] dispatch(setAccounts(accountsList)) // 如果当前没有选中的账号,或者选中的账号不在列表中,自动选择第一个账号 if (accountsList.length > 0) { const currentAccount = accountsList.find((a) => a.id === accountId) if (!currentAccount) { dispatch(selectFirstAccount()) } } }) .catch(() => dispatch(setAccounts([]))) } load() // 配置页创建/更新账号后会触发该事件,用于即时刷新下拉列表 const onUpdated = () => load() window.addEventListener('ats:accounts:updated', onUpdated) return () => window.removeEventListener('ats:accounts:updated', onUpdated) }, [dispatch, accountId]) // 当 accountId 变化时,调用 onChanged 回调 useEffect(() => { if (typeof onChanged === 'function') { onChanged(accountId) } }, [accountId, onChanged]) const list = Array.isArray(accounts) ? accounts : [] const options = list.reduce((acc, cur) => { if (!cur || !cur.id) return acc if (acc.some((x) => x.id === cur.id)) return acc acc.push(cur) return acc }, []) const optionsKey = options.map((x) => x.id).join(',') useEffect(() => { if (!options.length) { // 如果没有账号,清空 accountId if (accountId) { dispatch(setAccountId(null)) } return } // 检查当前选中的账号是否在新列表中 const currentAccount = options.find((a) => a.id === accountId) if (currentAccount) { // 账号在新列表中,保持选中(不管是否disabled) return } // 如果当前账号不在新列表中,选择第一个账号(不管是否disabled) const firstAccount = options[0] if (firstAccount) { dispatch(setAccountId(parseInt(String(firstAccount.id || ''), 10))) } else { // 如果没有账号,清空 accountId dispatch(setAccountId(null)) } }, [optionsKey, accountId, dispatch]) return (