All files / src/hooks/notifications useFlowsheetSubscriptions.ts

87.8% Statements 36/41
40% Branches 2/5
80% Functions 4/5
92.1% Lines 35/38

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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134                                                      2x       2x             8906x 8906x 8906x 8906x 8906x 8906x   8906x 182x   182x         64x   1x       70x     2x   2x             8906x 3x   3x       3x       1x     1x     1x       2x               8906x 2x   2x       2x 2x                   8906x       8906x   8906x   21x 21x     2x      
import { toast } from "sonner";
import { StatusEnum, TaskTypeEnum } from "@/api/apiStore.gen.ts";
import { useBuildStateRefresh } from "@/hooks/cache/useBuildStateRefresh.ts";
import { useInvalidateChildTaskCache } from "@/hooks/cache/useInvalidateChildTaskCache.ts";
import { useMLTrainRefresh } from "@/hooks/cache/useMLTrainRefresh.ts";
import { useSolveRefresh } from "@/hooks/cache/useSolveRefresh.ts";
import { useUpdateTaskCache } from "@/hooks/cache/useUpdateTaskCache.ts";
import type { BuildStateCompletionNotification } from "@/hooks/notifications/notificationSubscriptions.ts";
import {
  useBuildStateCompletedSubscription,
  useTaskCancelledSubscription,
  useTaskCancellingSubscription,
  useTaskChildrenCancelledSubscription,
  useTaskCompletedSubscription,
  useTaskUpdatedSubscription,
} from "@/hooks/notifications/notificationSubscriptions.ts";
import { ContentTypes } from "../../pages/flowsheet-page/flowsheet/LeftSideBar/LeftSideBarTabDefinitions";
import { useSearchParam } from "../searchParams";
 
function handleBuildStateFailure(completion: BuildStateCompletionNotification) {
  if (completion.failure_kind === "delivery-failure") {
    console.warn("Build-state request delivery failed.", {
      context: completion.context,
    });
    return;
  }
 
  console.warn("Related property calculation failed.", {
    context: completion.context,
    error: completion.error,
  });
  toast.error("Related properties could not be updated", {
    description:
      "The stream property update was saved, but some related properties could not be calculated.",
  });
}
 
export function useFlowsheetSubscriptions() {
  const { refreshSolveDependencies } = useSolveRefresh();
  const { refreshBuildStateDependencies } = useBuildStateRefresh();
  const { refreshMLTrainDependencies } = useMLTrainRefresh();
  const updateTaskCache = useUpdateTaskCache();
  const invalidateChildTaskCache = useInvalidateChildTaskCache();
  const [, setContent] = useSearchParam("content");
 
  useTaskCompletedSubscription((completedTask) => {
    updateTaskCache(completedTask);
 
    if (completedTask.task_type === TaskTypeEnum.BuildState) return;
 
    // 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);
 
    Iif (cancelledTask.task_type === TaskTypeEnum.BuildState) return;
 
    // Process completion for non-child tasks
    if (cancelledTask.parent == null) {
      refreshSolveDependencies(cancelledTask.id);
      if (cancelledTask.debug?.timed_out) {
        // Timeout metadata is persisted on the task debug payload by Django,
        // so users see the exact configured timeout value in the toast.
        const timeoutSeconds = Number(
          cancelledTask.debug?.solve_timeout_seconds,
        );
        const timeoutDescription = Number.isFinite(timeoutSeconds)
          ? `The solve exceeded its timeout of ${timeoutSeconds} seconds and was cancelled. See the logs for more details.`
          : "The solve exceeded its timeout and was cancelled. See the logs for more details.";
        toast.error("Solve timed out", {
          description: timeoutDescription,
        });
      } else {
        toast.error("Solve cancelled", {
          description:
            "The solve task is cancelled. See the logs for more details.",
        });
      }
    }
  });
 
  useTaskCancellingSubscription((cancelledTask) => {
    updateTaskCache(cancelledTask);
 
    Iif (cancelledTask.task_type === TaskTypeEnum.BuildState) return;
 
    // Process completion for non-child tasks
    if (cancelledTask.parent == null) {
      refreshSolveDependencies(cancelledTask.id);
      toast.info("Solve cancelling", {
        description:
          "The solve task is cancelling. See the logs for more details.",
      });
    }
  });
 
  // When a parent task's children are cancelled en-masse, drop all locally
  // cached child-task pages so they are refetched on demand rather than
  // processing individual updates for potentially thousands of children.
  useTaskChildrenCancelledSubscription((parentTask) => {
    invalidateChildTaskCache(parentTask.id);
  });
 
  useTaskUpdatedSubscription(updateTaskCache);
 
  useBuildStateCompletedSubscription((completion) => {
    if (completion.status === "success") {
      refreshBuildStateDependencies();
      return;
    }
 
    handleBuildStateFailure(completion);
  });
}