import { useMemo } from 'react';
import * as api from '../plex/api';
import { useSectionFor, useServerQuery } from '../app/queries';
import { navigate, useRouter, type SectionKind, type SectionTab } from '../app/router';
import { useSettings } from '../app/settings';
import type { Section } from '../plex/types';
import { Hero } from '../ui/Hero';
import { HubShelf } from '../ui/media';
import { useServer } from '../app/session';
import { Icon } from '../ui/Icon';
import { categoryArtwork } from './categories';
import { Artwork, EmptyState, ErrorState, PageLoader, Select } from '../ui/primitives';
import { buildFeatures, HIDDEN_HUBS, recentSeasons } from './discover';
import { LibraryBrowser } from './LibraryBrowser';
const TABS: { id: SectionTab; label: string }[] = [
{ id: 'discover', label: 'Discover' },
{ id: 'library', label: 'Library' },
{ id: 'categories', label: 'Categories' },
];
function SectionHeader({ kind, tab, section, all, overlay }: { kind: SectionKind; tab: SectionTab; section: Section; all: Section[]; overlay: boolean }) {
const set = useSettings((s) => s.set);
return (
{kind === 'movie' ? 'Movies' : 'TV Shows'}
{all.length > 1 && (
);
}
function Discover({ kind, section }: { kind: SectionKind; section: Section }) {
const hubs = useServerQuery(['sectionHubs', section.id], (s) => api.sectionHubs(s, section.id));
const cw = useServerQuery(['continue', section.id], (s) => api.continueWatching(s, section.id), { staleTime: 15_000 });
const seasons = useServerQuery(['recentSeasons', section.id], (s) => recentSeasons(s, section.id), { enabled: kind === 'show', staleTime: 120_000 });
const features = useMemo(() => buildFeatures(hubs.data ?? [], 7, kind === 'movie' ? 1 : 2), [hubs.data, kind]);
if (hubs.isError && !hubs.data) return hubs.refetch()} />;
if (!hubs.data) return ;
const rest = hubs.data.filter((h) => !HIDDEN_HUBS.test(h.id));
// TV: episodes lead; redundant episode hubs are skipped when they repeat Continue Watching.
const cwIds = new Set((cw.data ?? []).map((i) => i.id));
const shelves = rest.filter((h) => !(h.items.length && h.items.every((i) => cwIds.has(i.id))));
return (
<>
{kind === 'show' && }
{shelves.map((h) => (
navigate({ name: 'hub', key: h.key, title: h.title }) : undefined} />
))}
>
);
}
/** Genres as artwork tiles, as on Kaleido for TV; each opens the library filtered to it. */
function Categories({ kind, section }: { kind: SectionKind; section: Section }) {
const server = useServer();
const type = kind === 'movie' ? 1 : 2;
const genres = useServerQuery(['genres', section.id, type], (s) => api.genres(s, section.id, type), { staleTime: 30 * 60_000 });
const artwork = useServerQuery(['categoryArtwork', section.id, type, genres.data?.map((g) => g.key).join(',')], (s) => categoryArtwork(s, section.id, type, genres.data ?? []), {
enabled: !!genres.data?.length,
staleTime: 30 * 60_000,
});
if (genres.isError) return genres.refetch()} />;
if (!genres.data) return ;
if (!genres.data.length)
return (
{section.title} has no genres yet.
);
return (
{genres.data.map((g) => {
const path = artwork.data?.[g.key];
return (
);
})}
);
}
/** Back to the grid: pop the history entry when that's where the genre was opened from. */
function backToCategories(kind: SectionKind) {
const { entries, index, back } = useRouter.getState();
const prev = entries[index - 1]?.route;
if (prev?.name === 'section' && prev.tab === 'categories' && !prev.genre) back();
else navigate({ name: 'section', kind, tab: 'categories' }, { replace: true });
}
function CategoryLibrary({ kind, section, genre, title }: { kind: SectionKind; section: Section; genre: string; title: string }) {
return (
<>
{title}
>
);
}
export function SectionScreen({ kind, tab, genre, genreTitle }: { kind: SectionKind; tab: SectionTab; genre?: string; genreTitle?: string }) {
const { section, all, loading } = useSectionFor(kind);
if (loading) return ;
if (!section)
return (
This server has no {kind === 'movie' ? 'movie' : 'TV show'} libraries you can access.
);
return (
{tab === 'discover' && }
{tab === 'library' && }
{tab === 'categories' && (genre ? : )}
);
}