All files / src/pages/flowsheet-page/flowsheet/Solving useSolve.tsx

77.77% Statements 21/27
100% Branches 2/2
100% Functions 3/3
77.77% Lines 21/27

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 84 85 86 87 88 89 90 91 92 93 94 95 96                                    23834x 23834x 23834x 23834x 23834x     23834x 356x 356x     23834x 6x     23834x 4x                             70x   70x                 68x           67x       1x 1x                     67x         1x         23834x    
import { FetchBaseQueryError } from "@reduxjs/toolkit/query/react";
import { useState } from "react";
import { toast } from "sonner";
import { useUpdateTaskCache } from "@/hooks/cache/useUpdateTaskCache.ts";
import { useFlowsheetAccess } from "@/hooks/flowsheetAccess";
import { useCurrentGroupId } from "@/hooks/flowsheetObjects";
import {
  useTaskCancelledSubscription,
  useTaskCancellingSubscription,
  useTaskCompletedSubscription,
} from "@/hooks/notifications/notificationSubscriptions.ts";
import {
  StatusEnum,
  useIdaesSolveCreateMutation,
} from "../../../../api/apiStore.gen";
import { ContentTypes } from "../LeftSideBar/LeftSideBarTabDefinitions";
 
export function useSolve() {
  const [requestSolve] = useIdaesSolveCreateMutation();
  const updateTaskCache = useUpdateTaskCache();
  const [solveRunning, setSolveRunning] = useState(false);
  const groupId = useCurrentGroupId();
  const access = useFlowsheetAccess();
 
  // Subscribe to notifications of solve task completion
  useTaskCompletedSubscription((message) => {
    setSolveRunning(false);
    console.log(message); // This is helpful for debugging why the playwright tests are failing to solve, so I recommend leaving it in.
  });
 
  useTaskCancelledSubscription(() => {
    setSolveRunning(false);
  });
 
  useTaskCancellingSubscription(() => {
    setSolveRunning(false);
  });
 
  const solve = async (
    scenarioNumber?: number,
    perform_diagnostics?: boolean,
  ) => {
    if (access?.can_edit === false) {
      toast.warning("This flowsheet is read-only", {
        description: "Make a copy to edit or solve this flowsheet.",
      });
      return;
    }
 
    try {
      setSolveRunning(true);
 
      const response = await requestSolve({
        solveRequest: {
          group_id: groupId,
          perform_diagnostics,
          scenario_number: scenarioNumber,
          // is_rating_mode: ratingMode
        },
      }).unwrap();
 
      updateTaskCache(response);
 
      if (
        response.status === StatusEnum.Pending ||
        response.status === StatusEnum.Running
      )
        toast.success("Solve started successfully", {
          description: "A flowsheet solve is in progress.",
        });
      else {
        setSolveRunning(false);
        setContent(ContentTypes.solverLogs); // so the user can see the logs immediately
 
        const error = response.error;
        console.error("Error solving with idaes.");
        console.error(error);
 
        toast.error("Solve could not be started", {
          description: `${error.cause}: ${error.message}`,
        });
      }
 
      return response;
 
      // Known errors are handled at the task processing layer,
      // but we still need to catch unknown errors here
    } catch (error: unknown) {
      toast.error("Solve could not be started", {
        description: (error as FetchBaseQueryError)?.data as string,
      });
    }
  };
  return [solve, solveRunning] as const;
}