diff --git a/frontend/src/components/AccountSelector.jsx b/frontend/src/components/AccountSelector.jsx index aa4e5e9..500ec5d 100644 --- a/frontend/src/components/AccountSelector.jsx +++ b/frontend/src/components/AccountSelector.jsx @@ -27,12 +27,20 @@ const AccountSelector = ({ onChanged, currentUser, viewingUserId: propViewingUse user_id: item.user_id })) setAccounts(accountsList) - // 自动选择第一个active账号 + // 自动选择第一个active账号,并立即同步到localStorage和触发事件 const firstActive = accountsList.find((a) => String(a?.status || 'active') === 'active') || accountsList[0] if (firstActive) { const nextAccountId = parseInt(String(firstActive.id || ''), 10) if (Number.isFinite(nextAccountId) && nextAccountId > 0) { + // 立即更新localStorage和触发事件,确保其他页面能同步 + setCurrentAccountId(nextAccountId) setAccountId(nextAccountId) + // 立即触发事件,确保其他页面能立即响应 + try { + window.dispatchEvent(new CustomEvent('ats:account:changed', { detail: { accountId: nextAccountId } })) + } catch (e) { + // ignore + } } } }) @@ -59,12 +67,20 @@ const AccountSelector = ({ onChanged, currentUser, viewingUserId: propViewingUse user_id: item.user_id })) setAccounts(accountsList) - // 自动选择第一个active账号 + // 自动选择第一个active账号,并立即同步到localStorage和触发事件 const firstActive = accountsList.find((a) => String(a?.status || 'active') === 'active') || accountsList[0] if (firstActive) { const nextAccountId = parseInt(String(firstActive.id || ''), 10) if (Number.isFinite(nextAccountId) && nextAccountId > 0) { + // 立即更新localStorage和触发事件,确保其他页面能同步 + setCurrentAccountId(nextAccountId) setAccountId(nextAccountId) + // 立即触发事件,确保其他页面能立即响应 + try { + window.dispatchEvent(new CustomEvent('ats:account:changed', { detail: { accountId: nextAccountId } })) + } catch (e) { + // ignore + } } } })