import React, { useEffect, useState } from 'react' import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom' import ConfigPanel from './components/ConfigPanel' import ConfigGuide from './components/ConfigGuide' import TradeList from './components/TradeList' import StatsDashboard from './components/StatsDashboard' import Recommendations from './components/Recommendations' import LogMonitor from './components/LogMonitor' import AccountSelector from './components/AccountSelector' import Login from './components/Login' import { api, clearAuthToken, clearCurrentAccountId, setCurrentAccountId, getCurrentAccountId } from './services/api' import './App.css' function App() { const [me, setMe] = useState(null) const [checking, setChecking] = useState(true) const refreshMe = async () => { try { const u = await api.me() setMe(u) // 登录后默认选择账号(避免 admin/用户切换时沿用旧 accountId) try { const list = await api.getAccounts() const accounts = Array.isArray(list) ? list : [] const active = accounts.filter((a) => String(a?.status || 'active') === 'active') const isAdmin = (u?.role || '') === 'admin' let target = null if (isAdmin) { // 管理员:默认选第一个 active 账号(避免沿用旧值导致“看起来还是上个账号”) target = active[0]?.id || accounts[0]?.id } else { // 普通用户:优先选“自己的账号”(且必须 active),否则选第一个 active const uid = parseInt(String(u?.id || ''), 10) const match = active.find((a) => parseInt(String(a?.id || ''), 10) === uid) target = match?.id || active[0]?.id || accounts[0]?.id } if (target) { const cur = getCurrentAccountId() const next = parseInt(String(target), 10) if (Number.isFinite(next) && next > 0 && cur !== next) setCurrentAccountId(next) } } catch (e) { // ignore } } catch (e) { setMe(null) } finally { setChecking(false) } } useEffect(() => { refreshMe() // eslint-disable-next-line react-hooks/exhaustive-deps }, []) const isAdmin = (me?.role || '') === 'admin' if (checking) { return (