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

44.44% Statements 4/9
12.5% Branches 1/8
100% Functions 1/1
44.44% Lines 4/9

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                  103x                                 103x       2525x         2525x      
import { createContext, useContext } from "react";
 
export type FlowsheetRevisionSaveContextValue = {
  ensureNoActiveTasks: () => Promise<boolean>;
  isSaving: boolean;
  saveRevision: () => Promise<boolean>;
};
 
export const ACTIVE_TASKS_REVISION_MESSAGE =
  "Let active tasks finish or cancel them before saving or restoring a version.";
 
/** Identify the backend conflict returned when task activity changes after preflight. */
export function isActiveTasksRevisionConflict(error: unknown): boolean {
  if (typeof error !== "object" || error === null || !("data" in error)) {
    return false;
  }
  const data = error.data;
  return (
    typeof data === "object" &&
    data !== null &&
    "code" in data &&
    data.code === "active_tasks"
  );
}
 
export const FlowsheetRevisionSaveContext =
  createContext<FlowsheetRevisionSaveContextValue | null>(null);
 
/** Access the page-owned revision-save command and its shared pending state. */
export function useFlowsheetRevisionSave() {
  const value = useContext(FlowsheetRevisionSaveContext);
  if (value === null) {
    throw new Error(
      "useFlowsheetRevisionSave must be used inside FlowsheetRevisionSaveProvider.",
    );
  }
  return value;
}