Swap to using the atom values instead of raw setting them in the component. Passing the values forward as needed.

This commit is contained in:
Gigiaj 2025-06-29 23:35:07 -05:00
parent f31087e685
commit 72bb04f190

View file

@ -93,12 +93,15 @@ function EmailNotification() {
function WebPushNotificationSetting() { function WebPushNotificationSetting() {
const mx = useMatrixClient(); const mx = useMatrixClient();
const clientConfig = useClientConfig(); const clientConfig = useClientConfig();
const [userPushPreference, setUserPushPreference] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(true); const [isLoading, setIsLoading] = useState<boolean>(true);
const [usePushNotifications, setPushNotifications] = useSetting(
settingsAtom,
'usePushNotifications'
);
const pushSubAtom = useAtom(pushSubscriptionAtom);
const browserPermission = usePermissionState('notifications', getNotificationState()); const browserPermission = usePermissionState('notifications', getNotificationState());
useEffect(() => { useEffect(() => {
const storedPreference = localStorage.getItem('cinny_web_push_enabled');
setUserPushPreference(storedPreference === 'true');
setIsLoading(false); setIsLoading(false);
}, []); }, []);
const handleRequestPermissionAndEnable = async () => { const handleRequestPermissionAndEnable = async () => {
@ -106,9 +109,8 @@ function WebPushNotificationSetting() {
try { try {
const permissionResult = await requestBrowserNotificationPermission(); const permissionResult = await requestBrowserNotificationPermission();
if (permissionResult === 'granted') { if (permissionResult === 'granted') {
await enablePushNotifications(mx, clientConfig); await enablePushNotifications(mx, clientConfig, pushSubAtom);
localStorage.setItem('cinny_web_push_enabled', 'true'); setPushNotifications(true);
setUserPushPreference(true);
} }
} finally { } finally {
setIsLoading(false); setIsLoading(false);
@ -120,12 +122,11 @@ function WebPushNotificationSetting() {
try { try {
if (wantsPush) { if (wantsPush) {
await enablePushNotifications(mx, clientConfig); await enablePushNotifications(mx, clientConfig, pushSubAtom);
} else { } else {
await disablePushNotifications(mx, clientConfig); await disablePushNotifications(mx, clientConfig, pushSubAtom);
} }
localStorage.setItem('cinny_web_push_enabled', String(wantsPush)); setPushNotifications(wantsPush);
setUserPushPreference(wantsPush);
} finally { } finally {
setIsLoading(false); setIsLoading(false);
} }