All files / src/pages/flowsheet-page/flowsheet/revisions useRestoreFlowsheetRevision.ts

76.92% Statements 20/26
33.33% Branches 2/6
0% Functions 0/1
83.33% Lines 20/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83                                                      1384x 1384x   1384x 1384x 1384x 1384x         1x 1x 1x   1x 1x 1x       1x 1x 1x 1x                       1x                 1x 1x       1384x          
import { useRef, useState } from "react";
import { toast } from "sonner";
import {
  api,
  type FlowsheetRevisionRead,
  RestoreFlowsheetRevisionWarningCodeEnum,
  useCoreFlowsheetsRevisionsRestoreCreateMutation,
} from "@/api/apiStore.gen";
import {
  useClearStateLocalSearchParams,
  useFlowsheetRevisionRefresh,
} from "@/hooks/cache/useFlowsheetRevisionRefresh";
import { store } from "@/store/store";
import {
  ACTIVE_TASKS_REVISION_MESSAGE,
  isActiveTasksRevisionConflict,
  useFlowsheetRevisionSave,
} from "./FlowsheetRevisionSaveContext";
 
/** Restore one revision through the shared task guard and cache cutover. */
export function useRestoreFlowsheetRevision({
  flowsheetId,
  onRestored,
}: {
  flowsheetId: number;
  onRestored?: () => void;
}) {
  const pendingRef = useRef(false);
  const [pending, setPending] = useState(false);
  const [restoreMutation, restoreResult] =
    useCoreFlowsheetsRevisionsRestoreCreateMutation();
  const clearStateLocalSearchParams = useClearStateLocalSearchParams();
  const { refreshAfterRestore } = useFlowsheetRevisionRefresh();
  const { ensureNoActiveTasks } = useFlowsheetRevisionSave();
 
  const restoreRevision = async (
    revision: FlowsheetRevisionRead,
  ): Promise<boolean> => {
    Iif (pendingRef.current) return false;
    pendingRef.current = true;
    setPending(true);
    try {
      await Promise.all(store.dispatch(api.util.getRunningMutationsThunk()));
      Iif (!(await ensureNoActiveTasks())) return false;
      const response = await restoreMutation({
        id: String(flowsheetId),
        stateId: String(revision.state_id),
      }).unwrap();
      clearStateLocalSearchParams();
      refreshAfterRestore();
      onRestored?.();
      toast.success("Version restored");
      if (
        response.warnings.some(
          (warning) =>
            warning.code ===
            RestoreFlowsheetRevisionWarningCodeEnum.EconomicsReferenceUnavailable,
        )
      ) {
        toast.warning(
          "Some economics comparison or baseline selections were cleared because the referenced studies are no longer available.",
        );
      }
      return true;
    } catch (error) {
      toast.error(
        isActiveTasksRevisionConflict(error)
          ? ACTIVE_TASKS_REVISION_MESSAGE
          : "Version could not be restored",
      );
      return false;
    } finally {
      pendingRef.current = false;
      setPending(false);
    }
  };
 
  return {
    restoreRevision,
    isRestoring: pending || restoreResult.isLoading,
  };
}