import React, { useState, useEffect } from 'react'; import Sidebar from './components/Sidebar'; import Dashboard from './components/Dashboard'; import AccountList from './components/AccountList'; import OrderList from './components/OrderList'; import CardList from './components/CardList'; import ItemList from './components/ItemList'; import Settings from './components/Settings'; import Rules from './components/Rules'; import Notifications from './components/Notifications'; import Chat from './components/Chat'; import { readSidebarCollapsed, writeSidebarCollapsed } from './components/sidebarState'; import { YdisksBrandIcon } from './components/YdisksLogo'; import { initializeAdmin, login, logout, verifySession } from './app/features/session/api'; import { ShieldCheck, ArrowRight, Loader2, User, Lock } from 'lucide-react'; interface DeliveryRuleTarget { cookieId: string; itemId: string; requestId: number; } // 路由:URL path ↔ tab id。所有 SPA 路由统一挂 /app/ 前缀,避免和后端 API // 路径(/orders、/cards、/items 等)冲突——后者在 chi 里先注册,刷新会直接 // 返回 JSON 而不是 SPA 页面。 const ROUTES: Record = { '/app/dashboard': 'dashboard', '/app/accounts': 'accounts', '/app/chat': 'chat', '/app/orders': 'orders', '/app/cards': 'cards', '/app/items': 'items', '/app/rules': 'rules', '/app/notifications': 'notifications', '/app/settings': 'settings', }; const TAB_TO_PATH: Record = Object.fromEntries( Object.entries(ROUTES).map(([path, tab]) => [tab, path]) ); const tabFromPath = (): string => ROUTES[window.location.pathname] || 'dashboard'; const App: React.FC = () => { const [isLoggedIn, setIsLoggedIn] = useState(false); const [isAdmin, setIsAdmin] = useState(false); const [activeTab, setActiveTab] = useState(tabFromPath); const [checkingAuth, setCheckingAuth] = useState(true); const [needsInit, setNeedsInit] = useState(false); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [loginLoading, setLoginLoading] = useState(false); const [loginError, setLoginError] = useState(''); const [initialPassword, setInitialPassword] = useState(''); const [initialPasswordConfirm, setInitialPasswordConfirm] = useState(''); const [initializing, setInitializing] = useState(false); const [initializationError, setInitializationError] = useState(''); const [deliveryRuleTarget, setDeliveryRuleTarget] = useState(); const [sidebarCollapsed, setSidebarCollapsed] = useState(readSidebarCollapsed); // 切换 tab 并同步 URL。若 tab 没有对应 path(不应发生)则只切 tab。 const navigate = (tab: string) => { const nextTab = tab === 'settings' && !isAdmin ? 'dashboard' : tab; const path = TAB_TO_PATH[nextTab]; if (path && path !== window.location.pathname) { window.history.pushState({}, '', path); } setActiveTab(nextTab); }; // 浏览器后退/前进同步 tab。 useEffect(() => { const onPopState = () => setActiveTab(tabFromPath()); window.addEventListener('popstate', onPopState); return () => window.removeEventListener('popstate', onPopState); }, []); // Check auth on mount useEffect(() => { verifySession() .then((res) => { if (res?.initialized === false) { setNeedsInit(true); setIsLoggedIn(false); setIsAdmin(false); return; } setNeedsInit(false); if (res?.authenticated) { setIsLoggedIn(true); setIsAdmin(res.is_admin === true); } else { setIsAdmin(false); } }) .catch(() => { setIsLoggedIn(false); setIsAdmin(false); }) .finally(() => setCheckingAuth(false)); const handleAuthLogoutEvent = () => { setIsLoggedIn(false); setIsAdmin(false); }; window.addEventListener('auth:logout', handleAuthLogoutEvent); return () => window.removeEventListener('auth:logout', handleAuthLogoutEvent); }, []); useEffect(() => { if (!checkingAuth && isLoggedIn && !isAdmin && activeTab === 'settings') { window.history.replaceState({}, '', TAB_TO_PATH.dashboard); setActiveTab('dashboard'); } }, [checkingAuth, isLoggedIn, isAdmin, activeTab]); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setLoginLoading(true); setLoginError(''); try { const res = await login({ username, password }); if (res.success) { setIsLoggedIn(true); setIsAdmin(res.is_admin === true); } else { setLoginError(res.message || '登录失败'); } } catch (err) { const msg = err instanceof Error ? err.message : String(err); setLoginError(msg || '登录失败'); } finally { setLoginLoading(false); } }; const handleInitialize = async (e: React.FormEvent) => { e.preventDefault(); setInitializationError(''); if (initialPassword.length < 8) { setInitializationError('密码至少需要 8 个字符'); return; } if (initialPassword !== initialPasswordConfirm) { setInitializationError('两次输入的密码不一致'); return; } setInitializing(true); try { const res = await initializeAdmin(initialPassword); if (!res.success) { setInitializationError(res.message || '初始化失败,请重试'); return; } setNeedsInit(false); setIsLoggedIn(true); setIsAdmin(res.is_admin === true); setInitialPassword(''); setInitialPasswordConfirm(''); } catch (err) { const msg = err instanceof Error ? err.message : String(err); setInitializationError(msg || '初始化失败,请重试'); } finally { setInitializing(false); } }; const handleLogout = async () => { try { await logout(); } catch (err) { console.error('退出登录失败', err); } finally { setIsLoggedIn(false); setIsAdmin(false); } }; if (checkingAuth) { return (
); } // Init Screen (system not initialized) if (needsInit) { return (

首次设置管理员密码

设置完成后会自动进入系统,管理员账号为 admin。

setInitialPassword(e.target.value)} autoFocus className="w-full ios-input pl-14 pr-6 py-4.5 rounded-2xl text-base h-14" />
setInitialPasswordConfirm(e.target.value)} className="w-full ios-input pl-14 pr-6 py-4.5 rounded-2xl text-base h-14" />
{initializationError && (
{initializationError}
)}
Secure Bootstrap
); } // Login Screen Component if (!isLoggedIn) { return (
{/* Animated Background Blobs */}
{/* Header with Logo */}

欢迎回来

Ydisks闲鱼助手 · 自动发货与管家系统

setUsername(e.target.value)} className="w-full ios-input pl-14 pr-6 py-4.5 rounded-2xl text-base h-14" />
setPassword(e.target.value)} className="w-full ios-input pl-14 pr-6 py-4.5 rounded-2xl text-base h-14" />
{loginError && (
{loginError}
)}
Ydisks闲鱼助手 v1.0
); } // Main App Layout const renderContent = () => { switch (activeTab) { case 'dashboard': return ; case 'accounts': return ; case 'chat': return ; case 'orders': return ; case 'cards': return ; case 'items': return { setDeliveryRuleTarget({ cookieId: item.cookie_id, itemId: item.item_id, requestId: Date.now() }); navigate('rules'); }} />; case 'rules': return setDeliveryRuleTarget(undefined)} />; case 'notifications': return ; case 'settings': return isAdmin ? : ; default: return ; } }; return (
setSidebarCollapsed(current => { const next = !current; writeSidebarCollapsed(next); return next; })} onNavigate={navigate} onLogout={handleLogout} />
{/* Subtle background decoration */}
{renderContent()}
); }; export default App;