import React, {useState, useEffect} from "react" import {LeftSidebar} from "@/ui/Navigation/LeftSidebar.tsx" import {Homepage} from "@/ui/Homepage/Homepage.tsx" import {AppView} from "@/ui/Navigation/AppView.tsx" import {HostManager} from "@/ui/Apps/Host Manager/HostManager.tsx" import {TabProvider, useTabs} from "@/ui/Navigation/Tabs/TabContext.tsx" import {TopNavbar} from "@/ui/Navigation/TopNavbar.tsx"; import { AdminSettings } from "@/ui/Admin/AdminSettings"; import { UserProfile } from "@/ui/User/UserProfile.tsx"; import { Toaster } from "@/components/ui/sonner"; import { getUserInfo } from "@/ui/main-axios.ts"; function getCookie(name: string) { return document.cookie.split('; ').reduce((r, v) => { const parts = v.split('='); return parts[0] === name ? decodeURIComponent(parts[1]) : r; }, ""); } function setCookie(name: string, value: string, days = 7) { const expires = new Date(Date.now() + days * 864e5).toUTCString(); document.cookie = `${name}=${encodeURIComponent(value)}; expires=${expires}; path=/`; } function AppContent() { const [view, setView] = useState("homepage") const [mountedViews, setMountedViews] = useState>(new Set(["homepage"])) const [isAuthenticated, setIsAuthenticated] = useState(false) const [username, setUsername] = useState(null) const [isAdmin, setIsAdmin] = useState(false) const [authLoading, setAuthLoading] = useState(true) const [isTopbarOpen, setIsTopbarOpen] = useState(true) const {currentTab, tabs} = useTabs(); useEffect(() => { const checkAuth = () => { const jwt = getCookie("jwt"); if (jwt) { setAuthLoading(true); getUserInfo() .then((meRes) => { setIsAuthenticated(true); setIsAdmin(!!meRes.is_admin); setUsername(meRes.username || null); }) .catch((err) => { setIsAuthenticated(false); setIsAdmin(false); setUsername(null); document.cookie = 'jwt=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; }) .finally(() => setAuthLoading(false)); } else { setIsAuthenticated(false); setIsAdmin(false); setUsername(null); setAuthLoading(false); } } checkAuth() const handleStorageChange = () => checkAuth() window.addEventListener('storage', handleStorageChange) return () => window.removeEventListener('storage', handleStorageChange) }, []) const handleSelectView = (nextView: string) => { setMountedViews((prev) => { if (prev.has(nextView)) return prev const next = new Set(prev) next.add(nextView) return next }) setView(nextView) } const handleAuthSuccess = (authData: { isAdmin: boolean; username: string | null; userId: string | null }) => { setIsAuthenticated(true) setIsAdmin(authData.isAdmin) setUsername(authData.username) } const currentTabData = tabs.find(tab => tab.id === currentTab); const showTerminalView = currentTabData?.type === 'terminal' || currentTabData?.type === 'server' || currentTabData?.type === 'file_manager'; const showHome = currentTabData?.type === 'home'; const showSshManager = currentTabData?.type === 'ssh_manager'; const showAdmin = currentTabData?.type === 'admin'; const showProfile = currentTabData?.type === 'profile'; return (
{!isAuthenticated && !authLoading && (
)} {!isAuthenticated && !authLoading && (
)} {isAuthenticated && (
)}
) } function App() { return ( ); } export default App