pro-nudge.ts

1.2 KB · raw

1const NEXT_AT_KEY = "peach_pro_nudge_next_at";2const DAY_MS = 24 * 60 * 60 * 1000;3const FIRST_SIGHT_GRACE_MS = 14 * DAY_MS;4const CADENCE_MS = 30 * DAY_MS;5const SNOOZE_MS = 90 * DAY_MS;67type NextAt = number | "never";89/**10 * Returns whether the Peach Pro nudge is due now, and if so records the11 * showing so a popup closed without a choice waits a full cadence window.12 */13export async function claimProNudge(now = Date.now()): Promise<boolean> {14  let nextAt: NextAt | undefined;15  try {16    nextAt = (await chrome.storage.local.get(NEXT_AT_KEY))[NEXT_AT_KEY] as NextAt | undefined;17  } catch {18    return false;19  }20  if (nextAt === "never") return false;21  if (typeof nextAt !== "number") {22    await chrome.storage.local.set({ [NEXT_AT_KEY]: now + FIRST_SIGHT_GRACE_MS });23    return false;24  }25  if (now < nextAt) return false;26  await chrome.storage.local.set({ [NEXT_AT_KEY]: now + CADENCE_MS });27  return true;28}2930export async function snoozeProNudge(now = Date.now()): Promise<void> {31  await chrome.storage.local.set({ [NEXT_AT_KEY]: now + SNOOZE_MS });32}3334export async function silenceProNudge(): Promise<void> {35  await chrome.storage.local.set({ [NEXT_AT_KEY]: "never" satisfies NextAt });36}