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

Conversation

arnautov-anton
Copy link
Contributor

@arnautov-anton arnautov-anton commented Jun 27, 2023

This PR adds "beforeunload" handler which prompts users upon website reload or leave to prevent unwanted call leaves.

Known limitations:

  • event.returnValue is deprecated, documentation suggests using event.preventDefault() which does not work - documentation also suggest using return value - which (surprise, surprise) also does not work (compatibility table)
  • does not respect navigation changes (NextJS router solutions work - but prompt you twice)

Read more: https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event#compatibility_notes

@MartinCupela
Copy link
Contributor

MartinCupela commented Jun 28, 2023

I tested this hook and it did not work. Looking into the code, we could add window.confirm(message) to already existing hook in MeetingUI.tsx:

  useEffect(() => {
    const handlePageLeave = async () => {
      if (!window.confirm('Call in progress, are you sure you want to leave?')) return;
      if (
        activeCall &&
        [CallingState.JOINING, CallingState.JOINED].includes(callState)
      ) {
        await activeCall.leave();
      }
    };
    router.events.on('routeChangeStart', handlePageLeave);
    return () => {
      router.events.off('routeChangeStart', handlePageLeave);
    };
  }, [activeCall, callState, router.events]);

Or better we could add window.confirm(message) to onClick callback passed to CancelCallButton in the same file:

  const onLeave = useCallback(async () => {
    if (!window.confirm('Call in progress, are you sure you want to leave?'))
      return;
    setShow('loading');
   await call.leave();
    try {
      await router.push('/');
    } catch (e) {
      console.error(e);
      setShow('error-leave');
    }
  }, [router, call]);

@arnautov-anton arnautov-anton force-pushed the feat/before-unload-handler branch from 7c83699 to a9fc50b Compare July 18, 2023 10:17
@arnautov-anton arnautov-anton force-pushed the feat/before-unload-handler branch from a9fc50b to ff7329e Compare July 18, 2023 10:20
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants