Skip to content

Commit

Permalink
feat: add page exit confirmation for active chats (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
tasosbeast committed Jan 30, 2025
1 parent a211ca3 commit 02a9799
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions client/src/components/BuddyMatcher.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ const BuddyMatcher = () => {
endSearch();
}

const handleBeforeUnload = (event) => {
const message = 'You have an active chat. Are you sure you want to leave?';
event.preventDefault();
event.returnValue = message;
return message; // This is required for modern browsers
};

useEffect(() => {
window.addEventListener('beforeunload', handleBeforeUnload);

// Clean up event listener on component unmount
return () => {
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}, []); // Empty dependency array since handleBeforeUnload doesn't depend on props/state

useEffect(() => {
// Add event listener for beforeunload
window.addEventListener('beforeunload', handleBeforeUnload);

return () => {
// Cleanup event listener
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}, []);

async function handleReconnect() {
if (socket.connected) {
return;
Expand Down

0 comments on commit 02a9799

Please sign in to comment.