Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { useContext, useEffect } from "react";
import { useHistory } from "react-router-dom";
import { ToastContext } from "@humansignal/ui";
 
export const DRAFT_GUARD_KEY = "DRAFT_GUARD";
 
export const draftGuardCallback = {
  current: null,
};
 
export const DraftGuard = () => {
  const toast = useContext(ToastContext);
  const history = useHistory();
 
  useEffect(() => {
    const unblock = () => {
      draftGuardCallback.current?.(true);
      draftGuardCallback.current = null;
    };
 
    /**
     * The version of Router History that is in use does not currently support
     * the `block` method fully. This is a workaround to allow us to block navigation
     * when there are unsaved changes. The draftGuardCallback allows the unblock callback to be captured from the
     * history callback `getUserConfirmation` that is triggered by returning a string message from history.block, allowing the user to
     * confirm they want to leave the page. Here we send through a constant message
     * to signify that we aren't looking for user confirmation but to utilize this to enable navigation blocking based on
     * unsuccessful draft saves.
     */
    const unsubscribe = history.block(() => {
      const selected = window.Htx?.annotationStore?.selected;
      const submissionInProgress = !!selected?.submissionStarted;
      const hasChanges = !!selected?.history.undoIdx && !submissionInProgress;
 
      if (hasChanges) {
        selected.saveDraftImmediatelyWithResults()?.then((res) => {
          const status = res?.$meta?.status;
 
          if (status === 200 || status === 201) {
            toast.show({ message: "Draft saved successfully", type: "info" });
            unblock();
          } else if (status !== undefined) {
            toast.show({ message: "There was an error saving your draft", type: "error" });
          } else {
            unblock();
          }
        });
 
        return DRAFT_GUARD_KEY;
      }
    });
 
    return () => {
      unblock();
      unsubscribe();
    };
  }, []);
 
  return <></>;
};