Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react-dogfood): add "beforeunload" event handler #722

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion sample-apps/react/react-dogfood/components/MeetingUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ import {
UnreadCountBadge,
} from '.';
import { ActiveCallHeader } from './ActiveCallHeader';
import { useKeyboardShortcuts, useWatchChannel } from '../hooks';
import {
useBeforeUnload,
useKeyboardShortcuts,
useWatchChannel,
} from '../hooks';
import { DEFAULT_LAYOUT, getLayoutSettings, LayoutMap } from './LayoutSelector';
import { Stage } from './Stage';
import { ToggleParticipantListButton } from './ToggleParticipantListButton';
Expand Down Expand Up @@ -107,6 +111,11 @@ export const MeetingUI = ({ chatClient, enablePreview }: MeetingUIProps) => {
}
}, [router]);

useBeforeUnload(
callState === CallingState.JOINED,
'Call in progress, are you sure you want to leave?',
);

useEffect(() => {
if (callState === CallingState.LEFT) {
void onLeave();
Expand Down
1 change: 1 addition & 0 deletions sample-apps/react/react-dogfood/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useChatClient';
export * from './useWatchChannel';
export * from './useKeyboardShortcuts';
export * from './useBeforeUnload';
18 changes: 18 additions & 0 deletions sample-apps/react/react-dogfood/hooks/useBeforeUnload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useEffect } from 'react';

export const useBeforeUnload = (enabled: boolean, message: string) => {
useEffect(() => {
if (!enabled) return;

const handleBeforeUnload = (e: Event) => {
e.preventDefault(); // <- this does not work even though it's preffered way

// window.confirm does not work to display custom message
// @ts-expect-error
return (e.returnValue = message);
};

window.addEventListener('beforeunload', handleBeforeUnload);
return () => window.removeEventListener('beforeunload', handleBeforeUnload);
}, [enabled, message]);
};