import { useState, useEffect, useMemo, memo } from "react"; import { motion } from "framer-motion"; import { useUI, useConfig, useAudio, useGame, } from "../../context/LauncherContext"; import type { Edition } from "../../types/edition"; const HomeView = memo(function HomeView() { const { setActiveView, setShowCredits, focusSection, onNavigateToSkin } = useUI(); const { profile, legacyMode } = useConfig(); const { playPressSound, playSfx } = useAudio(); const { handleLaunch, editions, installs, toggleInstall, downloadingId, isGameRunning, stopGame, updatesAvailable, } = useGame(); const isFocusedSection = focusSection === "menu"; const selectedEdition = editions.find((e: Edition) => e.id === profile); const selectedVersionName = selectedEdition?.name || "Game"; const isInstalled = installs.includes(profile); const isDownloading = downloadingId === profile; const [menuFocus, setMenuFocus] = useState(null); const hasAnyInstall = installs.length > 0; const buttons = useMemo( () => [ { label: !hasAnyInstall ? "Install a version" : isGameRunning ? "Stop Game" : isDownloading ? "Installation in progress..." : isInstalled ? "Play Game" : `Download ${selectedVersionName}`, action: !hasAnyInstall ? () => setActiveView("versions") : isGameRunning ? stopGame : isDownloading ? () => {} : isInstalled ? handleLaunch : () => toggleInstall(profile), isDanger: isGameRunning, disabled: isDownloading, }, { label: "Help & Options", action: () => setActiveView("settings"), disabled: false, id: "settings", }, { label: "Versions", action: () => setActiveView("versions"), disabled: false, id: "versions", }, { label: "Workshop", action: () => setActiveView("workshop"), disabled: false, id: "workshop", }, { label: "Developer Tools", action: () => setActiveView("devtools"), disabled: false, id: "devtools", }, ], [ isDownloading, hasAnyInstall, isInstalled, selectedVersionName, handleLaunch, toggleInstall, profile, setActiveView, isGameRunning, stopGame, ], ); useEffect(() => { if (!isFocusedSection) { setMenuFocus(null); return; } const handleKeyDown = (e: KeyboardEvent) => { if (document.activeElement?.tagName === "INPUT") return; if (e.key === "ArrowDown") setMenuFocus((prev) => prev === null ? 0 : prev < buttons.length - 1 ? prev + 1 : prev, ); if (e.key === "ArrowUp") setMenuFocus((prev) => prev === null ? buttons.length - 1 : prev > 0 ? prev - 1 : prev, ); if (e.key === "ArrowLeft") onNavigateToSkin(); if (e.key === "Enter" && menuFocus !== null) { buttons[menuFocus].action(); } }; window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, [menuFocus, buttons, playPressSound, isFocusedSection, onNavigateToSkin]); return ( {buttons.map((btn: { label: string; action: () => void; isDanger?: boolean; disabled: boolean; id?: string }, i: number) => (
))} {!legacyMode && (
{ if (isFocusedSection) playPressSound(); }} className={`hover:scale-110 transition-transform ${!isFocusedSection ? "pointer-events-none" : ""}`} > { if (isFocusedSection) playPressSound(); }} className={`hover:scale-110 transition-transform ${!isFocusedSection ? "pointer-events-none" : ""}`} >
)} ); }); export default HomeView;