Page MenuHomePhabricator

D6819.id22921.diff
No OneTemporary

D6819.id22921.diff

diff --git a/web/push-notif/push-notifs-handler.js b/web/push-notif/push-notifs-handler.js
--- a/web/push-notif/push-notifs-handler.js
+++ b/web/push-notif/push-notifs-handler.js
@@ -2,7 +2,45 @@
import * as React from 'react';
+import {
+ setDeviceToken,
+ setDeviceTokenActionTypes,
+} from 'lib/actions/device-actions.js';
+import {
+ useDispatchActionPromise,
+ useServerCall,
+} from 'lib/utils/action-utils.js';
+
import electron from '../electron.js';
+import { useSelector } from '../redux/redux-utils.js';
+
+function useCreatePushSubscription(): () => Promise<void> {
+ const publicKey = useSelector(state => state.pushApiPublicKey);
+
+ const dispatchActionPromise = useDispatchActionPromise();
+ const callSetDeviceToken = useServerCall(setDeviceToken);
+
+ return React.useCallback(async () => {
+ if (!publicKey) {
+ return;
+ }
+
+ const workerRegistration = await navigator.serviceWorker?.ready;
+ if (!workerRegistration) {
+ return;
+ }
+
+ const subscription = await workerRegistration.pushManager.subscribe({
+ userVisibleOnly: true,
+ applicationServerKey: publicKey,
+ });
+
+ dispatchActionPromise(
+ setDeviceTokenActionTypes,
+ callSetDeviceToken(JSON.stringify(subscription)),
+ );
+ }, [callSetDeviceToken, dispatchActionPromise, publicKey]);
+}
function PushNotificationsHandler(): React.Node {
React.useEffect(() => {
@@ -19,4 +57,4 @@
return null;
}
-export { PushNotificationsHandler };
+export { PushNotificationsHandler, useCreatePushSubscription };

File Metadata

Mime Type
text/plain
Expires
Sun, Dec 22, 5:47 PM (17 h, 16 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2692027
Default Alt Text
D6819.id22921.diff (1 KB)

Event Timeline