Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ui/test/downloads-belt.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import assert from 'node:assert/strict';
import { test } from 'node:test';
import {
ENDS,
finishParts,
finishedToday,
hoursMinutes,
leftover,
LEFTOVER_CHANCE,
lidOpen,
readerHourCycle,
startOfDay,
variations,
} from '../widgets/downloads/belt-logic.js';
Expand Down Expand Up @@ -63,3 +65,19 @@ test('leftovers appear about one load in ten and stay inside the belt', () => {
assert.deepEqual([...kinds].sort(), ['cutter', 'roll']);
assert.deepEqual(leftover(42), leftover(42));
});

test('a finish time names the day in the interface language and keeps the reader clock', () => {
const now = new Date(2026, 9, 27, 9, 0).getTime();
const fri = new Date(2026, 9, 30, 13, 5).getTime();
const us = readerHourCycle('en-US');
assert.deepEqual(finishParts(fri, now, 'de', us), { time: 'Fr. 1:05', half: 'pm' });
assert.deepEqual(finishParts(fri, now, 'en', readerHourCycle('de-DE')), { time: 'Fri 13:05', half: '' });
assert.match(finishParts(fri, now, 'fa', us).time, /^جمعه /);
});

test('a finish time today shows only the clock, and a week out only the date', () => {
const now = new Date(2026, 9, 27, 9, 0).getTime();
const us = readerHourCycle('en-US');
assert.deepEqual(finishParts(new Date(2026, 9, 27, 9, 30).getTime(), now, 'en', us), { time: '9:30', half: 'am' });
assert.deepEqual(finishParts(new Date(2026, 10, 9, 9, 30).getTime(), now, 'de', us), { time: '9. Nov.', half: '' });
});
33 changes: 33 additions & 0 deletions ui/widgets/downloads/belt-logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,39 @@ export function startOfDay(now) {
return d.getTime();
}

/** The 12- or 24-hour clock the reader's own locale uses.
@param {string} [locale] @returns {string|undefined} */
export function readerHourCycle(locale) {
try {
return new Intl.DateTimeFormat(locale || 'en', { hour: 'numeric' }).resolvedOptions().hourCycle;
} catch {
return undefined;
}
}

/** A finish time in the interface language, without its day period. `half` is
'am' or 'pm' when the clock has one, so the caller names it from its catalog.
@param {number} ms @param {number} now @param {string} lang @param {string|undefined} hourCycle
@returns {{ time: string, half: ''|'am'|'pm' }} */
export function finishParts(ms, now, lang, hourCycle) {
const days = (startOfDay(ms) - startOfDay(now)) / 86400000;
/** @type {Intl.DateTimeFormatOptions} */
const opts =
days < 1
? { hour: 'numeric', minute: '2-digit', hourCycle }
: days < 7
? { weekday: 'short', hour: 'numeric', minute: '2-digit', hourCycle }
: { month: 'short', day: 'numeric' };
const parts = new Intl.DateTimeFormat(lang, opts).formatToParts(ms);
const time = parts
.filter(p => p.type !== 'dayPeriod')
.map(p => p.value)
.join('')
.trim();
if (!parts.some(p => p.type === 'dayPeriod')) return { time, half: '' };
return { time, half: new Date(ms).getHours() < 12 ? 'am' : 'pm' };
}

/** How many finished today, and the newest.
@param {number[]} finished epoch ms @param {number} now
@returns {{ count: number, last: number|null }} */
Expand Down
2 changes: 0 additions & 2 deletions ui/widgets/downloads/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ async function sabnzbd(ctx) {
});
if (r.status === 401 || r.status === 403) ctx.fail('SABnzbd refused the API key', { kind: ctx.KIND.AUTH });
if (r.status !== 200) ctx.fail(`SABnzbd returned HTTP ${r.status}`, { kind: ctx.KIND.UPSTREAM });
if (typeof r.data === 'string' && /api key/i.test(r.data))
ctx.fail('SABnzbd refused the API key', { kind: ctx.KIND.AUTH });
if (!r.data || typeof r.data !== 'object' || !r.data[mode])
ctx.fail('SABnzbd sent a reply in an unexpected shape', { kind: ctx.KIND.UPSTREAM });
return r.data[mode];
Expand Down
23 changes: 5 additions & 18 deletions ui/widgets/downloads/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
poll, getConfig, loadStrings, wt, wtCount, formatNumber, formatList, localiseDigits, linkTo, errorState, readingTime,
} from '/js/widget-toolbox.js?v=66813091';
import { ensureSprite } from '/js/icon-set.js?v=26655aca';
import { variations, finishedToday, hoursMinutes, leftover, lidOpen, startOfDay } from './belt-logic.js';
import { variations, finishedToday, hoursMinutes, leftover, lidOpen, finishParts, readerHourCycle } from './belt-logic.js';

const SVG = 'http://www.w3.org/2000/svg';
const TONES = [
Expand Down Expand Up @@ -190,25 +190,12 @@
return bytes >= 1e9 ? unit(bytes / 1e9, 'gigabyte', 1) : unit(bytes / 1e6, 'megabyte');
}
const percent = p => iso(formatNumber(p, { style: 'percent', maximumFractionDigits: 0 }));
const LANG = new URLSearchParams(location.search).get('lang') || 'en';
const HOUR_CYCLE = readerHourCycle(navigator.language);
function clockParts(ms) {
try {
const days = (startOfDay(ms) - startOfDay(Date.now())) / 86400000;
const opts =
days < 1
? { hour: 'numeric', minute: '2-digit' }
: days < 7
? { weekday: 'short', hour: 'numeric', minute: '2-digit' }
: { month: 'short', day: 'numeric' };
const parts = new Intl.DateTimeFormat(navigator.language || 'en', opts).formatToParts(ms);
const time = parts.filter(p => p.type !== 'dayPeriod').map(p => p.value).join('').trim();
const found = parts.find(p => p.type === 'dayPeriod')?.value || '';
/* The reader's locale picks a 12-hour clock; the interface language names its halves. */
const morning = new Date(ms).getHours() < 12;
const period = !found
? ''
: morning
? wt('ui.am', 'AM')
: wt('ui.pm', 'PM');
const { time, half } = finishParts(ms, Date.now(), LANG, HOUR_CYCLE);
const period = half === 'am' ? wt('ui.am', 'AM') : half === 'pm' ? wt('ui.pm', 'PM') : '';
return { time: localiseDigits(time), period };
} catch {
return { time: localiseDigits(new Date(ms).toTimeString().slice(0, 5)), period: '' };
Expand Down
Loading