Files
suwayomi-material-you-webui/src/util/localStorage.tsx
T

25 lines
688 B
TypeScript
Raw Normal View History

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
2023-02-06 10:06:33 +01:00
function getItem<T>(key: string, defaultValue: T): T {
2023-05-11 18:00:37 +02:00
const item = window.localStorage.getItem(key);
2021-03-07 16:27:13 +03:30
2023-05-11 18:00:37 +02:00
if (item !== null) {
return JSON.parse(item);
2023-02-06 10:06:33 +01:00
}
2023-05-11 18:00:37 +02:00
window.localStorage.setItem(key, JSON.stringify(defaultValue));
return defaultValue;
2021-03-07 16:27:13 +03:30
}
function setItem<T>(key: string, value: T): void {
2023-05-11 18:00:37 +02:00
window.localStorage.setItem(key, JSON.stringify(value));
2021-03-07 16:27:13 +03:30
}
export default { getItem, setItem };