Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3512206
D6833.id23242.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D6833.id23242.diff
View Options
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
@@ -6,12 +6,15 @@
setDeviceToken,
setDeviceTokenActionTypes,
} from 'lib/actions/device-actions.js';
+import { useModalContext } from 'lib/components/modal-provider.react.js';
+import { isLoggedIn } from 'lib/selectors/user-selectors.js';
import {
useDispatchActionPromise,
useServerCall,
} from 'lib/utils/action-utils.js';
import electron from '../electron.js';
+import PushNotifModal from '../modals/push-notif-modal.react.js';
import { useSelector } from '../redux/redux-utils.js';
function useCreatePushSubscription(): () => Promise<void> {
@@ -43,6 +46,11 @@
}
function PushNotificationsHandler(): React.Node {
+ const createPushSubscription = useCreatePushSubscription();
+
+ const modalContext = useModalContext();
+ const loggedIn = useSelector(isLoggedIn);
+
React.useEffect(() => {
(async () => {
if (!navigator.serviceWorker || electron) {
@@ -50,9 +58,35 @@
}
await navigator.serviceWorker.register('/worker/notif', { scope: '/' });
+
+ if (Notification.permission === 'granted') {
+ // Make sure the subscription is current if we have the permissions
+ await createPushSubscription();
+ } else if (Notification.permission === 'default' && loggedIn) {
+ // Ask existing users that are already logged in for permission
+ modalContext.pushModal(<PushNotifModal />);
+ }
})();
+ // eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
+ // Ask for permission on login
+ const prevLoggedIn = React.useRef(loggedIn);
+ React.useEffect(() => {
+ if (!navigator.serviceWorker || electron) {
+ return;
+ }
+
+ if (!prevLoggedIn.current && loggedIn) {
+ if (Notification.permission === 'granted') {
+ createPushSubscription();
+ } else if (Notification.permission === 'default') {
+ modalContext.pushModal(<PushNotifModal />);
+ }
+ }
+ prevLoggedIn.current = loggedIn;
+ }, [createPushSubscription, loggedIn, modalContext, prevLoggedIn]);
+
return null;
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 22, 6:51 PM (18 h, 41 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2690698
Default Alt Text
D6833.id23242.diff (2 KB)
Attached To
Mode
D6833: [web] Allow users to enable push notifs
Attached
Detach File
Event Timeline
Log In to Comment