import { useRef } from 'react'; import { useQueries } from '@tanstack/react-query'; import { FocusContext, useFocusable } from '@noriginmedia/norigin-spatial-navigation'; import { useFocusableAutoScroll } from '../hooks/useFocusableAutoScroll'; import { rommApiClient, Game } from '../api/client'; import { GameCard } from './GameCard'; import { CollectionCard } from './CollectionCard'; import { FeaturedHero } from './FeaturedHero'; const ScrollBumper = ({ children }: { children: React.ReactNode }) => (
{children}
); const ScrollableSection = ({ title, children, showViewAll }: { title: string, children: React.ReactNode, showViewAll?: boolean }) => { const scrollRef = useRef(null); const scroll = (direction: 'left' | 'right') => { if (scrollRef.current) { const { current } = scrollRef; const scrollAmount = direction === 'left' ? -current.offsetWidth * 0.75 : current.offsetWidth * 0.75; current.scrollBy({ left: scrollAmount, behavior: 'smooth' }); } }; return (

{title}

{showViewAll && View All}
{children}
); }; const FocusableContinueCard = ({ game }: { game: Game }) => { const { ref, focused } = useFocusableAutoScroll({ onEnterPress: () => console.log("Continue Game", game.id) }); return (
Cover

{game.title}

84% Complete Active
); }; const LibraryGrid = () => { const { ref: gridRef, focusKey: gridFocusKey } = useFocusable(); const results = useQueries({ queries: [ { queryKey: ['recentGames'], queryFn: () => rommApiClient.fetchRecentGames() }, { queryKey: ['collections'], queryFn: () => rommApiClient.fetchCollections() }, { queryKey: ['favorites'], queryFn: () => rommApiClient.fetchFavorites() }, { queryKey: ['playing'], queryFn: () => rommApiClient.fetchGames() } ] }); const isLoading = results.some(r => r.isLoading); const error = results.some(r => r.error); if (isLoading) return
Loading vault data...
; if (error) return
Failed to load vault data
; const recentGames = results[0].data || []; const collections = results[1].data || []; const favorites = results[2].data || []; const continuePlaying = results[3].data || []; const featuredCollections = collections.filter((c) => c.name.toLowerCase() === 'featured'); const combinedFeaturedGames = Array.from(new Map(featuredCollections.flatMap(c => c.games).map(g => [g.id, g])).values()); const hasFeatured = combinedFeaturedGames.length > 0; const heroGames = hasFeatured ? combinedFeaturedGames : recentGames; const showRecentSection = hasFeatured; const standardCollections = collections.filter(c => c.name.toLowerCase() !== 'featured'); return (
{heroGames.length > 0 && }
{showRecentSection && recentGames.length > 0 && ( {recentGames.map((game) => ( ))} )} {continuePlaying.length > 0 && ( {continuePlaying.slice(0, 5).map((game) => ( ))} )} {favorites.length > 0 && ( {favorites.map((game) => ( ))} )} {standardCollections.length > 0 && ( {standardCollections.map((col) => { const cover = col.games?.[0]?.coverUrl || ''; const gamesCount = col.games?.length || 0; const caption = gamesCount > 0 ? `${gamesCount} Game${gamesCount === 1 ? '' : 's'}` : (col.ownerRole === 'admin' ? 'Public' : 'Private'); return ( ); })} )} {/* Autogenerated Collections */} {[ { id: 'auto-1', name: 'All Games', icon: 'apps' }, { id: 'auto-2', name: 'Favorites', icon: 'star' }, { id: 'auto-3', name: 'Recently Added', icon: 'schedule' }, { id: 'auto-4', name: 'Recently Played', icon: 'history' }, { id: 'auto-5', name: 'Most Played', icon: 'leaderboard' }, { id: 'auto-6', name: 'Never Played', icon: 'videogame_asset_off' }, ].map((col) => ( ))}
); }; export default LibraryGrid;