aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/components/hooks/useWindowLeave.ts
blob: ecd999d4631b37b025d99aaf7a68e24f37d38d4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { useEffect } from "react";

import { useC, Text } from "../common";

export default function useWindowLeave(
  allow: boolean,
  message: Text = "timeline.confirmLeave",
) {
  const c = useC();

  useEffect(() => {
    if (!allow) {
      window.onbeforeunload = () => {
        return c(message);
      };

      return () => {
        window.onbeforeunload = null;
      };
    }
  }, [c, allow, message]);
}