import { useCallback, useEffect, useRef, useState } from "react"; import { Archive, BarChart3, Bot, Bug, Gauge, Globe, Loader2, Puzzle, Server, Settings, Swords, Terminal, Users, RotateCcw, type LucideIcon } from "lucide-react"; import { api, type ServerSummary } from "@/lib/api"; import { useT, type Key } from "@/i18n"; import logo from "@/assets/logo.png"; import { cn } from "@/lib/utils"; import { Overview } from "@/screens/Overview"; import { Placeholder } from "@/screens/Placeholder"; import { SettingsPage } from "@/screens/SettingsPage"; import { BackupsPage } from "@/screens/BackupsPage"; import { PlayersPage } from "@/screens/PlayersPage"; import { SettingsHome } from "@/screens/SettingsHome"; import { PlayWithFriends } from "@/screens/PlayWithFriends"; import { ReportPage } from "@/screens/ReportPage"; import { ModulesPage } from "@/screens/ModulesPage"; import { ConsolePage } from "@/screens/ConsolePage"; import { ServerMigrationWizard } from "@/screens/ServerMigrationWizard"; import { PortablePage } from "@/screens/PortablePage"; import { DashboardPage } from "@/screens/DashboardPage"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { startServerUpdatePolling, useServerUpdate } from "@/lib/serverUpdate"; import { startClientPolling } from "@/lib/clientUpdate"; import type { UnsavedChangesGuard } from "@/lib/unsavedGuard"; type Page = "overview" | "bots" | "dashboard" | "server" | "modules" | "players" | "characters" | "friends" | "backups" | "console" | "report" | "settings" | "migration"; const NAV: { id: Page; label: Key; icon: LucideIcon; question: Key }[] = [ { id: "overview", label: "nav.overview", icon: Gauge, question: "q.overview" }, { id: "bots", label: "nav.bots", icon: Bot, question: "q.bots" }, { id: "dashboard", label: "nav.dashboard", icon: BarChart3, question: "q.dashboard" }, { id: "server", label: "nav.server", icon: Server, question: "q.server" }, { id: "modules", label: "nav.modules", icon: Puzzle, question: "q.modules" }, { id: "players", label: "nav.players", icon: Users, question: "q.players" }, { id: "characters", label: "nav.characters", icon: Swords, question: "q.characters" }, { id: "friends", label: "nav.friends", icon: Globe, question: "q.friends" }, { id: "backups", label: "nav.backups", icon: Archive, question: "q.backups" }, { id: "migration", label: "nav.migration", icon: RotateCcw, question: "q.migration" }, { id: "console", label: "nav.console", icon: Terminal, question: "q.console" }, { id: "report", label: "nav.report", icon: Bug, question: "q.report" }, { id: "settings", label: "nav.settings", icon: Settings, question: "q.settings" }, ]; export function Shell(props: { servers: ServerSummary[]; activeId: string; onSelect: (id: string) => void; onAddAnother: () => void; /** Make a modular copy of a classic server (the install screen takes over). */ onCopy: (path: string) => void; onForget: (id: string) => Promise; }) { const t = useT(); const [page, setPage] = useState("overview"); const server = props.servers.find((s) => s.id === props.activeId)!; const current = NAV.find((n) => n.id === page)!; const update = useServerUpdate(props.activeId); // A module that is switched off takes its page away (the companions' "Bots" page, for one). const [botModule, setBotModule] = useState<"companions" | "playerbots">("companions"); const [hiddenPages, setHiddenPages] = useState([]); const guardRef = useRef(null); const registerGuard = useCallback((g: UnsavedChangesGuard | null) => { guardRef.current = g; }, []); const [pendingNav, setPendingNav] = useState<(() => void) | null>(null); const [modalSaving, setModalSaving] = useState(false); const [modalError, setModalError] = useState(null); const requestNavigation = (action: () => void) => { if (guardRef.current && guardRef.current.isDirty()) { setPendingNav(() => action); } else { action(); } }; const reloadModules = useCallback(() => { void api .modulesList(props.activeId) .then((list) => { const available = (m: typeof list[number]) => m.installed && m.compatibility !== "unsupported" && (!m.switchable || m.enabled); setHiddenPages([...new Set(list.flatMap(m => m.page ? [m.page] : []))].filter(page => !list.some(m => m.page === page && available(m)))); setBotModule(list.some(m => m.id === "playerbots" && available(m)) ? "playerbots" : "companions"); }) .catch(() => setHiddenPages([])); }, [props.activeId]); useEffect(() => { reloadModules(); }, [reloadModules]); useEffect(() => { if (hiddenPages.includes(page)) setPage("overview"); }, [hiddenPages, page]); // Look for a server and a game client update when the app starts and then every five minutes. useEffect(() => { const stopServer = startServerUpdatePolling(props.activeId); const stopClient = startClientPolling(props.activeId); return () => { stopServer(); stopClient(); }; }, [props.activeId]); return (
{page === "overview" ? ( props.onForget(server.id)} onOpenUpdates={() => requestNavigation(() => setPage("settings"))} onRealmChanged={reloadModules} onReport={() => requestNavigation(() => setPage("report"))} /> ) : page === "bots" || page === "server" ? ( ) : page === "dashboard" ? ( ) : page === "console" ? ( ) : page === "modules" ? ( ) : page === "report" ? ( ) : page === "friends" ? ( ) : page === "settings" ? ( props.onForget(server.id)} /> ) : page === "characters" ? ( ) : page === "players" ? ( ) : page === "migration" ? ( ) : page === "backups" ? ( ) : ( )}
{pendingNav && (

{t("unsaved.title")}

{t("unsaved.message")}

{modalError &&

{modalError}

}
)}
); }