fix(console): keep the backoff escalating when prefs writes keep failing

The local fallback recorded a flat one week with no hide count, so a run of
rejected preference writes restarted the sequence every time instead of
continuing it. That contradicted the behaviour described one line above it.

Extracts the fallback into localSnooze.ts, which reproduces the same curve
as notifications.ts: coolOffPeriod * factor ** (hideCount - 1). Repeated
failures now escalate 1w, 2w, 4w, 8w, and a successful write still clears
the local copy so account prefs remain the source of truth.

Reads and writes are guarded, so private mode or a corrupt value fails
towards showing the banner rather than hiding it forever.

Adds localSnooze.test.ts covering the escalation, the formula parity with
account prefs, the expiry boundary, and the corrupt-value case.
This commit is contained in:
harsh mahajan committed 2026-09-02 10:39:06 +05:30
1 parent 065a6a1646
commit 2862860d24
3 files changed
+139 -11

No files matched your search

+23 -11
View File
@@ -1,19 +1,19 @@
<script lang="ts" context="module">
import { shouldShowNotification } from '$lib/helpers/notifications';
import { isLocallySnoozed, readLocalSnooze } from '$lib/helpers/localSnooze';
export const NEW_CONSOLE_BANNER_ID = 'newConsoleBanner';
/**
* Account prefs are the source of truth for the snooze: they carry the backoff count and
* follow the user across devices. This local key only covers the case where that write is
* rejected, so a dismissal is never silently lost.
* Account prefs are the source of truth for the snooze: they carry the hide count and follow
* the user across devices. This local key only covers a rejected prefs write, so a dismissal
* is never silently lost.
*/
const SNOOZE_FALLBACK_KEY = 'newConsoleBanner:snoozedUntil';
const SNOOZE_FALLBACK_MS = 7 * 24 * 60 * 60 * 1000;
export const SNOOZE_FALLBACK_KEY = 'newConsoleBanner:snooze';
export const SNOOZE_COOL_OFF_HOURS = 24 * 7;
export function canShowNewConsoleBanner(): boolean {
const until = Number(localStorage.getItem(SNOOZE_FALLBACK_KEY) ?? 0);
if (Number.isFinite(until) && Date.now() < until) return false;
if (isLocallySnoozed(readLocalSnooze(SNOOZE_FALLBACK_KEY), Date.now())) return false;
return shouldShowNotification(NEW_CONSOLE_BANNER_ID);
}
@@ -25,6 +25,7 @@
import { Layout, Typography } from '@appwrite.io/pink-svelte';
import { isTabletViewport } from '$lib/stores/viewport';
import { hideNotification } from '$lib/helpers/notifications';
import { clearLocalSnooze, nextLocalSnooze, writeLocalSnooze } from '$lib/helpers/localSnooze';
import { headerAlert } from '$lib/stores/headerAlert';
import { activeHeaderAlert } from '$routes/(console)/store';
import GradientBanner from './billing/gradientBanner.svelte';
@@ -47,12 +48,23 @@
// a week at first, doubling each time. Someone who keeps closing it stops seeing it, but a
// single stray click on the X does not remove the message for the whole rollout.
try {
await hideNotification(id, { coolOffPeriod: 24 * 7, exponentialBackoff: true });
localStorage.removeItem(SNOOZE_FALLBACK_KEY);
await hideNotification(id, {
coolOffPeriod: SNOOZE_COOL_OFF_HOURS,
exponentialBackoff: true
});
clearLocalSnooze(SNOOZE_FALLBACK_KEY);
} catch {
// Prefs rejected the write, so the snooze would be lost on the next load. Hold it in
// this browser instead: a flat week, no backoff, cleared once prefs accept a write.
localStorage.setItem(SNOOZE_FALLBACK_KEY, String(Date.now() + SNOOZE_FALLBACK_MS));
// this browser instead, escalating on the same curve so repeated failures continue
// the sequence rather than restarting at a week. Cleared once a write succeeds.
writeLocalSnooze(
SNOOZE_FALLBACK_KEY,
nextLocalSnooze(
readLocalSnooze(SNOOZE_FALLBACK_KEY),
SNOOZE_COOL_OFF_HOURS,
Date.now()
)
);
}
}
</script>
+51
View File
@@ -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);
});
});
+65
View File
@@ -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.
}
}