Add store to push notif setting to local storage (so we can manage the pusher state cleanly)

This commit is contained in:
Gigiaj 2025-06-10 19:23:51 -05:00
parent b148cc3d1b
commit 654f32bd31

View file

@ -1,5 +1,6 @@
import React, { useCallback, useEffect, useState } from 'react'; import React, { useCallback, useEffect, useState } from 'react';
import { Box, Text, Switch, Button, color, Spinner } from 'folds'; import { Box, Text, Switch, Button, color, Spinner } from 'folds';
import { IPusherRequest } from 'matrix-js-sdk';
import { SequenceCard } from '../../../components/sequence-card'; import { SequenceCard } from '../../../components/sequence-card';
import { SequenceCardStyle } from '../styles.css'; import { SequenceCardStyle } from '../styles.css';
import { SettingTile } from '../../../components/setting-tile'; import { SettingTile } from '../../../components/setting-tile';
@ -92,59 +93,30 @@ function EmailNotification() {
function WebPushNotificationSetting() { function WebPushNotificationSetting() {
const mx = useMatrixClient(); const mx = useMatrixClient();
const clientConfig = useClientConfig(); const clientConfig = useClientConfig();
const [userWantsWebPush, setUserWantsWebPush] = useSetting(settingsAtom, 'enableWebPush'); const [userPushPreference, setUserPushPreference] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(true);
const browserPermission = usePermissionState('notifications', getNotificationState()); const browserPermission = usePermissionState('notifications', getNotificationState());
const [isPushSubscribed, setIsPushSubscribed] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(true); // Start loading to check status
const checkSubscriptionStatus = useCallback(async () => {
if (
browserPermission === 'granted' &&
'serviceWorker' in navigator &&
'PushManager' in window
) {
setIsLoading(true);
try {
const registration = await navigator.serviceWorker.ready;
const subscription = await registration.pushManager.getSubscription();
setIsPushSubscribed(!!subscription);
} catch (err) {
setIsPushSubscribed(false);
} finally {
setIsLoading(false);
}
} else {
setIsPushSubscribed(false);
setIsLoading(false);
}
}, [browserPermission]);
useEffect(() => { useEffect(() => {
checkSubscriptionStatus(); const storedPreference = localStorage.getItem(PUSH_PREFERENCE_KEY);
}, [checkSubscriptionStatus]); setUserPushPreference(storedPreference === 'true');
setIsLoading(false);
}, []);
const handleRequestPermissionAndEnable = async () => { const handleRequestPermissionAndEnable = async () => {
setIsLoading(true); setIsLoading(true);
try { try {
const permissionResult = await requestBrowserNotificationPermission(); const permissionResult = await requestBrowserNotificationPermission();
if (permissionResult === 'granted') { if (permissionResult === 'granted') {
setUserWantsWebPush(true);
await enablePushNotifications(mx, clientConfig); await enablePushNotifications(mx, clientConfig);
} else { localStorage.setItem('cinny_web_push_enabled', 'true');
setUserWantsWebPush(false); setUserPushPreference(true);
} }
} catch (error: any) {
setUserWantsWebPush(false);
} finally { } finally {
await checkSubscriptionStatus();
setIsLoading(false); setIsLoading(false);
} }
}; };
const handlePushSwitchChange = async (wantsPush: boolean) => { const handlePushSwitchChange = async (wantsPush: boolean) => {
setIsLoading(true); setIsLoading(true);
setUserWantsWebPush(wantsPush);
try { try {
if (wantsPush) { if (wantsPush) {
@ -152,30 +124,23 @@ function WebPushNotificationSetting() {
} else { } else {
await disablePushNotifications(mx, clientConfig); await disablePushNotifications(mx, clientConfig);
} }
} catch (error: any) { localStorage.setItem('cinny_web_push_enabled', String(wantsPush));
setUserWantsWebPush(!wantsPush); setUserPushPreference(wantsPush);
} finally { } finally {
await checkSubscriptionStatus();
setIsLoading(false); setIsLoading(false);
} }
}; };
let descriptionText = 'Receive notifications when the app is closed or in the background.';
if (browserPermission === 'granted' && isPushSubscribed) {
descriptionText =
'You are subscribed to receive notifications when the app is in the background.';
}
return ( return (
<SettingTile <SettingTile
title="Background Push Notifications" title="Background Push Notifications"
description={ description={
browserPermission === 'denied' ? ( browserPermission === 'denied' ? (
<Text as="span" style={{ color: color.Critical.Main }} size="T200"> <Text as="span" style={{ color: color.Critical.Main }} size="T200">
Notification permission is blocked by your browser. Please allow it from site settings. Permission blocked. Please allow notifications in your browser settings.
</Text> </Text>
) : ( ) : (
<span>{descriptionText}</span> 'Receive notifications when the app is closed or in the background.'
) )
} }
after={ after={
@ -186,7 +151,10 @@ function WebPushNotificationSetting() {
<Text size="B300">Enable</Text> <Text size="B300">Enable</Text>
</Button> </Button>
) : browserPermission === 'granted' ? ( ) : browserPermission === 'granted' ? (
<Switch value={userWantsWebPush && isPushSubscribed} onChange={handlePushSwitchChange} /> <Switch
value={userPushPreference}
onChange={handlePushSwitchChange}
/>
) : null ) : null
} }
/> />