aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/components/hooks/useWindowLeave.ts
blob: b829a92fa42f1119ca501ed546e532b9a515e197 (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, I18nText } from "../common";

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

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

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