Page type-check: noImplicitAny, every parameter and variable typed (#235)
290 places the type-check could not see into, mostly parameters, now typed with web/src/types.ts (feeds, items, files, accounts, log lines, the directory's rows and items). Two changes run differently in form only: the seek and volume inputs divide Number(value) where they divided the string, and esc's table has a name. Null checks are #236. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,13 +5,14 @@ const $ = (s: string, r: ParentNode = document): any => r.querySelector(s);
|
||||
const $$ = (s: string, r: ParentNode = document): any[] => [...r.querySelectorAll(s)];
|
||||
/// What went wrong, from whatever a catch caught: the page throws Errors, but a catch cannot know.
|
||||
const why = (e: unknown): string => e instanceof Error ? e.message : String(e);
|
||||
const esc = s => (s??'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
||||
const ESCAPED: Record<string,string> = {'&':'&','<':'<','>':'>','"':'"',"'":'''};
|
||||
const esc = (s: string|null|undefined) => (s??'').replace(/[&<>"']/g,c=>ESCAPED[c]);
|
||||
// Icons: Font Awesome Free 7.3.1 by @fontawesome - https://fontawesome.com
|
||||
// License - https://fontawesome.com/license/free (Icons: CC BY 4.0). Embedded as SVG, only the
|
||||
// ones used, so there is no font to download and nothing is fetched from anyone else. Each takes
|
||||
// the button's own colour. To add one, copy the path from svgs/<style>/<name>.svg at the same tag.
|
||||
const fa=(box,body)=>`<svg class="i" viewBox="${box}" aria-hidden="true">${body}</svg>`;
|
||||
const ICON={
|
||||
const fa=(box: string,body: string)=>`<svg class="i" viewBox="${box}" aria-hidden="true">${body}</svg>`;
|
||||
const ICON: Record<string,string>={
|
||||
share:fa('0 0 512 512','<path fill="currentColor" d="M384 192c53 0 96-43 96-96s-43-96-96-96-96 43-96 96c0 5.4 .5 10.8 1.3 16L159.6 184.1c-16.9-15-39.2-24.1-63.6-24.1-53 0-96 43-96 96s43 96 96 96c24.4 0 46.6-9.1 63.6-24.1L289.3 400c-.9 5.2-1.3 10.5-1.3 16 0 53 43 96 96 96s96-43 96-96-43-96-96-96c-24.4 0-46.6 9.1-63.6 24.1L190.7 272c.9-5.2 1.3-10.5 1.3-16s-.5-10.8-1.3-16l129.7-72.1c16.9 15 39.2 24.1 63.6 24.1z"/>'), // solid/share-nodes
|
||||
plus:fa('0 0 448 512','<path fill="currentColor" d="M256 64c0-17.7-14.3-32-32-32s-32 14.3-32 32l0 160-160 0c-17.7 0-32 14.3-32 32s14.3 32 32 32l160 0 0 160c0 17.7 14.3 32 32 32s32-14.3 32-32l0-160 160 0c17.7 0 32-14.3 32-32s-14.3-32-32-32l-160 0 0-160z"/>'), // solid/plus
|
||||
circleMinus:fa('0 0 512 512','<path fill="currentColor" d="M512 256A256 256 0 1 0 0 256a256 256 0 1 0 512 0zM184 232l144 0c13.3 0 24 10.7 24 24s-10.7 24-24 24l-144 0c-13.3 0-24-10.7-24-24s10.7-24 24-24z"/>'), // solid/circle-minus, for Unsubscribe
|
||||
@@ -66,9 +67,9 @@ for(const b of $$('[data-icon]')) b.insertAdjacentHTML('afterbegin',ICON[b.datas
|
||||
/// becomes how much shorter it is once the copy is ready (the dead_air event).
|
||||
const DEAD_PENDING='<span class="dapend" title="DeadAir is still working on this episode. Until it is done, it plays at its full length.">Cutting dead air…</span>';
|
||||
/// What DeadAir took off an episode, as the list says it.
|
||||
function shorter(secs){ return secs<60?`${secs} s shorter`:`${Math.round(secs/60)} min shorter`; }
|
||||
function shorter(secs: number){ return secs<60?`${secs} s shorter`:`${Math.round(secs/60)} min shorter`; }
|
||||
/// A total of time saved: "3 h 12 min", "12 min", "40 s".
|
||||
function spanText(secs){
|
||||
function spanText(secs: number){
|
||||
const h=Math.floor(secs/3600), m=Math.floor(secs%3600/60);
|
||||
return h?`${h} h ${m} min`:m?`${m} min`:`${secs} s`;
|
||||
}
|
||||
@@ -107,12 +108,12 @@ async function api(url: string, opts?: RequestInit): Promise<any>{
|
||||
// A page in place of ipx's words is Cloudflare or Caddy answering for it: the message became a
|
||||
// whole 502 page (#219).
|
||||
if(!r.ok) throw new Error((!/html/.test(r.headers.get('Content-Type')||'')&&await r.text().catch(()=>''))||`${r.status} ${r.statusText}`.trim());
|
||||
return r.status===204?null:r.json().catch(()=>null);
|
||||
return r.status===204?null:r.json().catch((): null=>null);
|
||||
}
|
||||
// navigator.clipboard only exists in a secure context. Served over plain HTTP on a LAN
|
||||
// address it is undefined, so fall back to the old selection-based copy.
|
||||
async function copyText(text,btn){
|
||||
const flash=ok=>{
|
||||
async function copyText(text: string,btn?: HTMLElement){
|
||||
const flash=(ok: boolean)=>{
|
||||
if(!btn) return;
|
||||
// The button is an icon, so it is the markup that has to come back, not just its text.
|
||||
const was=btn.innerHTML;
|
||||
@@ -146,7 +147,7 @@ async function copyText(text,btn){
|
||||
///
|
||||
/// ponytail: a guess from the address. A private feed whose key has another name is shared
|
||||
/// without asking; add its pattern here when one turns up.
|
||||
async function share(title: string, url: string, btn?){
|
||||
async function share(title: string, url: string, btn?: HTMLElement){
|
||||
if(/patreon\.com\/|\/\/[^/]*@|[?&][^=]*(auth|token|key|secret|pass|sig)[^=]*=/i.test(url)
|
||||
&& !confirm('This address looks like it includes your own access to the feed, and anyone you '
|
||||
+'send it to could use it as you.\n\nShare it anyway?')) return;
|
||||
@@ -166,12 +167,12 @@ function toast(msg: string, bad?: boolean){
|
||||
$('#toasts').appendChild(t);
|
||||
setTimeout(()=>{t.style.opacity='0';t.style.transition='opacity .3s';setTimeout(()=>t.remove(),320)},bad?6000:3200);
|
||||
}
|
||||
const clock = s => {
|
||||
const clock = (s: number) => {
|
||||
s=Math.max(0,Math.floor(s||0));
|
||||
const h=Math.floor(s/3600),m=Math.floor(s%3600/60),x=s%60;
|
||||
return h?`${h}:${String(m).padStart(2,'0')}:${String(x).padStart(2,'0')}`:`${m}:${String(x).padStart(2,'0')}`;
|
||||
};
|
||||
const ago = t => {
|
||||
const ago = (t: number|null) => {
|
||||
if(!t) return 'never';
|
||||
const d=(Date.now()/1000)-t;
|
||||
if(d<3600) return Math.max(1,Math.round(d/60))+'m ago';
|
||||
@@ -179,16 +180,16 @@ const ago = t => {
|
||||
if(d<2592000) return Math.round(d/86400)+'d ago';
|
||||
return new Date(t*1000).toLocaleDateString(undefined,{month:'short',day:'numeric',year:'numeric'});
|
||||
};
|
||||
const dateOf = t => t?new Date(t*1000).toLocaleDateString(undefined,{month:'short',day:'numeric',year:'numeric'}):'';
|
||||
const dateOf = (t: number|null) => t?new Date(t*1000).toLocaleDateString(undefined,{month:'short',day:'numeric',year:'numeric'}):'';
|
||||
// A podcast episode is tens of MB, an article's image a few KB: whole MB made the small ones "0 MB".
|
||||
const mb = n => !n?'' : n<1048576?Math.max(1,Math.round(n/1024))+' KB'
|
||||
const mb = (n: number|null) => !n?'' : n<1048576?Math.max(1,Math.round(n/1024))+' KB'
|
||||
: n<1073741824?Math.round(n/1048576)+' MB' : (n/1073741824).toFixed(1)+' GB';
|
||||
const initials = s => (s||'?').replace(/[^A-Za-z0-9 ]/g,'').split(/\s+/).filter(Boolean).slice(0,2).map(w=>w[0]).join('').toUpperCase()||'?';
|
||||
const plural=(n,word)=>`${n} ${word}${n===1?'':'s'}`;
|
||||
const initials = (s: string|null) => (s||'?').replace(/[^A-Za-z0-9 ]/g,'').split(/\s+/).filter(Boolean).slice(0,2).map(w=>w[0]).join('').toUpperCase()||'?';
|
||||
const plural=(n: number,word: string)=>`${n} ${word}${n===1?'':'s'}`;
|
||||
// An initials tile takes one of these, by a hash of the name, so neighbours rarely match.
|
||||
const TINTS=['var(--accent)','var(--good)','var(--dim)','color-mix(in srgb,var(--accent),var(--good))'];
|
||||
const tint=name=>{ let h=0; for(const c of name||'?') h=(h*31+c.charCodeAt(0))>>>0; return TINTS[h%TINTS.length]; };
|
||||
function tileHTML(name,cls){
|
||||
const tint=(name: string)=>{ let h=0; for(const c of name||'?') h=(h*31+c.charCodeAt(0))>>>0; return TINTS[h%TINTS.length]; };
|
||||
function tileHTML(name: string,cls?: string){
|
||||
return `<div class="art ini ${cls||''}" style="--tint:${tint(name)}">${esc(initials(name))}</div>`;
|
||||
}
|
||||
/// Every image a feed names comes from iPX, which keeps a copy: fast after the first time, and
|
||||
@@ -203,7 +204,7 @@ function artHTML(url: string | null, name: string, cls?: string){
|
||||
: tileHTML(name,cls);
|
||||
}
|
||||
/// A folder's tile is its first four shows' art. With fewer than four to show, the folder's own.
|
||||
function folderArt(f,kids){
|
||||
function folderArt(f: Feed,kids: Feed[]){
|
||||
const art=kids.filter(c=>c.image).slice(0,4);
|
||||
if(art.length<4) return artHTML(f.image,f.title||f.id);
|
||||
// Tinted underneath, so art that fails to load leaves colour behind rather than a hole.
|
||||
@@ -212,7 +213,7 @@ function folderArt(f,kids){
|
||||
}
|
||||
|
||||
/// The sidebar slides over the page on a phone, so it needs a scrim to tap away.
|
||||
function nav(on){ $('#sidebar').classList.toggle('open',on); $('#scrim').hidden=!on; }
|
||||
function nav(on: boolean){ $('#sidebar').classList.toggle('open',on); $('#scrim').hidden=!on; }
|
||||
|
||||
/* ---------------- state ---------------- */
|
||||
const S: State = {
|
||||
@@ -230,19 +231,19 @@ const S: State = {
|
||||
const LIMIT = 50;
|
||||
|
||||
const UNITS = [['m','minutes'],['h','hours'],['d','days'],['w','weeks']];
|
||||
const UNIT_MINS = {m:1, h:60, d:1440, w:10080};
|
||||
const UNIT_MINS: Record<string,number> = {m:1, h:60, d:1440, w:10080};
|
||||
|
||||
/// Largest unit that divides evenly, so 120 reads "2 hours" not "120 minutes".
|
||||
function splitEvery(m){
|
||||
function splitEvery(m: number){
|
||||
if(!m) return {n:1, u:'h'};
|
||||
for(const u of ['w','d','h']) if(m % UNIT_MINS[u] === 0) return {n:m/UNIT_MINS[u], u};
|
||||
return {n:m, u:'m'};
|
||||
}
|
||||
function unitOptions(sel){
|
||||
function unitOptions(sel: string){
|
||||
return UNITS.map(([v,l]) =>
|
||||
`<option value="${v}"${sel===v?' selected':''}>${l}</option>`).join('');
|
||||
}
|
||||
function everyText(m){
|
||||
function everyText(m: number){
|
||||
if(!m) return '\u2014';
|
||||
const {n,u} = splitEvery(m);
|
||||
const name = {m:'min', h:'hour', d:'day', w:'week'}[u];
|
||||
|
||||
Reference in New Issue
Block a user