Skip to content

Commit

Permalink
feat: add a dialog if User denies notfication permission (#737)
Browse files Browse the repository at this point in the history
Co-authored-by: Dunsin <78784850+Dun-sin@users.noreply.github.com>
  • Loading branch information
AdarshKumar02291 and Dun-sin authored Dec 12, 2024
1 parent f5a1d0b commit 1210997
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
9 changes: 7 additions & 2 deletions client/src/lib/browserNotification.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import BadWordsNext from 'bad-words-next';
import en from 'bad-words-next/data/en.json';

export const requestBrowserNotificationPermissions = () => {
export const requestBrowserNotificationPermissions = async () => {
if (!('Notification' in window)) {
console.log('Browser does not support desktop notification');
} else {
Notification.requestPermission();
const res = await Notification.requestPermission();
if (res === 'granted') {
return true;
} else {
return false;
}
}
};

Expand Down
34 changes: 32 additions & 2 deletions client/src/pages/Start.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,50 @@ import { requestBrowserNotificationPermissions } from 'src/lib/browserNotificati
import { useApp } from 'src/context/AppContext';
import useCloseChat from 'src/hooks/useCloseChat';
import { useEffect } from 'react';
import { useDialog } from 'src/context/DialogContext';

const centerElement = 'flex flex-col items-center justify-center';

const Start = () => {
const { app } = useApp();
const navigate = useNavigate();
const { handleClose } = useCloseChat();
const { setDialog } = useDialog();

const requestPermission = async () => {
try {
const isGranted = await requestBrowserNotificationPermissions();
if (!isGranted) {
setDialog({
isOpen: true,
text: "You've blocked Whisper notifications. Enable them to stay updated, keep track of conversations, and never miss important messages.",
handler: async (response) => {
if (response) {
await requestBrowserNotificationPermissions();
setDialog({ isOpen: false });
} else {
setDialog({ isOpen: false });
}
},
noBtnText: 'No',
yesBtnText: 'Try Again',
});
}

return isGranted;
} catch (error) {
console.error('Error requesting notification permission:', error);
return false;
}
};
useEffect(() => {
if (app.isSearching) {
navigate('/searching');
}

requestBrowserNotificationPermissions();
const checkPermission = async () => {
await requestPermission();
};
checkPermission();
}, []);

return (
Expand Down

0 comments on commit 1210997

Please sign in to comment.