-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwidgetData.ts
More file actions
72 lines (60 loc) · 2.01 KB
/
Copy pathwidgetData.ts
File metadata and controls
72 lines (60 loc) · 2.01 KB
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
64
65
66
67
68
69
70
71
72
import type { PickupInfo } from "./types"
declare const Storage: {
get<T>(key: string): T | null
set<T>(key: string, value: T): boolean
}
export const WIDGET_DATA_KEY = "smsPickup_widget_data_cache_v1"
export const WIDGET_ACTIONS_KEY = "smsPickup_widget_actions_v1"
const CONFIG_KEY = "smsPickup_widget_config_v2"
export type WidgetAction =
| { type: "toggle"; code: string; createdAt: number }
| { type: "markAll"; createdAt: number }
export type WidgetDataCache = {
items: PickupInfo[]
showCount: number
updatedAt: number
}
function fallbackShowCount() {
const cfg = Storage.get<{ widgetShowCount?: number }>(CONFIG_KEY) || {}
return Math.max(1, Math.min(8, Number(cfg.widgetShowCount) || 5))
}
export function loadWidgetData(): WidgetDataCache {
const cache = Storage.get<WidgetDataCache>(WIDGET_DATA_KEY)
if (cache && Array.isArray(cache.items)) {
return {
items: cache.items,
showCount: Math.max(1, Math.min(8, Number(cache.showCount) || fallbackShowCount())),
updatedAt: Number(cache.updatedAt) || 0,
}
}
return {
items: [],
showCount: fallbackShowCount(),
updatedAt: 0,
}
}
export function saveWidgetData(items: PickupInfo[], showCount: number) {
return Storage.set(WIDGET_DATA_KEY, {
items,
showCount: Math.max(1, Math.min(8, showCount || fallbackShowCount())),
updatedAt: Date.now(),
})
}
export function applyWidgetToggleToCache(code: string) {
const cache = loadWidgetData()
saveWidgetData(cache.items.filter((item) => item.code !== code), cache.showCount)
}
export function clearWidgetCacheItems() {
const cache = loadWidgetData()
saveWidgetData([], cache.showCount)
}
export function appendWidgetAction(action: WidgetAction) {
const actions = Storage.get<WidgetAction[]>(WIDGET_ACTIONS_KEY) || []
actions.push(action)
Storage.set(WIDGET_ACTIONS_KEY, actions.slice(-50))
}
export function takeWidgetActions() {
const actions = Storage.get<WidgetAction[]>(WIDGET_ACTIONS_KEY) || []
Storage.set(WIDGET_ACTIONS_KEY, [])
return actions
}