From b68908959997ceabe8f3b3115ea025d11e6e36f0 Mon Sep 17 00:00:00 2001 From: Gigiaj Date: Sun, 29 Jun 2025 23:27:48 -0500 Subject: [PATCH] Place our storage handling into a state module instead and reference it --- src/app/state/pushSubscription.ts | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/app/state/pushSubscription.ts diff --git a/src/app/state/pushSubscription.ts b/src/app/state/pushSubscription.ts new file mode 100644 index 00000000..1f5f10e3 --- /dev/null +++ b/src/app/state/pushSubscription.ts @@ -0,0 +1,32 @@ +import { atom } from 'jotai'; +import { + atomWithLocalStorage, + getLocalStorageItem, + setLocalStorageItem, +} from './utils/atomWithLocalStorage'; + +const PUSH_SUBSCRIPTION_KEY = 'webPushSubscription'; + +const basePushSubscriptionAtom = atomWithLocalStorage( + PUSH_SUBSCRIPTION_KEY, + (key) => getLocalStorageItem(key, null), + (key, value) => { + setLocalStorageItem(key, value); + } +); + +export const pushSubscriptionAtom = atom< + PushSubscriptionJSON | null, + [PushSubscription | null], + void +>( + (get) => get(basePushSubscriptionAtom), + (get, set, subscription: PushSubscription | null) => { + if (subscription) { + const subscriptionJSON = subscription.toJSON(); + set(basePushSubscriptionAtom, subscriptionJSON); + } else { + set(basePushSubscriptionAtom, null); + } + } +);