import React, { useEffect, useState } from "react"; import { TabProvider } from "@/shell/TabContext.tsx"; import { CommandHistoryProvider } from "@/features/terminal/command-history/CommandHistoryContext.tsx"; import { SidebarProvider } from "@/components/sidebar.tsx"; import { getSSHHosts, getUserInfo } from "@/main-axios.ts"; import type { SSHHost } from "@/types"; import { Dashboard } from "@/dashboard/Dashboard.tsx"; import { Toaster } from "@/components/sonner.tsx"; import { dbHealthMonitor } from "@/lib/db-health-monitor.ts"; import { useTranslation } from "react-i18next"; import { RefreshCw } from "lucide-react"; interface FullScreenAppWrapperProps { hostId?: string; children: (hostConfig: SSHHost | null, loading: boolean) => React.ReactNode; } export const FullScreenAppWrapper: React.FC = ({ hostId, children, }) => { const { t } = useTranslation(); const [hostConfig, setHostConfig] = useState(null); const [loading, setLoading] = useState(true); const [isAuthenticated, setIsAuthenticated] = useState(false); const [authLoading, setAuthLoading] = useState(true); const [, setIsAdmin] = useState(false); useEffect(() => { const handleSessionExpired = () => { setIsAuthenticated(false); setIsAdmin(false); setHostConfig(null); }; dbHealthMonitor.on("session-expired", handleSessionExpired); return () => dbHealthMonitor.off("session-expired", handleSessionExpired); }, []); useEffect(() => { const checkAuth = async () => { try { const userInfo = await getUserInfo(); if (userInfo) { setIsAuthenticated(true); } } catch { setIsAuthenticated(false); } finally { setAuthLoading(false); } }; checkAuth(); }, []); useEffect(() => { const fetchHost = async () => { if (!hostId || !isAuthenticated) { setLoading(false); return; } try { const hosts = await getSSHHosts(); const host = hosts.find((h) => h.id === parseInt(hostId, 10)); if (host) { setHostConfig(host); } } catch (error) { console.error("Failed to fetch host:", error); } finally { setLoading(false); } }; if (!authLoading && isAuthenticated) { fetchHost(); } }, [hostId, isAuthenticated, authLoading]); const handleAuthSuccess = () => { setIsAuthenticated(true); window.location.reload(); }; if (authLoading) { return (

{t("common.loading")}

); } if (!isAuthenticated) { return (
{}} />
); } return (
{children(hostConfig, loading)}
); };