import { Suspense, lazy, useEffect } from 'react'; import { connectSSE } from './api/sse'; import { useAuthStore } from './stores/authStore'; import { useMailStore } from './stores/mailStore'; import { useSessionStore } from './stores/sessionStore'; import { useContactStore } from './stores/contactStore'; import { useUIStore } from './stores/uiStore'; import Sidebar from './components/Sidebar'; import MailList from './components/MailList'; import PermissionList from './components/PermissionList'; import ContactPanel from './components/ContactPanel'; import MailView from './components/MailView'; import ComposePage from './components/ComposePage'; import LoginPage from './components/LoginPage'; import SetupPage from './components/SetupPage'; import AccountPage from './components/AccountPage'; import AdminUsersPage from './components/AdminUsersPage'; // 日历懒加载。 // // 它传递依赖 lunar-javascript(node_modules 里 588KB)—— 全量打进主 chunk // 会让**登录页**也背上这份体积(实测主 bundle 447KB → 751KB, // gzip 133 → 239KB)。农历表是一大张查找数据,压不动也没必要提前拉: // 大多数会话根本不打开日历。 const CalendarView = lazy(() => import('./components/CalendarView')); import NarrowNav from './components/NarrowNav'; import NarrowStack from './components/NarrowStack'; import { useIsNarrow } from './hooks/useIsNarrow'; export default function App() { const phase = useAuthStore(s => s.phase); const bootstrap = useAuthStore(s => s.bootstrap); const user = useAuthStore(s => s.user); const viewMode = useUIStore(s => s.viewMode); const composing = useUIStore(s => s.composing); const resetUI = useUIStore(s => s.reset); const narrowPane = useUIStore(s => s.narrowPane); const narrow = useIsNarrow(); const fetchInbox = useMailStore(s => s.fetchInbox); const fetchSent = useMailStore(s => s.fetchSent); const dropMailSession = useMailStore(s => s.dropSession); const fetchSessions = useSessionStore(s => s.fetchSessions); const dropSessionIfCurrent = useSessionStore(s => s.dropSessionIfCurrent); const refreshRenameProposal = useSessionStore(s => s.refreshRenameProposal); const refreshBudget = useSessionStore(s => s.refreshBudget); const fetchContacts = useContactStore(s => s.fetchContacts); const removeSessionLocally = useContactStore(s => s.removeSessionLocally); // 启动时检测初始化状态 / 登录态 useEffect(() => { bootstrap(); }, []); // 登出后清理客户端状态,避免脏数据残留 useEffect(() => { if (phase === 'anonymous') { resetUI(); } }, [phase]); // 登录态就绪后拉取数据 + SSE useEffect(() => { if (phase !== 'authenticated') return; fetchInbox('all'); fetchSent(); fetchSessions(); fetchContacts(); return connectSSE((type, data) => { switch (type) { case 'new_mail': fetchInbox('all'); fetchSessions(); fetchContacts(); // 新来信可能带改名建议;Agent 发信也会消耗本任务的往返预算 refreshRenameProposal(); refreshBudget(); break; case 'session_update': // 别人(或另一个标签页)改了预算/别名 refreshBudget(); fetchInbox('all'); fetchSessions(); fetchContacts(); break; case 'permission_decision': fetchInbox('all'); fetchSessions(); fetchContacts(); break; case 'session_archived': { const id = typeof data.session_id === 'string' ? data.session_id : ''; if (!id) break; removeSessionLocally(id); dropMailSession(id); dropSessionIfCurrent(id); break; } } }); }, [phase]); // loading 阶段 if (phase === 'checking') { return (
加载中
加载中
加载中