-
-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathannounce.ts
63 lines (51 loc) · 1.37 KB
/
announce.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import asyncStorage from './asyncStorage'
import redraw from './utils/redraw'
const STORAGE_KEY = 'announce'
export interface Announcement {
msg: string
date: string
}
let announce: Announcement | undefined
function keyOf(a: Announcement): string {
return a.date // includes milliseconds, effectively unique
}
function isExpired(dateStr: string): boolean {
return new Date(dateStr) < new Date()
}
export function get(): Announcement | undefined {
if (announce && isExpired(announce.date)) {
announce = undefined
}
return announce
}
export async function set(a?: Announcement): Promise<void> {
if (a) {
const newKey = keyOf(a)
const existing = await asyncStorage.get<string[]>(STORAGE_KEY)
if (existing?.includes(newKey)) {
// previously dismissed, no-op
return
}
}
announce = a
redraw()
}
export async function dismiss(): Promise<void> {
if (announce === undefined) {
return
}
const dismissKey = keyOf(announce)
// clear the announcement
announce = undefined
redraw()
// clean up existing dismissed cache and add this new entry
const existingDismissed = await asyncStorage.get<string[]>(STORAGE_KEY)
const newDismissed = (existingDismissed || []).filter(dateStr => !isExpired(dateStr))
newDismissed.push(dismissKey)
await asyncStorage.set(STORAGE_KEY, newDismissed)
}
export default {
get,
set,
dismiss,
}