diff options
author | Sebastian <sebasjm@gmail.com> | 2023-05-29 14:53:06 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-05-29 14:53:06 -0300 |
commit | be9d3dad83801806c9a7d99c925aa2b86571a70b (patch) | |
tree | e98c7c1810b8cc8e081b1098d0bb36a7e5a6a692 /packages/demobank-ui/src/hooks/settings.ts | |
parent | d4dd82eda110b165e62f451bb1bad65a2c9963d5 (diff) | |
download | wallet-core-be9d3dad83801806c9a7d99c925aa2b86571a70b.tar.xz |
use new local storage api
Diffstat (limited to 'packages/demobank-ui/src/hooks/settings.ts')
-rw-r--r-- | packages/demobank-ui/src/hooks/settings.ts | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/packages/demobank-ui/src/hooks/settings.ts b/packages/demobank-ui/src/hooks/settings.ts index 325b583f9..46b31bf2a 100644 --- a/packages/demobank-ui/src/hooks/settings.ts +++ b/packages/demobank-ui/src/hooks/settings.ts @@ -14,36 +14,44 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { useLocalStorage } from "@gnu-taler/web-util/browser"; +import { + Codec, + buildCodecForObject, + codecForString, + codecOptional, +} from "@gnu-taler/taler-util"; +import { buildStorageKey, useLocalStorage } from "@gnu-taler/web-util/browser"; interface Settings { currentWithdrawalOperationId: string | undefined; } +export const codecForSettings = (): Codec<Settings> => + buildCodecForObject<Settings>() + .property("currentWithdrawalOperationId", codecOptional(codecForString())) + .build("Settings"); + const defaultSettings: Settings = { currentWithdrawalOperationId: undefined, }; -function parse_json_or_undefined<T>(str: string | undefined): T | undefined { - if (str === undefined) return undefined; - try { - return JSON.parse(str); - } catch { - return undefined; - } -} +const DEMOBANK_SETTINGS_KEY = buildStorageKey( + "demobank-settings", + codecForSettings(), +); export function useSettings(): [ Readonly<Settings>, <T extends keyof Settings>(key: T, value: Settings[T]) => void, ] { - const { value, update } = useLocalStorage("demobank-settings"); + const { value, update } = useLocalStorage( + DEMOBANK_SETTINGS_KEY, + defaultSettings, + ); - const parsed: Settings = parse_json_or_undefined(value) ?? defaultSettings; function updateField<T extends keyof Settings>(k: T, v: Settings[T]) { - const newValue = { ...parsed, [k]: v }; - const json = JSON.stringify(newValue); - update(json); + const newValue = { ...value, [k]: v }; + update(newValue); } - return [parsed, updateField]; + return [value, updateField]; } |