forked from mermaid-js/mermaid-live-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promo.ts
47 lines (42 loc) · 1.17 KB
/
promo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { writable, type Writable, get } from 'svelte/store';
import { persist, localStorage } from '../persist';
import Holiday2023 from './Holiday2023.svelte';
interface Promotion {
id: string;
startDate: Date;
endDate: Date;
component: ConstructorOfATypedSvelteComponent;
}
const promotions: Promotion[] = [
{
id: 'holiday-2023',
startDate: new Date('2023-11-27'),
endDate: new Date('2024-01-01'),
component: Holiday2023
}
];
export const dismissPromotion = (id?: string): void => {
if (!id) {
return;
}
dismissedPromotionsStore.update((dismissedIDs: string[]) => {
dismissedIDs.push(id);
return dismissedIDs;
});
};
const dismissedPromotionsStore: Writable<string[]> = persist(
writable([]),
localStorage(),
'dismissedPromotions'
);
export const getActivePromotion = (): Promotion | undefined => {
const dismissedPromotions = get(dismissedPromotionsStore);
const now = new Date();
return promotions
.filter(
(p: Promotion) =>
p.startDate <= now && p.endDate >= now && !dismissedPromotions.includes(p.id)
)
.sort((a: Promotion, b: Promotion) => b.endDate.getTime() - a.endDate.getTime())
.pop();
};