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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 7x 7x 3x 7x 6x 6x 5x 5x 1x 3x 3x 2x 2x 2x 2x 2x 1x 2x 11x 11x 11x 1x 1x 1x 1x 3x 1x 1x 1x 11x 11x 11x 11x 11x | import { listNotificationsQueryKey } from "features/queryKeys"; import Sentry from "platform/sentry"; import { ListNotificationsRes } from "proto/notifications_pb"; import { useMutation, useQueryClient } from "react-query"; import { service } from "service"; import { getVapidPublicKey, registerPushNotificationSubscription, } from "service/notifications"; import { arrayBufferToBase64 } from "utils/arrayBufferToBase64"; interface PushNotificationPermissionSuccessResponse { success: true; } interface PushNotificationPermissionErrorResponse { success: false; errorMessage: string; } type PushNotificationPermissionResponse = | PushNotificationPermissionSuccessResponse | PushNotificationPermissionErrorResponse; export const onPushNotificationPermissionGranted = async (): Promise<PushNotificationPermissionResponse> => { 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 { success: true }; } } 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); return { success: true }; } else E{ Sentry.captureException( new Error("Push notifications or service workers not supported"), { tags: { component: "PushNotificationPermission", action: "onPermissionGranted", userAgent: navigator.userAgent, }, }, ); return { success: false, errorMessage: "notifications:notification_settings.push_notifications.error_unsupported", }; } } catch (error) { console.error("Error subscribing to push notifications", error); Sentry.captureException(error, { tags: { component: "PushNotificationPermission", action: "onPermissionGranted", }, }); return { success: false, errorMessage: "notifications:notification_settings.push_notifications.error_generic", }; } }; export const getCurrentSubscription = async () => { let registration = await navigator.serviceWorker.getRegistration(); if (!registration) { registration = await navigator.serviceWorker.register( "/service-worker.js", { scope: "/", }, ); } return registration?.pushManager.getSubscription(); }; export const checkPushEnabled = async () => { if ("serviceWorker" in navigator && "PushManager" in window) { const existingPushSubscription = await getCurrentSubscription(); return ( Notification.permission === "granted" && existingPushSubscription !== null ); } else { throw new Error("Push notifications or service workers not supported"); } }; export const turnPushNotificationsOn = async ( setShouldPromptAllow: (on: boolean) => void, ): Promise<PushNotificationPermissionResponse> => { if (Notification.permission !== "denied") { setShouldPromptAllow(true); const result = await Notification.requestPermission(); setShouldPromptAllow(false); if (result === "granted") { return await onPushNotificationPermissionGranted(); } } return { success: false, errorMessage: "notifications:notification_settings.push_notifications.error_not_granted", }; }; export const turnPushNotificationsOff = async () => { const existingPushSubscription = await getCurrentSubscription(); Iif (existingPushSubscription) { await existingPushSubscription.unsubscribe(); return true; } return false; }; export const useMarkAllNotificationsSeen = () => { const queryClient = useQueryClient(); const { error, mutate, isLoading } = useMutation({ mutationFn: async ({ latestNotificationId, }: { latestNotificationId: number; }) => await service.notifications.markAllNotificationsSeen( latestNotificationId, ), onMutate: () => { queryClient.cancelQueries(listNotificationsQueryKey); const previousData = queryClient.getQueryData<ListNotificationsRes.AsObject>( listNotificationsQueryKey, ); const newData: ListNotificationsRes.AsObject = { ...previousData, notificationsList: previousData?.notificationsList ? previousData.notificationsList.map((notification) => ({ ...notification, isSeen: true, })) : [], nextPageToken: previousData?.nextPageToken ?? "", }; if (previousData) { queryClient.setQueryData<ListNotificationsRes.AsObject>( listNotificationsQueryKey, newData, ); } return { previousData }; }, onError: (error) => { Sentry.captureException(error, { tags: { component: "useMarkAllNotificationsSeen", action: "onMutate", }, }); }, }); return { error, markAllNotificationsSeenMutation: mutate, isLoading }; }; export const useMarkSingleNotificationIsSeen = () => { const queryClient = useQueryClient(); const { error, mutate, isLoading } = useMutation({ mutationFn: async ({ notificationId, isSeen, }: { notificationId: number; isSeen: boolean; }) => await service.notifications.markNotificationSeen(notificationId, isSeen), onMutate: ({ notificationId, isSeen }) => { queryClient.cancelQueries(listNotificationsQueryKey); const previousData = queryClient.getQueryData<ListNotificationsRes.AsObject>( listNotificationsQueryKey, ); const newData: ListNotificationsRes.AsObject = { ...previousData, notificationsList: previousData?.notificationsList ? previousData.notificationsList.map((notification) => notification.notificationId === notificationId ? { ...notification, isSeen: isSeen } : notification, ) : [], nextPageToken: previousData?.nextPageToken ?? "", }; Iif (previousData) { queryClient.setQueryData<ListNotificationsRes.AsObject>( listNotificationsQueryKey, newData, ); } return { previousData }; }, onError: (error) => { Sentry.captureException(error, { tags: { component: "useMarkSingleNotificationSeen", action: "onMutate", }, }); }, }); return { error, markSingleNotificationIsSeenMutation: mutate, isLoading }; }; |