diff --git a/src/lib/components/newConsoleBanner.svelte b/src/lib/components/newConsoleBanner.svelte index e94353807..35c4a4ef3 100644 --- a/src/lib/components/newConsoleBanner.svelte +++ b/src/lib/components/newConsoleBanner.svelte @@ -1,19 +1,19 @@ diff --git a/src/lib/helpers/localSnooze.test.ts b/src/lib/helpers/localSnooze.test.ts new file mode 100644 index 000000000..f0fc11006 --- /dev/null +++ b/src/lib/helpers/localSnooze.test.ts @@ -0,0 +1,51 @@ +import { describe, expect, it } from 'vitest'; +import { isLocallySnoozed, nextLocalSnooze, type LocalSnooze } from './localSnooze'; + +const HOUR = 3600000; +const WEEK_HOURS = 24 * 7; +const NOW = 1_700_000_000_000; + +describe('nextLocalSnooze', () => { + it('starts at the base cool-off', () => { + const snooze = nextLocalSnooze(null, WEEK_HOURS, NOW); + + expect(snooze.hideCount).toBe(1); + expect(snooze.expiry).toBe(NOW + WEEK_HOURS * HOUR); + }); + + // The point of the fallback: a run of failed preference writes has to keep escalating, + // otherwise the user is asked again every week forever. + it('doubles on each repeat instead of restarting at the base period', () => { + let snooze: LocalSnooze | null = null; + const weeks: number[] = []; + + for (let i = 0; i < 4; i++) { + snooze = nextLocalSnooze(snooze, WEEK_HOURS, NOW); + weeks.push((snooze.expiry - NOW) / (WEEK_HOURS * HOUR)); + } + + expect(weeks).toEqual([1, 2, 4, 8]); + expect(snooze?.hideCount).toBe(4); + }); + + it('matches the account-prefs formula, coolOff * factor ** (hideCount - 1)', () => { + const third = nextLocalSnooze({ hideCount: 2, expiry: 0 }, WEEK_HOURS, NOW); + + expect(third.expiry).toBe(NOW + WEEK_HOURS * HOUR * 2 ** 2); + }); +}); + +describe('isLocallySnoozed', () => { + it('holds until the deadline, then releases', () => { + const snooze = nextLocalSnooze(null, WEEK_HOURS, NOW); + + expect(isLocallySnoozed(snooze, NOW)).toBe(true); + expect(isLocallySnoozed(snooze, snooze.expiry - 1)).toBe(true); + expect(isLocallySnoozed(snooze, snooze.expiry)).toBe(false); + }); + + it('never hides the notification for a missing or corrupt value', () => { + expect(isLocallySnoozed(null, NOW)).toBe(false); + expect(isLocallySnoozed({ hideCount: 1, expiry: NaN }, NOW)).toBe(false); + }); +}); diff --git a/src/lib/helpers/localSnooze.ts b/src/lib/helpers/localSnooze.ts new file mode 100644 index 000000000..9b1486eda --- /dev/null +++ b/src/lib/helpers/localSnooze.ts @@ -0,0 +1,65 @@ +/** + * Browser-local mirror of the account-prefs snooze in `notifications.ts`. + * + * Account prefs are the source of truth: they carry the hide count and follow the user across + * devices. This only covers a rejected prefs write, where the dismissal would otherwise be lost + * on the next load. It deliberately reproduces the same escalation, so intermittent failures + * continue the sequence instead of restarting it at the base period every time. + */ + +const COOL_OFF_MS = 3600000; + +export type LocalSnooze = { + /** How many times this has been snoozed locally. */ + hideCount: number; + /** Epoch ms after which the notification may show again. */ + expiry: number; +}; + +export function nextLocalSnooze( + previous: LocalSnooze | null, + coolOffPeriodHours: number, + now: number, + backoffFactor = 2 +): LocalSnooze { + const hideCount = (previous?.hideCount ?? 0) + 1; + + return { + hideCount, + expiry: now + coolOffPeriodHours * COOL_OFF_MS * backoffFactor ** (hideCount - 1) + }; +} + +export function isLocallySnoozed(snooze: LocalSnooze | null, now: number): boolean { + return !!snooze && Number.isFinite(snooze.expiry) && now < snooze.expiry; +} + +export function readLocalSnooze(key: string): LocalSnooze | null { + try { + const raw = localStorage.getItem(key); + if (!raw) return null; + + const parsed = JSON.parse(raw) as LocalSnooze; + return Number.isFinite(parsed?.expiry) ? parsed : null; + } catch { + // A malformed or unreadable value must not keep the notification hidden forever. + return null; + } +} + +export function writeLocalSnooze(key: string, snooze: LocalSnooze) { + try { + localStorage.setItem(key, JSON.stringify(snooze)); + } catch { + // Storage can be unavailable in private mode. Nothing to recover here: the notification + // reappears on the next load, which is the safer direction to fail. + } +} + +export function clearLocalSnooze(key: string) { + try { + localStorage.removeItem(key); + } catch { + // See writeLocalSnooze. + } +}