| import { createBrowserRouter, RouterProvider } from 'react-router-dom'; |
|
|
| import Layout from './components/layout/Layout'; |
| import Dashboard from './pages/Dashboard'; |
| import Accounts from './pages/Accounts'; |
| import Settings from './pages/Settings'; |
| import ApiProxy from './pages/ApiProxy'; |
| import Monitor from './pages/Monitor'; |
| import TokenStats from './pages/TokenStats'; |
| import Security from './pages/Security'; |
| import ThemeManager from './components/common/ThemeManager'; |
| import UserToken from './pages/UserToken'; |
| import { UpdateNotification } from './components/UpdateNotification'; |
| import DebugConsole from './components/debug/DebugConsole'; |
| import { useEffect, useState } from 'react'; |
| import { useConfigStore } from './stores/useConfigStore'; |
| import { useAccountStore } from './stores/useAccountStore'; |
| import { useTranslation } from 'react-i18next'; |
| import { listen } from '@tauri-apps/api/event'; |
| import { isTauri } from './utils/env'; |
| import { request as invoke } from './utils/request'; |
| import { AdminAuthGuard } from './components/common/AdminAuthGuard'; |
|
|
| const router = createBrowserRouter([ |
| { |
| path: '/', |
| element: <Layout />, |
| children: [ |
| { |
| index: true, |
| element: <Dashboard />, |
| }, |
| { |
| path: 'accounts', |
| element: <Accounts />, |
| }, |
| { |
| path: 'api-proxy', |
| element: <ApiProxy />, |
| }, |
| { |
| path: 'monitor', |
| element: <Monitor />, |
| }, |
| { |
| path: 'token-stats', |
| element: <TokenStats />, |
| }, |
| { |
| path: 'user-token', |
| element: <UserToken />, |
| }, |
| { |
| path: 'security', |
| element: <Security />, |
| }, |
| { |
| path: 'settings', |
| element: <Settings />, |
| }, |
| ], |
| }, |
| ]); |
|
|
| function App() { |
| const { config, loadConfig } = useConfigStore(); |
| const { fetchCurrentAccount, fetchAccounts } = useAccountStore(); |
| const { i18n } = useTranslation(); |
|
|
| useEffect(() => { |
| loadConfig(); |
| }, [loadConfig]); |
|
|
| |
| useEffect(() => { |
| if (config?.language) { |
| i18n.changeLanguage(config.language); |
| |
| if (config.language === 'ar') { |
| document.documentElement.dir = 'rtl'; |
| } else { |
| document.documentElement.dir = 'ltr'; |
| } |
| } |
| }, [config?.language, i18n]); |
|
|
| |
| useEffect(() => { |
| if (!isTauri()) return; |
| const unlistenPromises: Promise<() => void>[] = []; |
|
|
| |
| unlistenPromises.push( |
| listen('tray://account-switched', () => { |
| console.log('[App] Tray account switched, refreshing...'); |
| fetchCurrentAccount(); |
| fetchAccounts(); |
| }) |
| ); |
|
|
| |
| unlistenPromises.push( |
| listen('tray://refresh-current', () => { |
| console.log('[App] Tray refresh triggered, refreshing...'); |
| fetchCurrentAccount(); |
| fetchAccounts(); |
| }) |
| ); |
|
|
| |
| unlistenPromises.push( |
| listen('accounts://refreshed', () => { |
| console.log('[App] Backend triggered quota refresh, syncing UI...'); |
| fetchCurrentAccount(); |
| fetchAccounts(); |
| }) |
| ); |
|
|
| |
| return () => { |
| Promise.all(unlistenPromises).then(unlisteners => { |
| unlisteners.forEach(unlisten => unlisten()); |
| }); |
| }; |
| }, [fetchCurrentAccount, fetchAccounts]); |
|
|
| |
| const [showUpdateNotification, setShowUpdateNotification] = useState(false); |
|
|
| |
| useEffect(() => { |
| const checkUpdates = async () => { |
| try { |
| console.log('[App] Checking if we should check for updates...'); |
| const shouldCheck = await invoke<boolean>('should_check_updates'); |
| console.log('[App] Should check updates:', shouldCheck); |
|
|
| if (shouldCheck) { |
| setShowUpdateNotification(true); |
| |
| |
| await invoke('update_last_check_time'); |
| console.log('[App] Update check cycle initiated and last check time updated.'); |
| } |
| } catch (error) { |
| console.error('Failed to check update settings:', error); |
| } |
| }; |
|
|
| |
| const timer = setTimeout(checkUpdates, 2000); |
| return () => clearTimeout(timer); |
| }, []); |
|
|
| return ( |
| <AdminAuthGuard> |
| <ThemeManager /> |
| <DebugConsole /> |
| {showUpdateNotification && ( |
| <UpdateNotification onClose={() => setShowUpdateNotification(false)} /> |
| )} |
| <RouterProvider router={router} /> |
| </AdminAuthGuard> |
| ); |
| } |
|
|
| export default App; |