From c57c7ea220209431bda0af44c882b76c845d93e0 Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 8 Jul 2024 18:30:11 -0700 Subject: [PATCH] useAtomValueSafe --- frontend/util/util.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/frontend/util/util.ts b/frontend/util/util.ts index 4f06f3f2..6221db21 100644 --- a/frontend/util/util.ts +++ b/frontend/util/util.ts @@ -3,6 +3,7 @@ import base64 from "base64-js"; import clsx from "clsx"; +import * as jotai from "jotai"; function isBlank(str: string): boolean { return str == null || str == ""; @@ -152,6 +153,15 @@ function jotaiLoadableValue(value: Loadable, def: T): T { return def; } +const NullAtom = jotai.atom(null); + +function useAtomValueSafe(atom: jotai.Atom): T { + if (atom == null) { + return jotai.useAtomValue(NullAtom) as T; + } + return jotai.useAtomValue(atom); +} + export { base64ToArray, base64ToString, @@ -163,4 +173,5 @@ export { jsonDeepEqual, makeIconClass, stringToBase64, + useAtomValueSafe, };