Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading