import { useMemo } from 'react'; import { useQuery } from '@tanstack/react-query'; import * as api from '../plex/api'; import { useSections, useServerQuery } from '../app/queries'; import { useServer } from '../app/session'; import { navigate } from '../app/router'; import { Hero } from '../ui/Hero'; import { HubShelf } from '../ui/media'; import { ErrorState, PageLoader } from '../ui/primitives'; import { buildFeatures, HIDDEN_HUBS } from './discover'; export function Home() { const server = useServer(); const sections = useSections(); const video = (sections.data ?? []).filter((s) => (s.type === 'movie' || s.type === 'show') && !s.hidden); const home = useServerQuery(['homeHubs'], (s) => api.homeHubs(s)); const continueWatching = useServerQuery(['continue'], (s) => api.continueWatching(s), { staleTime: 15_000 }); // Section hubs feed the hero and the release shelves; shared with Discover. const sectionHubs = useQuery({ queryKey: [server.machineId, 'sectionHubsAll', video.map((s) => s.id).join(',')], queryFn: async () => (await Promise.all(video.map((s) => api.sectionHubs(server, s.id).catch(() => [])))).flat(), enabled: video.length > 0, staleTime: 60_000, }); const features = useMemo(() => buildFeatures(sectionHubs.data ?? [], 8), [sectionHubs.data]); const extra = (sectionHubs.data ?? []).filter((h) => /recentlyreleased|recentlyaired|topunwatched|toprated/.test(h.id)); if (home.isError && !home.data) return home.refetch()} />; if (!home.data || sections.isLoading) return ; const hubs = home.data.filter((h) => !HIDDEN_HUBS.test(h.id)); const cw = continueWatching.data ?? []; const homeIds = new Set(hubs.map((h) => h.title)); return (
{hubs.map((h) => ( navigate({ name: 'hub', key: h.key, title: h.title }) : undefined} /> ))} {extra .filter((h) => !homeIds.has(h.title)) .map((h) => ( navigate({ name: 'hub', key: h.key, title: h.title })} /> ))}
); }