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

105 lines
3.9 KiB
JavaScript

/*
actions.js — shared title actions: links, play/resume targets, My List toggle, share.
*/
import { h } from '../util.js';
import { href } from '../router.js';
import { resume, myList, progressOf } from '../storage.js';
import { icon } from './icons.js';
import { toast } from './toast.js';
export function titlePath(type, id) {
return `/title/${type === 'tv' ? 'tv' : 'movie'}/${id}`;
}
export function watchPath(type, id, season, episode) {
if (type === 'tv') return `/watch/tv/${id}/${season || 1}/${episode || 1}`;
return `/watch/movie/${id}`;
}
export const titleHref = (type, id) => href(titlePath(type, id));
export const watchHref = (type, id, s, e) => href(watchPath(type, id, s, e));
/*
Where "Play" should go for a title, using resume data.
details (optional, tv) lets us advance past a finished episode and pick the first real season.
→ { path, label, record, progress }
*/
export function playTarget(type, id, details) {
const recs = resume.forShow(type === 'tv' ? 'tv' : 'movie', id);
const rec = recs[0] || null;
if (type !== 'tv') {
const p = progressOf(rec);
if (rec && p > 0.01 && p < 0.95) return { path: watchPath('movie', id), label: 'Resume', record: rec, progress: p };
return { path: watchPath('movie', id), label: 'Play', record: null, progress: 0 };
}
if (rec) {
let s = Number(rec.season) || 1;
let e = Number(rec.episode) || 1;
const p = progressOf(rec);
if (p >= 0.95 && details) {
const seasons = (details.seasons || []).filter((x) => x.season_number > 0);
const cur = seasons.find((x) => x.season_number === s);
if (cur && e < cur.episode_count) e += 1;
else if (seasons.some((x) => x.season_number === s + 1)) {
s += 1;
e = 1;
}
return { path: watchPath('tv', id, s, e), label: `Play S${s} E${e}`, record: rec, progress: 0 };
}
return { path: watchPath('tv', id, s, e), label: `Continue S${s} E${e}`, record: rec, progress: p };
}
let first = 1;
if (details && Array.isArray(details.seasons)) {
const real = details.seasons.filter((x) => x.season_number > 0 && x.episode_count > 0);
if (real.length) first = real[0].season_number;
}
return { path: watchPath('tv', id, first, 1), label: 'Play', record: null, progress: 0 };
}
// "+ My List" toggle button; item = { type, id, title, poster_path, backdrop_path, year, rating }
export function myListButton(item, opts = {}) {
const btn = h('button', { type: 'button', class: opts.className || 'btn btn--glass' });
function paint() {
const on = myList.has(item.type, item.id);
btn.classList.toggle('is-on', on);
btn.setAttribute('aria-pressed', on ? 'true' : 'false');
btn.setAttribute('aria-label', on ? 'Remove from My List' : 'Add to My List');
btn.title = on ? 'Remove from My List' : 'Add to My List';
btn.replaceChildren(...[icon(on ? 'check' : 'plus'), opts.iconOnly ? null : h('span', on ? 'In My List' : 'My List')].filter(Boolean));
}
btn.dataset.mylist = `${item.type}:${item.id}`;
btn._paint = paint;
btn.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
const on = myList.toggle(item);
toast(on ? `Added “${item.title}” to My List` : `Removed “${item.title}” from My List`);
});
paint();
return btn;
}
// one global listener repaints every toggle for the changed title
window.addEventListener('bugtv:mylist', (e) => {
const d = e.detail || {};
document.querySelectorAll(`[data-mylist="${d.type}:${d.id}"]`).forEach((b) => b._paint && b._paint());
});
export async function shareTitle(title, path) {
const url = new URL(href(path), location.href).toString();
try {
if (navigator.share) {
await navigator.share({ title, url });
return;
}
} catch (e) {
if (e && e.name === 'AbortError') return;
}
try {
await navigator.clipboard.writeText(url);
toast('Link copied to clipboard');
} catch (e) {
toast(url);
}
}