Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 12x 12x 12x 12x 12x 12x 12x 12x 6x 6x 5x 5x 1x 1x 6x 6x 12x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 12x 3x 2x 2x 2x 2x 2x 2x 2x 2x 1x 12x | import { Typography } from "@mui/material"; import Alert from "components/Alert"; import CustomColorSwitch from "components/CustomColorSwitch"; import { Trans, useTranslation } from "i18n"; import { AUTH } from "i18n/namespaces"; import Sentry from "platform/sentry"; import { useEffect, useState } from "react"; import { getVapidPublicKey, registerPushNotificationSubscription, } from "service/notifications"; import { theme } from "theme"; import { arrayBufferToBase64 } from "utils/arrayBufferToBase64"; import makeStyles from "utils/makeStyles"; import { getCurrentSubscription } from "./notificationUtils"; import PushNotificationDenied from "./PushNotificationDenied"; const useStyles = makeStyles((theme) => ({ alert: { marginBottom: theme.spacing(3), marginTop: theme.spacing(2), }, status: { marginBottom: theme.spacing(2), }, titleBox: { display: "flex", alignItems: "center", }, })); export default function PushNotificationSettings({ className, }: { className: string; }) { const { t } = useTranslation(AUTH); const classes = useStyles(); const isNotificationSupported = typeof Notification !== "undefined"; const [errorMessage, setErrorMessage] = useState<string | null>(null); const [isPushEnabled, setIsPushEnabled] = useState<boolean>(false); const [isLoading, setIsLoading] = useState<boolean>(true); const [shouldPromptAllow, setShouldPromptAllow] = useState<boolean>(false); // whether to show the user instructions to click 'Allow' in their browser useEffect(() => { const checkPushEnabled = async () => { if ("serviceWorker" in navigator && "PushManager" in window) { const existingPushSubscription = await getCurrentSubscription(); setIsPushEnabled( Notification.permission === "granted" && existingPushSubscription !== null ); } else { setErrorMessage( t("notification_settings.push_notifications.error_unsupported") ); Sentry.captureException( new Error("Push notifications or service workers not supported"), { tags: { component: "PushNotificationPermission", action: "onPermissionGranted", userAgent: navigator.userAgent, }, } ); } setIsLoading(false); }; checkPushEnabled(); }, [t]); const onPermissionGranted = async () => { try { // Check if service workers and push notifications are supported if ("serviceWorker" in navigator && "PushManager" in window) { const existingPushSubscription = await getCurrentSubscription(); const p256dhKey = existingPushSubscription?.getKey("p256dh"); const { vapidPublicKey } = await getVapidPublicKey(); if (existingPushSubscription && p256dhKey) { const publicKey = arrayBufferToBase64(p256dhKey); /** * The purpose of this check is to ensure that the push subscription is correctly authenticated with the server’s VAPID key. * If the client’s p256dh key no longer matches the server’s vapidPublicKey, then the subscription is unsubscribed and needs * to be re-registered to ensure the security and validity of the Web Push connection. */ if (publicKey !== vapidPublicKey) { await existingPushSubscription.unsubscribe(); } else E{ return; } } const registration = await navigator.serviceWorker.getRegistration(); // Subscribe to push notifications via the PushManager const subscription: PushSubscription = await registration!.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: vapidPublicKey, }); await registerPushNotificationSubscription(subscription); } else E{ setErrorMessage( t("notification_settings.push_notifications.error_unsupported") ); Sentry.captureException( new Error("Push notifications or service workers not supported"), { tags: { component: "PushNotificationPermission", action: "onPermissionGranted", userAgent: navigator.userAgent, }, } ); } } catch (error) { console.error("Error subscribing to push notifications", error); setErrorMessage( t("notification_settings.push_notifications.error_generic") ); Sentry.captureException(error, { tags: { component: "PushNotificationPermission", action: "onPermissionGranted", }, }); } }; const turnPushNotificationsOn = async () => { if (Notification.permission !== "denied") { setIsLoading(true); setShouldPromptAllow(true); const result = await Notification.requestPermission(); setShouldPromptAllow(false); if (result === "granted") { await onPermissionGranted(); setIsPushEnabled(true); } else E{ setIsPushEnabled(false); } setIsLoading(false); } else { setIsPushEnabled(false); } }; const turnPushNotificationsOff = async () => { setIsLoading(true); const existingPushSubscription = await getCurrentSubscription(); Iif (existingPushSubscription) { await existingPushSubscription.unsubscribe(); setIsPushEnabled(false); } setIsLoading(false); }; return ( <div className={className}> <div className={classes.titleBox}> <Typography variant="h2"> {t("notification_settings.push_notifications.title")} </Typography> <CustomColorSwitch checked={isPushEnabled} onClick={ isPushEnabled ? turnPushNotificationsOff : turnPushNotificationsOn } customColor={theme.palette.primary.main} isLoading={isLoading} /> </div> {errorMessage && ( <Alert className={classes.alert} severity="error"> {errorMessage || t("notification_settings.push_notifications.error_generic")} </Alert> )} {shouldPromptAllow && ( <Alert severity="info"> {t("notification_settings.push_notifications.allow_push")} </Alert> )} {isNotificationSupported && Notification.permission === "denied" && ( <PushNotificationDenied /> )} <Typography variant="body1" className={classes.status}> {isPushEnabled ? ( <Trans i18nKey="auth:notification_settings.push_notifications.enabled_message"> You currently have push notifications <strong>enabled</strong>. </Trans> ) : ( <Trans i18nKey="auth:notification_settings.push_notifications.disabled_message"> You currently have push notifications <strong>disabled</strong>. </Trans> )} </Typography> <Typography variant="body1"> {t("notification_settings.push_notifications.description")} </Typography> </div> ); } |