-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstorage.ts
102 lines (82 loc) · 3.16 KB
/
storage.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { DEFAULT_OPTIONS, type OptionsForm } from './options'
import { type LeetcodeVersion } from './pages/Content/leetcode-version'
import { type Nullish } from './pages/Popup/types/Nullish'
export const ENABLED_STORAGE_KEY = 'enabled'
export const LEETCODE_VERSION_STORAGE_KEY = 'leetcodeVersion'
export const OPTIONS_STORAGE_KEY = 'options'
export const AUTO_RESET_CODE_ENABLED_STORAGE_KEY = 'autoResetCodeEnabled'
export const INSERT_YOUTUBE_LINK_STORAGE_KEY = 'insertYoutubeLinkEnabled'
export const INSERT_DISLIKE_COUNT_STORAGE_KEY = 'insertDislikeCountEnabled'
export interface StorageSchema {
[ENABLED_STORAGE_KEY]: boolean,
[LEETCODE_VERSION_STORAGE_KEY]: Nullish<LeetcodeVersion>,
[OPTIONS_STORAGE_KEY]: OptionsForm,
[AUTO_RESET_CODE_ENABLED_STORAGE_KEY]: boolean,
[INSERT_YOUTUBE_LINK_STORAGE_KEY]: boolean,
[INSERT_DISLIKE_COUNT_STORAGE_KEY]: boolean,
}
export const storageDefaultValues: StorageSchema = {
[ENABLED_STORAGE_KEY]: true,
[LEETCODE_VERSION_STORAGE_KEY]: null,
[OPTIONS_STORAGE_KEY]: DEFAULT_OPTIONS,
[AUTO_RESET_CODE_ENABLED_STORAGE_KEY]: false,
[INSERT_YOUTUBE_LINK_STORAGE_KEY]: true,
[INSERT_DISLIKE_COUNT_STORAGE_KEY]: true,
}
export type StorageKey = keyof StorageSchema
const localStorageKeys = new Set<StorageKey>([
LEETCODE_VERSION_STORAGE_KEY,
])
export async function getStorage <
Key extends StorageKey
> (key: Key): Promise<StorageSchema[Key]> {
const value = localStorageKeys.has(key)
? (await chrome.storage.local.get(key))[key]
: (await chrome.storage.sync.get(key))[key]
return value ?? structuredClone(storageDefaultValues[key])
}
export async function setStorage<
Key extends StorageKey
> (key: Key, value: StorageSchema[Key]) {
if (localStorageKeys.has(key)) {
await chrome.storage.local.set({ [key]: value })
} else {
await chrome.storage.sync.set({ [key]: value })
}
}
export const loadIsEnabled = () => {
return getStorage(ENABLED_STORAGE_KEY)
}
export const saveIsEnabled = (isEnabled: boolean) => {
return setStorage(ENABLED_STORAGE_KEY, isEnabled)
}
export const loadLeetcodeVersion = () => {
return getStorage(LEETCODE_VERSION_STORAGE_KEY)
}
export const saveLeetcodeVersion = (version: LeetcodeVersion) => {
return setStorage(LEETCODE_VERSION_STORAGE_KEY, version)
}
export const loadOptions = () => {
return getStorage(OPTIONS_STORAGE_KEY)
}
export const saveOptions = (options: OptionsForm) => {
return setStorage(OPTIONS_STORAGE_KEY, options)
}
export const loadIsAutoResetCodeEnabled = () => {
return getStorage(AUTO_RESET_CODE_ENABLED_STORAGE_KEY)
}
export const saveIsAutoResetCodeEnabled = (isEnabled: boolean) => {
return setStorage(AUTO_RESET_CODE_ENABLED_STORAGE_KEY, isEnabled)
}
export const loadIsInsertYoutubeLinkEnabled = () => {
return getStorage(INSERT_YOUTUBE_LINK_STORAGE_KEY)
}
export const saveIsInsertYoutubeLinkEnabled = (isEnabled: boolean) => {
return setStorage(INSERT_YOUTUBE_LINK_STORAGE_KEY, isEnabled)
}
export const loadIsInsertDislikeCountEnabled = () => {
return getStorage(INSERT_DISLIKE_COUNT_STORAGE_KEY)
}
export const saveIsInsertDislikeCountEnabled = (isEnabled: boolean) => {
return setStorage(INSERT_DISLIKE_COUNT_STORAGE_KEY, isEnabled)
}