mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-11-04 14:30:29 +03:00
Place our storage handling into a state module instead and reference it
This commit is contained in:
parent
b972f308f4
commit
b689089599
1 changed files with 32 additions and 0 deletions
32
src/app/state/pushSubscription.ts
Normal file
32
src/app/state/pushSubscription.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import { atom } from 'jotai';
|
||||
import {
|
||||
atomWithLocalStorage,
|
||||
getLocalStorageItem,
|
||||
setLocalStorageItem,
|
||||
} from './utils/atomWithLocalStorage';
|
||||
|
||||
const PUSH_SUBSCRIPTION_KEY = 'webPushSubscription';
|
||||
|
||||
const basePushSubscriptionAtom = atomWithLocalStorage<PushSubscriptionJSON | null>(
|
||||
PUSH_SUBSCRIPTION_KEY,
|
||||
(key) => getLocalStorageItem<PushSubscriptionJSON | null>(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);
|
||||
}
|
||||
}
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue