import { createFileRoute, ErrorComponentProps, useRouter } from "@tanstack/react-router"; import { RPC_URL } from "@shared/constants"; import { useRef, useState } from "react"; import { FocusContext, useFocusable } from "@noriginmedia/norigin-spatial-navigation"; import { Calendar, Folder, Gamepad2, Image, Info, TriangleAlert, Trophy } from "lucide-react"; import { HeaderUI, StickyHeaderUI } from "../../components/Header"; import { AnimatedBackground } from "../../components/AnimatedBackground"; import { useQuery } from "@tanstack/react-query"; import Shortcuts, { FloatingShortcuts } from "../../components/Shortcuts"; import { GamePadButtonCode, useShortcutContext, useShortcuts } from "@/mainview/scripts/shortcuts"; import Screenshots from "@/mainview/components/Screenshots"; import { HandleGoBack, scrollIntoViewHandler, useOnNavigateBack } from "@/mainview/scripts/utils"; import { FilterUI } from "@/mainview/components/Filters"; import StatList, { StatEntry } from "@/mainview/components/StatList"; import { useIntersectionObserver, useLocalStorage } from "usehooks-ts"; import { EmulatorsSection } from "@/mainview/components/store/EmulatorsSection"; import { zodValidator } from "@tanstack/zod-adapter"; import z from "zod"; import Achievements from "@/mainview/components/game/Achievements"; import { GameDetailsContext } from "@/mainview/scripts/contexts"; import { gameQuery, gamesRecommendedBasedOnGameQuery } from "@queries/romm"; import { GamesSection } from "@/mainview/components/store/GamesSection"; import Details from "@/mainview/components/game/Details"; import { AutoFocus } from "@/mainview/components/AutoFocus"; import SelectMenu from "@/mainview/components/SelectMenu"; import { en } from "zod/v4/locales"; export const Route = createFileRoute("/game/$source/$id")({ loader: async ({ params, context }) => { context.queryClient.prefetchQuery(gameQuery(params.source, params.id)); }, component: RouteComponent, errorComponent: Error, validateSearch: zodValidator(z.object({ focus: z.string().optional() })), staticData: { enterSound: 'openDetails', goBackSound: "returnDetails" }, }); function useDetailsSection () { return useLocalStorage('details-section', 'screenshots'); } function Error (data: ErrorComponentProps) { const { ref, focusKey, focusSelf } = useFocusable({ focusKey: "game-details-error", preferredChildFocusKey: "main-details" }); const router = useRouter(); useShortcuts(focusKey, () => [{ label: "Back", button: GamePadButtonCode.B, action: (e) => HandleGoBack(router, e) }]); return
{JSON.stringify(data.error, null, 3)}
; } function MoreDetails (data: { game: FrontEndGameTypeDetailed | undefined; }) { const [details] = useDetailsSection(); const { ref, focusKey, hasFocusedChild } = useFocusable({ focusKey: "game-more-details-section", onFocus: (l, p, d) => scrollIntoViewHandler({ block: 'start', behavior: 'smooth' })(focusKey, ref.current, d), trackChildren: true }); return
{details === 'screenshots' && !!data.game &&
} {details === 'stats' && } {details === 'achievements' && !!data.game && }
; } function Stats (data: { game: FrontEndGameTypeDetailed | undefined; }) { const stats: StatEntry[] = []; if (data.game) { if (data.game.path_fs) stats.push({ label: "Location", content: data.game.path_fs, icon: }); if (data.game.metadata.companies) stats.push({ label: "Companies", content: data.game.metadata.companies }); if (data.game.metadata.genres) stats.push({ label: 'Genres', content: data.game.metadata.genres }); if (data.game.metadata.first_release_date) stats.push({ label: "Release Date", content: data.game.metadata.first_release_date.toLocaleDateString(), icon: }); if (data.game.emulators) stats.push({ label: "Emulators", content: data.game.emulators.map(e => e.name) }); if (data.game.source) stats.push({ label: "Source", content: `${data.game.source} - ${data.game.source_id}` }); const integrations = new Set(data.game.emulators?.flatMap(e => e.integrations).flatMap(i => i.capabilities).filter(c => !!c)); stats.push({ label: "Integrations", content: Array.from(integrations) }); } return ; } function Divider (data: { rootFocusKey: string; showShortcuts: boolean; game: FrontEndGameTypeDetailed | undefined; }) { const [details, setDetails] = useDetailsSection(); const { ref, focusKey } = useFocusable({ focusKey: "details-divider", onFocus: (l, p, d) => scrollIntoViewHandler({ block: 'nearest', behavior: 'smooth' })(focusKey, ref.current, d), }); const detailFilter: Record = { stats: { label: "Stats", selected: details === 'stats', icon: }, screenshots: { label: "Screenshots", selected: details === 'screenshots', icon: }, }; if (data.game?.achievements) { detailFilter.achievements = { label: "Achievements", selected: details === 'achievements', icon: }; } return
; } function RouteComponent () { const router = useRouter(); const [recommendedGamesVisible, setRecommendedGamesVisible] = useState(false); const { source, id } = Route.useParams(); const { data } = useQuery(gameQuery(source, id)); const [, setUpdate] = useState(0); const { ref, focusKey, focusSelf } = useFocusable({ focusKey: "game-details", preferredChildFocusKey: "main-details", forceFocus: true }); const headerRef = useRef(null); const backgroundImage = data ? new URL(`${RPC_URL(__HOST__)}${data.path_covers[0]}`) : undefined; const { data: recommendedGames } = useQuery({ ...gamesRecommendedBasedOnGameQuery(data?.id.source ?? source, data?.id.id ?? id), enabled: !!data && recommendedGamesVisible }); useShortcuts(focusKey, () => [{ label: "Back", button: GamePadButtonCode.B, action: (e) => HandleGoBack(router, e) }], [router]); useOnNavigateBack((s) => s.sound = 'returnDetails'); const recommendedEmulators = data?.emulators?.filter(e => e.validSources.some(em => em.exists) || e.source === 'store'); const { ref: intersct } = useIntersectionObserver({ onChange: (isIntersecting, entry) => { setRecommendedGamesVisible(isIntersecting); } }); return ( setUpdate(v => v + 1) }} >
{!!recommendedEmulators && recommendedEmulators.length > 0 &&

Related Emulators

} onFocus={scrollIntoViewHandler({ block: 'center' })} onSelect={(em, focus) => { if (em.source === 'local') return; router.navigate({ to: '/store/details/emulator/$id', params: { id: em.name } }); }} emulators={recommendedEmulators} />}

Related Games

{ router.navigate({ to: '/game/$source/$id', params: { id: id.id, source: id.source } }); }} onFocus={scrollIntoViewHandler({ block: 'center', inline: 'nearest' })} games={recommendedGames} />
); }