On a phone, on a row of its own under the picker, it cost a line above the list for one button. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RqkrVAJFkEgz9cXG3jG1Zy
93 lines
5.8 KiB
TypeScript
93 lines
5.8 KiB
TypeScript
/* ---------------- seasons (#482) ---------------- */
|
|
/// A podcast's seasons, by feed id, as its page last fetched them, so going back to it draws its
|
|
/// picker at once while the counts are asked for again.
|
|
const seasonsOf=new Map<string,SeasonCount[]>();
|
|
/// Whether a podcast is told in seasons: two or more of them, holding most of its episodes. A few
|
|
/// numbered among hundreds (The Vintage RPG Podcast's 2 of 366), or years given as seasons on a
|
|
/// quarter of them (Ken and Robin's), are no seasons worth a picker.
|
|
function seasonsShown(list: SeasonCount[]|undefined): boolean{
|
|
if(!list) return false;
|
|
const numbered=list.filter(s=>s.season!=null);
|
|
const inOne=numbered.reduce((n,s)=>n+s.episodes,0), all=list.reduce((n,s)=>n+s.episodes,0);
|
|
return numbered.length>=2 && inOne*2>=all;
|
|
}
|
|
/// What the picker sends for a season: its number, or `none` for the episodes that name none.
|
|
const seasonKey=(s: SeasonCount)=>s.season==null?'none':String(s.season);
|
|
const seasonName=(s: number|null|undefined)=>s==null?'Other episodes':`Season ${s}`;
|
|
/// The picker, under the tabs: every season, one of them, or the episodes naming none.
|
|
function seasonsHTML(list: SeasonCount[]): string{
|
|
const chip=(key: string,label: string,title: string)=>
|
|
`<button data-season="${key}" class="${S.season===key?'on':''}" aria-pressed="${S.season===key}" title="${title}">${label}</button>`;
|
|
return `<div class="tabs seasons" role="group" aria-label="Seasons">${chip('','All seasons','Every season')}${
|
|
list.map(s=>chip(seasonKey(s),s.season==null?'Other':`Season ${s.season}`,
|
|
`${plural(s.episodes,'episode')}, ${s.unread} unread`)).join('')}</div>`;
|
|
}
|
|
function drawSeasons(f: Feed, list: SeasonCount[]|undefined){
|
|
const bar=$('#content .toolbar'); if(!bar||S.feed!==f.id) return;
|
|
bar.querySelector('.seasonrow')?.remove();
|
|
if(!list||!seasonsShown(list)) return;
|
|
// The download beside the picker, on its row: on a phone, on a row of its own, it cost a line.
|
|
const picked=list.find(s=>seasonKey(s)===S.season);
|
|
bar.insertAdjacentHTML('beforeend',`<div class="seasonrow">${seasonsHTML(list)}${picked?`<button class="btn ico seasondl"
|
|
title="Download ${esc(seasonName(picked.season))}" aria-label="Download ${esc(seasonName(picked.season))}">${ICON.download}</button>`:''}</div>`);
|
|
bar.querySelector<HTMLButtonElement>('.seasondl')?.addEventListener('click',()=>downloadSeason(f));
|
|
// The one picked in view: on a phone Season 3 of six is past the edge, and the picker looked as
|
|
// if every season were still showing.
|
|
const track=bar.querySelector<HTMLElement>('.seasons'), on=track?.querySelector<HTMLElement>('.on');
|
|
if(track&&on) track.scrollLeft+=on.getBoundingClientRect().left-track.getBoundingClientRect().left-(track.clientWidth-on.offsetWidth)/2;
|
|
$$<HTMLButtonElement>('.seasons button',bar).forEach(b=>b.onclick=()=>{
|
|
S.season=b.dataset.season!; S.offset=0;
|
|
renderFeed(); loadEntries();
|
|
});
|
|
// The dividers in the list wait on knowing the podcast is told in seasons.
|
|
if(S.entries.length) renderEntries();
|
|
}
|
|
/// A podcast's seasons onto its page: kept ones at once, then the server's. Told in seasons only
|
|
/// once the answer came, its list is asked for again in its own order.
|
|
async function loadSeasons(f: Feed){
|
|
const had=seasonsShown(seasonsOf.get(f.id));
|
|
drawSeasons(f,seasonsOf.get(f.id));
|
|
try{
|
|
const got: SeasonCount[]=await api(`/api/feeds/${encodeURIComponent(f.id)}/seasons`);
|
|
seasonsOf.set(f.id,got);
|
|
drawSeasons(f,got);
|
|
// Drawn again, so the headings say the order too.
|
|
if(!had&&seasonsShown(got)&&!sortPicked&&S.feed===f.id){ S.offset=0; renderFeed(); loadEntries(); }
|
|
}catch{}
|
|
}
|
|
/// Whether a heading was clicked to sort the podcast open: until one is, a podcast told in
|
|
/// seasons runs in its own order, season 1 episode 1 first, not by date (#482).
|
|
let sortPicked=false;
|
|
/// The order a list is asked for in: the page's, or a seasoned podcast's own.
|
|
function sortFor(feed: string): {col: string, dir: 'asc'|'desc'}{
|
|
return feed===S.feed&&!sortPicked&&seasonsShown(seasonsOf.get(feed)) ? {col:'episode',dir:'asc'} : S.sort;
|
|
}
|
|
/// The unread of the season picked, for the count under the list; null for every season.
|
|
function seasonUnread(feed: string): number|null{
|
|
if(!S.season) return null;
|
|
return seasonsOf.get(feed)?.find(s=>seasonKey(s)===S.season)?.unread ?? null;
|
|
}
|
|
/// Where a season starts in the list, said above its first episode: only for every season at
|
|
/// once, newest or oldest first, where the episodes run season by season.
|
|
function seasonBreaks(): boolean{
|
|
const feed=S.feed ?? '', col=sortFor(feed).col;
|
|
return !S.season && (col==='episode'||col==='published') && seasonsShown(seasonsOf.get(feed));
|
|
}
|
|
/// Every episode of the season picked onto iPX, after asking: a season can be a few gigabytes.
|
|
function downloadSeason(f: Feed){
|
|
const s=seasonsOf.get(f.id)?.find(x=>seasonKey(x)===S.season); if(!s) return;
|
|
const name=seasonName(s.season);
|
|
openModal(`<h3>Download ${esc(name)}?</h3>
|
|
<p style="color:var(--dim)">Every episode of ${esc(name)} of <b>${esc(f.title||f.id)}</b> not on iPX yet, ${
|
|
plural(s.episodes,'episode')} in all, is downloaded in order, the first episode first.</p>
|
|
<div class="cardacts"><button class="btn ico" onclick="closeModal()" title="Cancel" aria-label="Cancel">${ICON.close}</button>
|
|
<button class="btn ico primary" id="sgo" title="Download ${esc(name)}" aria-label="Download ${esc(name)}">${ICON.download}</button></div>`);
|
|
$<HTMLButtonElement>('#sgo').onclick=async()=>{
|
|
try{
|
|
const r=await api(`/api/feeds/${encodeURIComponent(f.id)}/download-latest`,{method:'POST',body:JSON.stringify({season:S.season})});
|
|
closeModal();
|
|
toast(r.queued?`Downloading ${plural(r.queued,'episode')} of ${name}`:`All of ${name} is on iPX already`);
|
|
}catch(e){ toast(why(e),true); }
|
|
};
|
|
}
|