-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
26 lines (24 loc) · 746 Bytes
/
index.ts
File metadata and controls
26 lines (24 loc) · 746 Bytes
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
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
/**
* @param app app ID, e.g. "mail"
* @param key name of the property
* @param fallback optional parameter to use as default value
* @throws if the key can't be found
*/
export function loadState<T>(app: string, key: string, fallback?: T): T {
const elem = document.querySelector<HTMLInputElement>(`#initial-state-${app}-${key}`)
if (elem === null) {
if (fallback !== undefined) {
return fallback
}
throw new Error(`Could not find initial state ${key} of ${app}`)
}
try {
return JSON.parse(atob(elem.value))
} catch (e) {
throw new Error(`Could not parse initial state ${key} of ${app}`)
}
}