import { useState, useEffect, useMemo, memo } from "react"; import { motion } from "framer-motion"; import { useUI, useConfig, useAudio, useGame, } from "../../context/LauncherContext"; import { usePluginActions } from "../../plugins/PluginContext"; import { usePlatform } from "../../hooks/usePlatform"; import type { Edition } from "../../types/edition"; const HomeView = memo(function HomeView() { const { setActiveView, focusSection, onNavigateToSkin } = useUI(); const { profile, legacyMode } = useConfig(); const { playPressSound } = useAudio(); const { handleLaunch, editions, installs, toggleInstall, downloadingIds, isGameRunning, stopGame, updatesAvailable, } = useGame(); const pluginActions = usePluginActions("home-menu"); const { isAndroid } = usePlatform(); 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 = downloadingIds.includes(profile); const [menuFocus, setMenuFocus] = useState(null); const hasAnyInstall = installs.length > 0; const buttonsVal = useMemo(() => { const mainBtn = { 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, id: "main-action", }; const pluginBtns = pluginActions.map((a) => ({ label: a.label, action: () => a.onClick(), isDanger: false, disabled: false, id: a.id, })); const menuBtns = [ { label: "Help & Options", action: () => setActiveView("settings"), isDanger: false, disabled: false, id: "settings", }, { label: "Versions", action: () => setActiveView("versions"), isDanger: false, disabled: false, id: "versions", }, { label: "Workshop", action: () => setActiveView("workshop"), isDanger: false, disabled: false, id: "workshop", }, { label: "Developer Tools", action: () => setActiveView("devtools"), isDanger: false, disabled: false, id: "devtools", }, ].filter((b) => !(isAndroid && b.id === "devtools")); return [mainBtn, ...pluginBtns, ...menuBtns]; }, [ isDownloading, hasAnyInstall, isInstalled, selectedVersionName, handleLaunch, toggleInstall, profile, setActiveView, isGameRunning, stopGame, pluginActions, isAndroid, ]); useEffect(() => { if (!isFocusedSection) { setMenuFocus(null); return; } const handleKey = (e: KeyboardEvent) => { if (document.activeElement?.tagName === "INPUT") return; if (e.key === "ArrowDown") setMenuFocus((prev) => prev === null ? 0 : prev < buttonsVal.length - 1 ? prev + 1 : prev, ); if (e.key === "ArrowUp") setMenuFocus((prev) => prev === null ? buttonsVal.length - 1 : prev > 0 ? prev - 1 : prev, ); if (e.key === "ArrowLeft") onNavigateToSkin(); if (e.key === "Enter" && menuFocus !== null) buttonsVal[menuFocus].action(); }; window.addEventListener("keydown", handleKey); return () => window.removeEventListener("keydown", handleKey); }, [menuFocus, buttonsVal, isFocusedSection, onNavigateToSkin]); return ( {buttonsVal.map((btn, i) => (
))} {!legacyMode && (
{ if (isFocusedSection) playPressSound(); }} className={`${!isFocusedSection ? "pointer-events-none" : ""}`} > { if (isFocusedSection) playPressSound(); }} className={`${!isFocusedSection ? "pointer-events-none" : ""}`} >
)} ); }); export default HomeView;