/* views/title.js — /title/:type/:id detail page. */ import { h, runtimeLabel, plural, yearOf } from '../util.js'; import * as tmdb from '../tmdb.js'; import { href } from '../router.js'; import { resume } from '../storage.js'; import { icon } from '../ui/icons.js'; import { row } from '../ui/row.js'; import { posterCard } from '../ui/card.js'; import { castRow } from '../ui/cast.js'; import { episodesSection } from '../ui/episodes.js'; import { trailerModal } from '../ui/modal.js'; import { playTarget, myListButton, shareTitle, titlePath } from '../ui/actions.js'; const DAY = 86400000; function badgeFor(d, type) { const now = Date.now(); if (type === 'tv') { if (d.next_episode_to_air) { const n = d.next_episode_to_air; return n.episode_number === 1 ? `Season ${n.season_number} coming soon` : 'New episodes weekly'; } const last = d.last_episode_to_air; if (last && last.air_date && now - new Date(last.air_date) < 45 * DAY) return `Season ${last.season_number} out now`; if (d.status === 'Ended') return 'Complete series'; return null; } if (d.release_date) { const age = now - new Date(d.release_date); if (age < 0) return 'Coming soon'; if (age < 90 * DAY) return 'New release'; } return null; } function skeleton() { return h('div.page.page--title', h('section.title-hero.title-hero--loading', h('div.title-hero__content.container', h('div.skeleton.skeleton--block', { style: { width: '40%', height: '64px' } }), h('div.skeleton.skeleton--line', { style: { width: '30%' } }), h('div.skeleton.skeleton--line', { style: { width: '55%' } })))); } function infoList(d, type) { const rows = []; const add = (k, v) => v && rows.push(h('div.facts__row', h('dt', k), h('dd', v))); if (type === 'tv') { add('Status', d.status); add('First aired', d.first_air_date && new Date(d.first_air_date).toLocaleDateString(undefined, { year: 'numeric', month: 'long', day: 'numeric' })); add('Seasons', d.number_of_seasons && `${plural(d.number_of_seasons, 'season')} · ${plural(d.number_of_episodes || 0, 'episode')}`); add('Network', (d.networks || []).map((n) => n.name).join(', ')); add('Created by', (d.created_by || []).map((c) => c.name).join(', ')); } else { add('Released', d.release_date && new Date(d.release_date).toLocaleDateString(undefined, { year: 'numeric', month: 'long', day: 'numeric' })); add('Runtime', runtimeLabel(d.runtime)); const directors = ((d.credits && d.credits.crew) || []).filter((c) => c.job === 'Director').map((c) => c.name); add('Director', directors.join(', ')); add('Studio', (d.production_companies || []).slice(0, 2).map((c) => c.name).join(', ')); } add('Language', (d.spoken_languages || []).slice(0, 3).map((l) => l.english_name || l.name).join(', ')); return rows.length ? h('dl.facts', rows) : null; } export async function render(root, params) { const type = params.type === 'tv' ? 'tv' : 'movie'; const id = params.id; root.append(skeleton()); let d; try { d = await tmdb.details(type, id); } catch (e) { if (params.signal.aborted) return; root.replaceChildren(h('section.page.page--center', h('div.empty', h('h1.empty__title', 'Could not load this title'), h('p.empty__text', e.message), h('a.btn.btn--primary', { href: href('/') }, 'Back to home')))); return; } if (params.signal.aborted) return; const item = tmdb.toItem(d, type); document.title = `${item.title} · bug tv`; const logo = tmdb.logoFor(d); const cert = tmdb.certificationFor(d); const badge = badgeFor(d, type); const trailer = tmdb.trailerFor(d); const target = playTarget(type, id, d); const titleText = h('h1.title-hero__title', item.title); const titleBox = h('div.hero__titlebox.title-hero__titlebox', titleText); if (logo) { const im = h('img.hero__logo', { src: tmdb.img(logo.file_path, 'w500'), alt: item.title, decoding: 'async' }); im.addEventListener('load', () => { titleBox.classList.add('has-logo'); titleText.classList.add('sr-only'); }, { once: true }); im.addEventListener('error', () => im.remove(), { once: true }); titleBox.prepend(im); } const genres = (d.genres || []).slice(0, 4).map((g) => h('a.meta-link', { href: href(`/browse/${type}?with_genres=${g.id}&title=${encodeURIComponent(g.name)}`) }, g.name)); const extra = type === 'tv' ? (d.number_of_seasons ? plural(d.number_of_seasons, 'Season') : '') : runtimeLabel(d.runtime); const meta = h('div.hero__meta', item.rating > 0 ? h('span.rating.rating--lg', icon('star', { size: 16 }), item.rating.toFixed(1)) : null, yearOf(d.release_date || d.first_air_date) ? h('span', yearOf(d.release_date || d.first_air_date)) : null, extra ? h('span', extra) : null, cert ? h('span.chip.chip--outline', cert) : null, genres.length ? h('span.hero__genres', genres.flatMap((g, i) => (i ? [h('span.dot', '•'), g] : [g]))) : null, ); const playBtn = h('a.btn.btn--primary.btn--lg', { href: href(target.path) }, icon('play'), h('span', target.label), target.progress > 0 ? h('span.btn__progress', h('i', { style: { width: `${Math.round(target.progress * 100)}%` } })) : null); const actions = h('div.hero__actions', playBtn, myListButton(item, { className: 'btn btn--glass btn--lg' }), trailer ? h('button.btn.btn--glass.btn--lg', { type: 'button', onclick: () => trailerModal(trailer.key, item.title) }, icon('trailer'), h('span', 'Trailer')) : null, h('button.btn.btn--glass.btn--lg.btn--icon', { type: 'button', 'aria-label': 'Share', title: 'Share', onclick: () => shareTitle(item.title, titlePath(type, id)) }, icon('share')), ); const backdropPath = d.backdrop_path || (d.images && d.images.backdrops && d.images.backdrops[0] && d.images.backdrops[0].file_path); const hero = h('section.title-hero', backdropPath ? h('div.hero__media', h('img.hero__img.is-loaded', { src: tmdb.img(backdropPath, 'w1280'), srcset: `${tmdb.img(backdropPath, 'w780')} 780w, ${tmdb.img(backdropPath, 'w1280')} 1280w, ${tmdb.img(backdropPath, 'original')} 1920w`, sizes: '100vw', alt: '', decoding: 'async', fetchpriority: 'high', })) : null, h('div.hero__scrim'), h('div.title-hero__content.container', badge ? h('span.badge', badge) : null, titleBox, meta, d.tagline ? h('p.title-hero__tagline', d.tagline) : null, actions, ), ); const story = h('section.section.story.container', h('div.story__main', h('h2.section__title', 'Storyline'), h('p.story__text', d.overview || 'No synopsis available.')), infoList(d, type)); const body = h('div.title-body'); const cast = castRow(d.credits); if (cast) body.appendChild(h('div.container-row', cast)); let eps = null; if (type === 'tv') { const last = resume.forShow('tv', id)[0]; const season = params.query.get('season') || (last && last.season); eps = episodesSection(d, { season }); if (eps) body.appendChild(h('div.container', eps)); } const similar = ((d.recommendations && d.recommendations.results && d.recommendations.results.length ? d.recommendations : d.similar) || {}).results || []; const similarItems = similar.map((r) => tmdb.toItem(r, type)).filter((x) => x.poster_path); if (similarItems.length) body.appendChild(h('div.container-row', row({ title: 'More Like This', items: similarItems, render: (it) => posterCard(it), lazy: false }))); root.replaceChildren(h('div.page.page--title', hero, story, body)); // keep Continue / progress fresh when the player writes resume data (e.g. returning from /watch) const onResume = () => { const t = playTarget(type, id, d); playBtn.href = href(t.path); playBtn.querySelector('span').textContent = t.label; if (eps) eps.refresh(); }; window.addEventListener('bugtv:resume', onResume); return () => window.removeEventListener('bugtv:resume', onResume); }