187 lines
8.5 KiB
JavaScript
187 lines
8.5 KiB
JavaScript
/*
|
|
views/home.js — home (/), Movies (/movies) and TV (/tv) browse pages.
|
|
params.kind: undefined | 'movie' | 'tv'
|
|
*/
|
|
import { h, durationLabel } from '../util.js';
|
|
import * as tmdb from '../tmdb.js';
|
|
import { href } from '../router.js';
|
|
import { resume, myList, progressOf } from '../storage.js';
|
|
import { billboard } from '../ui/hero.js';
|
|
import { row } from '../ui/row.js';
|
|
import { posterCard, wideCard, logoCard } from '../ui/card.js';
|
|
import { watchHref } from '../ui/actions.js';
|
|
import { toast } from '../ui/toast.js';
|
|
|
|
const NETWORKS = [
|
|
{ id: 213, name: 'Netflix' },
|
|
{ id: 49, name: 'HBO' },
|
|
{ id: 2739, name: 'Disney+' },
|
|
{ id: 2552, name: 'Apple TV+' },
|
|
{ id: 1024, name: 'Prime Video' },
|
|
{ id: 453, name: 'Hulu' },
|
|
{ id: 4330, name: 'Paramount+' },
|
|
{ id: 3186, name: 'HBO Max' },
|
|
];
|
|
|
|
const GENRES = {
|
|
all: [
|
|
{ type: 'movie', id: 28, name: 'Action Movies' },
|
|
{ type: 'tv', id: 10765, name: 'Sci-Fi & Fantasy Series' },
|
|
{ type: 'movie', id: 35, name: 'Comedy Movies' },
|
|
{ type: 'tv', id: 80, name: 'Crime Series' },
|
|
{ type: 'movie', id: 16, name: 'Animation' },
|
|
{ type: 'movie', id: 27, name: 'Horror' },
|
|
],
|
|
movie: [
|
|
{ type: 'movie', id: 28, name: 'Action' },
|
|
{ type: 'movie', id: 878, name: 'Science Fiction' },
|
|
{ type: 'movie', id: 35, name: 'Comedy' },
|
|
{ type: 'movie', id: 53, name: 'Thrillers' },
|
|
{ type: 'movie', id: 16, name: 'Animation' },
|
|
{ type: 'movie', id: 27, name: 'Horror' },
|
|
{ type: 'movie', id: 10749, name: 'Romance' },
|
|
{ type: 'movie', id: 99, name: 'Documentaries' },
|
|
],
|
|
tv: [
|
|
{ type: 'tv', id: 18, name: 'Drama' },
|
|
{ type: 'tv', id: 10765, name: 'Sci-Fi & Fantasy' },
|
|
{ type: 'tv', id: 80, name: 'Crime' },
|
|
{ type: 'tv', id: 35, name: 'Comedy' },
|
|
{ type: 'tv', id: 16, name: 'Animation' },
|
|
{ type: 'tv', id: 10759, name: 'Action & Adventure' },
|
|
{ type: 'tv', id: 9648, name: 'Mystery' },
|
|
{ type: 'tv', id: 99, name: 'Documentary' },
|
|
],
|
|
};
|
|
|
|
const items = (type) => (d) => ((d && d.results) || []).map((r) => tmdb.toItem(r, type)).filter((x) => x.poster_path || x.backdrop_path);
|
|
const poster = (it) => posterCard(it);
|
|
|
|
// Continue Watching record → wide card, backdrop looked up lazily when the record has none
|
|
function continueCard(rec, onRemoved) {
|
|
const item = { type: rec.type, id: rec.tmdbId, title: rec.show || rec.title || '', backdrop_path: null, rating: 0 };
|
|
const left = rec.d ? Math.max(0, rec.d - rec.t) : 0;
|
|
const leftText = left ? `${durationLabel(left)} left` : '';
|
|
const meta = rec.type === 'tv' ? [`S${rec.season} E${rec.episode}`, leftText].filter(Boolean).join(' · ') : leftText || 'Movie';
|
|
const image = rec.backdrop || rec.still || null;
|
|
const card = wideCard(item, {
|
|
href: watchHref(rec.type, rec.tmdbId, rec.season, rec.episode),
|
|
image: image || null,
|
|
meta,
|
|
kicker: rec.type === 'tv' && rec.title && rec.title !== rec.show ? rec.title : null,
|
|
progress: progressOf(rec),
|
|
onRemove: (el) => {
|
|
resume.removeShow(rec.type, rec.tmdbId);
|
|
el.remove();
|
|
onRemoved();
|
|
toast(`Removed “${item.title}” from Continue Watching`);
|
|
},
|
|
});
|
|
if (!image) {
|
|
tmdb.details(rec.type, rec.tmdbId).then((d) => {
|
|
const path = d.backdrop_path || d.poster_path;
|
|
if (!path) return;
|
|
const media = card.querySelector('.card__media');
|
|
const im = h('img.card__img', { src: tmdb.img(path, 'w780'), alt: '', loading: 'lazy', decoding: 'async', onload: (e) => e.target.classList.add('is-loaded') });
|
|
media.insertBefore(im, media.querySelector('.card__shade'));
|
|
}).catch(() => {});
|
|
}
|
|
return card;
|
|
}
|
|
|
|
export async function render(root, params) {
|
|
const kind = params.kind === 'movie' || params.kind === 'tv' ? params.kind : null;
|
|
document.title = kind === 'movie' ? 'Movies · bug tv' : kind === 'tv' ? 'Series · bug tv' : 'bug tv';
|
|
|
|
const heroSlot = h('div.hero-slot', h('div.hero.hero--loading', h('div.hero__skeleton.skeleton')));
|
|
const rows = h('div.rows');
|
|
root.append(h('div.page.page--home', heroSlot, rows));
|
|
|
|
let hero = null;
|
|
const listeners = [];
|
|
|
|
// hero: top trending
|
|
tmdb.trending(kind || 'all', 'day').then((d) => {
|
|
if (params.signal.aborted) return;
|
|
const list = items(kind)(d).filter((x) => x.backdrop_path && x.overview).slice(0, 5);
|
|
const label = kind === 'movie' ? 'Movies' : kind === 'tv' ? 'TV Shows' : null;
|
|
hero = billboard(list, {
|
|
eyebrow: (i, it) => `#${i + 1} in ${label || (it.type === 'tv' ? 'TV Shows' : 'Movies')} Today`,
|
|
});
|
|
heroSlot.replaceChildren(hero.el);
|
|
}).catch((e) => {
|
|
heroSlot.replaceChildren();
|
|
toast(e.message, { error: true });
|
|
});
|
|
|
|
// Continue Watching
|
|
const contRecords = () => resume.latestPerShow().filter((r) => !kind || r.type === kind).filter((r) => progressOf(r) < 0.97 || r.type === 'tv');
|
|
const cont = row({
|
|
title: 'Continue Watching',
|
|
variant: 'wide',
|
|
items: contRecords(),
|
|
render: (rec) => continueCard(rec, () => { if (!cont.querySelector('.card')) cont.hidden = true; }),
|
|
});
|
|
rows.appendChild(cont);
|
|
const onResume = () => cont.setItems(contRecords());
|
|
window.addEventListener('bugtv:resume', onResume);
|
|
listeners.push(['bugtv:resume', onResume]);
|
|
|
|
// My List
|
|
const listItems = () => myList.all().filter((x) => !kind || x.type === kind).map((x) => tmdb.toItem(x, x.type));
|
|
const mine = row({ title: 'My List', moreHref: href('/list'), items: listItems(), render: poster });
|
|
rows.appendChild(mine);
|
|
const onList = () => mine.setItems(listItems());
|
|
window.addEventListener('bugtv:mylist', onList);
|
|
listeners.push(['bugtv:mylist', onList]);
|
|
|
|
const add = (opts) => rows.appendChild(row(opts));
|
|
|
|
if (!kind) {
|
|
add({ title: 'Trending Today', load: () => tmdb.trending('all', 'day').then(items()), render: (it, i) => posterCard(it, { rank: i < 10 ? i + 1 : null }) });
|
|
add({ title: 'Popular on Streaming', variant: 'logo', items: NETWORKS.slice(0, 6), render: networkCard, lazy: true });
|
|
add({ title: 'Popular Movies', moreHref: href('/browse/movie?title=Popular%20Movies'), load: () => tmdb.popular('movie').then(items('movie')), render: poster });
|
|
add({ title: 'Popular Series', variant: 'wide', moreHref: href('/browse/tv?title=Popular%20Series'), load: () => tmdb.popular('tv').then(items('tv')), render: (it) => wideCard(it) });
|
|
add({ title: 'Top Rated Movies', load: () => tmdb.topRated('movie').then(items('movie')), render: poster });
|
|
add({ title: 'Top Rated Series', load: () => tmdb.topRated('tv').then(items('tv')), render: poster });
|
|
} else {
|
|
const label = kind === 'movie' ? 'Movies' : 'Series';
|
|
add({ title: `Trending ${label}`, load: () => tmdb.trending(kind, 'day').then(items(kind)), render: (it, i) => posterCard(it, { rank: i < 10 ? i + 1 : null }) });
|
|
if (kind === 'tv') add({ title: 'Networks', variant: 'logo', items: NETWORKS, render: networkCard, lazy: true });
|
|
add({ title: `Popular ${label}`, variant: 'wide', moreHref: href(`/browse/${kind}?title=${encodeURIComponent(`Popular ${label}`)}`), load: () => tmdb.popular(kind).then(items(kind)), render: (it) => wideCard(it) });
|
|
add({ title: `Top Rated ${label}`, load: () => tmdb.topRated(kind).then(items(kind)), render: poster });
|
|
if (kind === 'movie') {
|
|
add({ title: 'Now in Theaters', load: () => tmdb.get('/movie/now_playing', { page: 1 }, { session: true }).then(items('movie')), render: poster });
|
|
} else {
|
|
add({ title: 'Airing This Week', load: () => tmdb.get('/tv/on_the_air', { page: 1 }, { session: true }).then(items('tv')), render: poster });
|
|
}
|
|
}
|
|
|
|
(GENRES[kind || 'all']).forEach((g, i) => {
|
|
const more = href(`/browse/${g.type}?with_genres=${g.id}&title=${encodeURIComponent(g.name)}`);
|
|
add({
|
|
title: g.name,
|
|
moreHref: more,
|
|
variant: i % 3 === 1 ? 'wide' : 'poster',
|
|
load: () => tmdb.discover(g.type, { with_genres: g.id, 'vote_count.gte': 200 }).then(items(g.type)),
|
|
render: i % 3 === 1 ? (it) => wideCard(it) : poster,
|
|
});
|
|
});
|
|
|
|
return () => {
|
|
if (hero) hero.destroy();
|
|
listeners.forEach(([n, fn]) => window.removeEventListener(n, fn));
|
|
};
|
|
}
|
|
|
|
function networkCard(n) {
|
|
const card = logoCard({ name: n.name, logo: null, href: href(`/browse/tv?with_networks=${n.id}&title=${encodeURIComponent(n.name)}`) });
|
|
tmdb.network(n.id).then((d) => {
|
|
if (!d || !d.logo_path) return;
|
|
const media = card.querySelector('.card__media');
|
|
const im = h('img.card__logo', { src: tmdb.img(d.logo_path, 'w300'), alt: n.name, decoding: 'async' });
|
|
im.addEventListener('load', () => media.replaceChildren(im), { once: true });
|
|
}).catch(() => {});
|
|
return card;
|
|
}
|