All files / src/hooks/notifications useFlowsheetSubscriptions.ts

100% Statements 22/22
100% Branches 8/8
100% Functions 3/3
100% Lines 22/22

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                            5912x 5912x 5912x 5912x   5912x 131x     131x 61x 60x 1x 1x     61x 60x   1x 1x   1x             5912x 2x     2x 2x 2x             5912x    
import { toast } from "sonner";
import { StatusEnum, TaskTypeEnum } from "@/api/apiStore.gen.ts";
import { useMLTrainRefresh } from "@/hooks/cache/useMLTrainRefresh.ts";
import { useSolveRefresh } from "@/hooks/cache/useSolveRefresh.ts";
import { useUpdateTaskCache } from "@/hooks/cache/useUpdateTaskCache.ts";
import {
  useTaskCancelledSubscription,
  useTaskCompletedSubscription,
  useTaskUpdatedSubscription,
} from "@/hooks/notifications/notificationSubscriptions.ts";
import { ContentTypes } from "../../pages/flowsheet-page/flowsheet/LeftSideBar/LeftSideBarTabDefinitions";
import { useSearchParam } from "../searchParams";
 
export function useFlowsheetSubscriptions() {
  const { refreshSolveDependencies } = useSolveRefresh();
  const { refreshMLTrainDependencies } = useMLTrainRefresh();
  const updateTaskCache = useUpdateTaskCache();
  const [, setContent] = useSearchParam("content");
 
  useTaskCompletedSubscription((completedTask) => {
    updateTaskCache(completedTask);
 
    // Process completion for non-child tasks
    if (completedTask.parent == null) {
      if (completedTask.task_type == TaskTypeEnum.Solve) {
        refreshSolveDependencies(completedTask.id);
      } else if (completedTask.task_type == TaskTypeEnum.MlTraining) {
        refreshMLTrainDependencies();
      }
 
      if (completedTask.status == StatusEnum.Completed)
        toast.success(`${completedTask.task_type} completed successfully`);
      else {
        if (completedTask.task_type == TaskTypeEnum.Solve) {
          setContent(ContentTypes.solverLogs); // so the user can see the logs immediately
        }
        toast.error(`${completedTask.task_type} failed`, {
          description: `The ${completedTask.task_type} task did not complete successfully. See the logs for more details.`,
        });
      }
    }
  });
 
  useTaskCancelledSubscription((cancelledTask) => {
    updateTaskCache(cancelledTask);
 
    // Process completion for non-child tasks
    if (cancelledTask.parent == null) {
      refreshSolveDependencies(cancelledTask.id);
      toast.error("Solve cancelled", {
        description:
          "The solve task is cancelled. See the logs for more details.",
      });
    }
  });
 
  useTaskUpdatedSubscription(updateTaskCache);
}