import React, {useEffect, useState} from "react"; import {HomepageAuth} from "@/ui/Desktop/Homepage/HomepageAuth.tsx"; import {HomepageUpdateLog} from "@/ui/Desktop/Homepage/HompageUpdateLog.tsx"; import {HomepageAlertManager} from "@/ui/Desktop/Homepage/HomepageAlertManager.tsx"; import {Button} from "@/components/ui/button.tsx"; import { getUserInfo, getDatabaseHealth, setCookie, getCookie } from "@/ui/main-axios.ts"; import {useTranslation} from "react-i18next"; interface HomepageProps { onSelectView: (view: string) => void; isAuthenticated: boolean; authLoading: boolean; onAuthSuccess: (authData: { isAdmin: boolean; username: string | null; userId: string | null }) => void; isTopbarOpen: boolean; } export function Homepage({ onSelectView, isAuthenticated, authLoading, onAuthSuccess, isTopbarOpen }: HomepageProps): React.ReactElement { const {t} = useTranslation(); const [loggedIn, setLoggedIn] = useState(isAuthenticated); const [isAdmin, setIsAdmin] = useState(false); const [username, setUsername] = useState(null); const [userId, setUserId] = useState(null); const [dbError, setDbError] = useState(null); // Calculate margins based on topbar state (same logic as AppView.tsx) const topMarginPx = isTopbarOpen ? 74 : 26; const leftMarginPx = 26; // Assuming sidebar is collapsed for homepage const bottomMarginPx = 8; useEffect(() => { setLoggedIn(isAuthenticated); }, [isAuthenticated]); useEffect(() => { if (isAuthenticated) { const jwt = getCookie("jwt"); if (jwt) { Promise.all([ getUserInfo(), getDatabaseHealth() ]) .then(([meRes]) => { setIsAdmin(!!meRes.is_admin); setUsername(meRes.username || null); setUserId(meRes.id || null); setDbError(null); }) .catch((err) => { setIsAdmin(false); setUsername(null); setUserId(null); if (err?.response?.data?.error?.includes("Database")) { setDbError("Could not connect to the database. Please try again later."); } else { setDbError(null); } }); } } }, [isAuthenticated]); return ( <> {!loggedIn ? (
) : (
)} ); }