From dc815cdc4d1e1605eaec4d0a480e2bfa9929653c Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Tue, 4 Feb 2025 23:39:59 +0530 Subject: [PATCH] feat: Add hide duration for promotions --- src/lib/util/promos/promo.ts | 58 ++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/src/lib/util/promos/promo.ts b/src/lib/util/promos/promo.ts index f1bb4fb7..47635b49 100644 --- a/src/lib/util/promos/promo.ts +++ b/src/lib/util/promos/promo.ts @@ -1,52 +1,66 @@ -import { writable, type Writable, get } from 'svelte/store'; -import { persist, localStorage } from '../persist'; -import January2025 from './January2025.svelte'; import { env } from '$lib/util/env'; +import dayjs from 'dayjs'; +import duration from 'dayjs/plugin/duration'; +import type { Component } from 'svelte'; +import { get, writable, type Writable } from 'svelte/store'; +import { localStorage, persist } from '../persist'; +import January2025 from './January2025.svelte'; + +dayjs.extend(duration); interface Promotion { - id: string; startDate: Date; endDate: Date; - component: typeof January2025; + component: Component; + hideDuration: number; } -const promotions: Promotion[] = [ - { - id: 'promo-january-2025', +const promotions: Record = { + 'promo-january-2025': { startDate: new Date('2025-01-01'), endDate: new Date('2028-12-31'), - component: January2025 + component: January2025, + hideDuration: dayjs.duration(1, 'week').asMilliseconds() } -]; +}; export const dismissPromotion = (id?: string): void => { - if (!id) { + if (!id || !promotions[id]) { return; } - dismissedPromotionsStore.update((dismissedIDs: string[]) => { - dismissedIDs.push(id); + hiddenPromotionsStore.update((dismissedIDs) => { + dismissedIDs[id] = dayjs().add(promotions[id].hideDuration).valueOf(); return dismissedIDs; }); }; -const dismissedPromotionsStore: Writable = persist( - writable([]), +const hiddenPromotionsStore: Writable> = persist( + writable({}), localStorage(), - 'dismissedPromotions' + 'hiddenPromotions' ); -export const getActivePromotion = (): Promotion | undefined => { +export const getActivePromotion = (): (Promotion & { id: string }) | undefined => { if (!env.isEnabledMermaidChartLinks) { return; } - const dismissedPromotions = get(dismissedPromotionsStore); + const hiddenPromotions = get(hiddenPromotionsStore); const now = new Date(); - return promotions + const promotionWithID = Object.entries(promotions) .filter( - (p: Promotion) => - p.startDate <= now && p.endDate >= now && !dismissedPromotions.includes(p.id) + ([id, p]) => + dayjs(p.startDate).isBefore(now) && + dayjs(p.endDate).isAfter(now) && + (!hiddenPromotions[id] || dayjs(hiddenPromotions[id]).isBefore(now)) ) - .sort((a: Promotion, b: Promotion) => b.endDate.getTime() - a.endDate.getTime()) + .sort(([, a], [, b]) => dayjs(b.endDate).diff(dayjs(a.endDate))) .pop(); + + if (!promotionWithID) { + return; + } + + const [id, promotion] = promotionWithID; + return { ...promotion, id }; };