import React, { useState, useEffect } from "react"; import { LeftSidebar } from "@/ui/Desktop/Navigation/LeftSidebar.tsx"; import { Dashboard } from "@/ui/Desktop/Apps/Dashboard/Dashboard.tsx"; import { AppView } from "@/ui/Desktop/Navigation/AppView.tsx"; import { HostManager } from "@/ui/Desktop/Apps/Host Manager/HostManager.tsx"; import { TabProvider, useTabs, } from "@/ui/Desktop/Navigation/Tabs/TabContext.tsx"; import { TopNavbar } from "@/ui/Desktop/Navigation/TopNavbar.tsx"; import { AdminSettings } from "@/ui/Desktop/Admin/AdminSettings.tsx"; import { UserProfile } from "@/ui/Desktop/User/UserProfile.tsx"; import { Toaster } from "@/components/ui/sonner.tsx"; import { VersionCheckModal } from "@/components/ui/version-check-modal.tsx"; import { getUserInfo } from "@/ui/main-axios.ts"; function AppContent() { const [isAuthenticated, setIsAuthenticated] = useState(false); const [username, setUsername] = useState(null); const [isAdmin, setIsAdmin] = useState(false); const [authLoading, setAuthLoading] = useState(true); const [showVersionCheck, setShowVersionCheck] = useState(true); const [isTopbarOpen, setIsTopbarOpen] = useState(() => { const saved = localStorage.getItem("topNavbarOpen"); return saved !== null ? JSON.parse(saved) : true; }); const { currentTab, tabs } = useTabs(); useEffect(() => { const checkAuth = () => { setAuthLoading(true); getUserInfo() .then((meRes) => { setIsAuthenticated(true); setIsAdmin(!!meRes.is_admin); setUsername(meRes.username || null); if (!meRes.data_unlocked) { console.warn("User data is locked - re-authentication required"); setIsAuthenticated(false); setIsAdmin(false); setUsername(null); } }) .catch((err) => { setIsAuthenticated(false); setIsAdmin(false); setUsername(null); const errorCode = err?.response?.data?.code; if (errorCode === "SESSION_EXPIRED") { console.warn("Session expired - please log in again"); } }) .finally(() => setAuthLoading(false)); }; checkAuth(); const handleStorageChange = () => checkAuth(); window.addEventListener("storage", handleStorageChange); return () => window.removeEventListener("storage", handleStorageChange); }, []); useEffect(() => { localStorage.setItem("topNavbarOpen", JSON.stringify(isTopbarOpen)); }, [isTopbarOpen]); const handleSelectView = () => { // View switching is now handled by tabs context }; 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 === "user_profile"; return (
{showVersionCheck && ( setShowVersionCheck(false)} onContinue={() => setShowVersionCheck(false)} isAuthenticated={isAuthenticated} /> )} {!isAuthenticated && !authLoading && !showVersionCheck && (
)} {!isAuthenticated && !authLoading && !showVersionCheck && (
)} {isAuthenticated && (
{showHome && (
)} {showSshManager && (
)} {showAdmin && (
)} {showProfile && (
)}
)}
); } function DesktopApp() { return ( ); } export default DesktopApp;