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

70.37% Statements 19/27
75% Branches 3/4
100% Functions 4/4
70.37% Lines 19/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                              10531x 10531x 10531x 10531x 10531x     10531x 250x 250x     10531x 2x     10531x       56x 56x   56x                 56x   56x 56x                               56x                       10531x  
import { useProjectId } from "@/hooks/project";
import { useState } from "react";
import { toast } from "sonner";
import {
  StatusEnum,
  useIdaesSolveCreateMutation
} from "../../../../api/apiStore.gen";
import { useSearchParam } from "../../../../hooks/searchParams";
 
import { useUpdateTaskCache } from "@/hooks/cache/useUpdateTaskCache.ts";
import { useTaskCompletedSubscription, useTaskCancelledSubscription } from "@/hooks/notifications/notificationSubscriptions.ts";
import { ContentTypes } from "../LeftSideBar/LeftSideBarTabDefinitions";
 
 
export function useSolve() {
  const [requestSolve] = useIdaesSolveCreateMutation();
  const updateTaskCache = useUpdateTaskCache();
  const [solveRunning, setSolveRunning] = useState(false);
  const flowsheetId = useProjectId();
  const [content, setContent] = useSearchParam("content");
 
  // 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);
  })
 
  const solve = async (
    scenarioNumber?: number,
    perform_diagnostics?: boolean,
  ) => {
    try {
      setSolveRunning(true);
 
      const response = await requestSolve({
        solveRequest: {
          flowsheet_id: flowsheetId,
          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 E{
        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) {
      setSolveRunning(false);
 
      toast.error("Solve could not be started", {
        description: "An unknown error occurred.",
      });
    }
  };
  return [solve, solveRunning] as const;
}