Files
tv/web/js/views/browse.js
T
2026-10-27 05:49:38 -05:00

64 lines
2.5 KiB
JavaScript

/*
views/browse.js — /browse/:type?with_genres=&with_networks=&title= discover grid with infinite scroll.
Without filters it lists popular titles of that type.
*/
import { h } from '../util.js';
import * as tmdb from '../tmdb.js';
import { posterCard, skeletonCard } from '../ui/card.js';
const PASS = ['with_genres', 'with_networks', 'with_companies', 'with_original_language', 'sort_by', 'with_keywords', 'first_air_date_year', 'primary_release_year'];
export function render(root, params) {
const type = params.type === 'tv' ? 'tv' : 'movie';
const q = params.query;
const filters = {};
PASS.forEach((k) => q.get(k) && (filters[k] = q.get(k)));
const title = q.get('title') || (type === 'tv' ? 'Series' : 'Movies');
document.title = `${title} · bug tv`;
const gridEl = h('div.grid', { role: 'list' });
const sentinel = h('div.load-more');
const status = h('p.muted.center');
root.append(h('div.page.page--padded.container',
h('div.page__head', h('div', h('p.page__eyebrow', type === 'tv' ? 'Series' : 'Movies'), h('h1.page__title', title))),
gridEl, status, sentinel));
let page = 0;
let total = 1;
let loading = false;
const seen = new Set();
async function more() {
if (loading || page >= total || page >= 20) return;
loading = true;
const skels = Array.from({ length: 12 }, () => skeletonCard('poster'));
gridEl.append(...skels);
try {
const next = page + 1;
const d = Object.keys(filters).length
? await tmdb.discover(type, { ...filters, page: next, 'vote_count.gte': filters.with_networks ? 20 : 50 })
: await tmdb.popular(type, next);
if (params.signal.aborted) return;
page = next;
total = d.total_pages || 1;
skels.forEach((s) => s.remove());
const items = (d.results || []).map((r) => tmdb.toItem(r, type)).filter((x) => x.poster_path && !seen.has(x.id) && seen.add(x.id));
gridEl.append(...items.map((it) => posterCard(it)));
if (!gridEl.children.length) status.textContent = 'Nothing found.';
} catch (e) {
skels.forEach((s) => s.remove());
status.textContent = e.message;
total = 0;
} finally {
loading = false;
}
// keep filling while the sentinel is still visible
if (sentinel.getBoundingClientRect().top < window.innerHeight + 600) more();
}
const io = new IntersectionObserver((entries) => entries.some((e) => e.isIntersecting) && more(), { rootMargin: '800px 0px' });
io.observe(sentinel);
more();
return () => io.disconnect();
}