Files
2026-10-27 05:49:38 -05:00

93 lines
3.6 KiB
JavaScript

/*
views/search.js — /search?q= live TMDB multi-search (movies + tv), URL-synced.
*/
import { h, debounce } from '../util.js';
import * as tmdb from '../tmdb.js';
import { navigate } from '../router.js';
import { grid, posterCard, skeletonCard } from '../ui/card.js';
import { icon } from '../ui/icons.js';
let state = null;
function skeletonGrid(n = 12) {
return h('div.grid', Array.from({ length: n }, () => skeletonCard('poster')));
}
async function run(q) {
const s = state;
if (!s) return;
const my = ++s.req;
s.input.value !== q && document.activeElement !== s.input && (s.input.value = q);
document.title = q ? `“${q}” · Search · bug tv` : 'Search · bug tv';
if (!q.trim()) {
s.heading.textContent = 'Popular right now';
s.results.replaceChildren(skeletonGrid());
try {
const d = await tmdb.trending('all', 'week');
if (my !== s.req) return;
s.results.replaceChildren(grid((d.results || []).map((r) => tmdb.toItem(r)).filter((x) => x.poster_path)));
} catch (e) {
if (my === s.req) s.results.replaceChildren(h('p.muted', e.message));
}
return;
}
s.heading.textContent = `Results for “${q}”`;
s.results.replaceChildren(skeletonGrid());
try {
const [p1, p2] = await Promise.all([tmdb.search(q, 1), tmdb.search(q, 2).catch(() => ({ results: [] }))]);
if (my !== s.req) return;
const seen = new Set();
const list = [...p1.results, ...(p1.total_pages > 1 ? p2.results : [])]
.map((r) => tmdb.toItem(r))
.filter((x) => !seen.has(`${x.type}:${x.id}`) && seen.add(`${x.type}:${x.id}`));
list.sort((a, b) => (b.poster_path ? 1 : 0) - (a.poster_path ? 1 : 0));
if (!list.length) {
s.results.replaceChildren(h('div.empty',
h('div.empty__icon', icon('search', { size: 28 })),
h('h2.empty__title', 'No matches'),
h('p.empty__text', 'Try a different title, or check the spelling.')));
return;
}
s.heading.textContent = `${list.length}${p1.total_results > list.length ? '+' : ''} results for “${q}”`;
s.results.replaceChildren(grid(list, (it) => posterCard(it)));
} catch (e) {
if (my === s.req) s.results.replaceChildren(h('div.empty', h('h2.empty__title', 'Search failed'), h('p.empty__text', e.message)));
}
}
export function render(root, params) {
const q = params.query.get('q') || '';
const input = h('input.search-field__input', {
type: 'search', placeholder: 'Search movies and series', value: q, autocomplete: 'off', spellcheck: 'false', enterkeyhint: 'search', 'aria-label': 'Search',
});
const go = debounce(() => navigate(`/search${input.value.trim() ? `?q=${encodeURIComponent(input.value.trim())}` : ''}`, { replace: true }), 300);
input.addEventListener('input', go);
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
go.cancel();
navigate(`/search?q=${encodeURIComponent(input.value.trim())}`, { replace: true });
input.blur();
}
});
const heading = h('h1.page__title');
const results = h('div.search-results', { 'aria-live': 'polite' });
root.append(h('div.page.page--padded.container',
h('label.search-field.search-field--page', icon('search', { size: 22 }), input),
heading,
results));
state = { input, heading, results, req: 0 };
run(q);
// focus the page input on mobile (header search is hidden there)
if (!q && window.matchMedia('(max-width: 720px)').matches) setTimeout(() => input.focus(), 50);
return () => {
go.cancel();
state = null;
};
}
export function update(params) {
if (!state) return false;
run(params.query.get('q') || '');
return true;
}