mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-04 15:52:24 -08:00
33 lines
862 B
TypeScript
33 lines
862 B
TypeScript
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);
|
|
}
|
|
}
|
|
);
|