All files / src/pages/flowsheet-page/economics/page EconomicsPage.tsx

77.04% Statements 47/61
66.17% Branches 45/68
33.33% Functions 3/9
85.36% Lines 35/41

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                                    238x 238x       238x 238x 238x 238x 238x 238x 253x 251x     291x   238x   238x       427x 238x             238x   10x   2x       394x     225x         238x 238x   251x   5x 3x 418x   5x 5x 630x 434x   9x 9x 688x                         598x 3163x          
import { useNavigate } from "react-router-dom";
import {
  api,
  type EconomicsStudyRead,
  useEconomicsStudiesProjectListQuery,
} from "@/api/apiStore.gen";
import { useFlowsheetAccess } from "@/hooks/flowsheetAccess";
import { useFlowsheetId } from "@/hooks/project";
import { useSearchParam } from "@/hooks/searchParams";
import { useAppDispatch } from "@/store/hooks";
import { ECONOMICS_STUDY_PARAM } from "../navigation/economicsNavigation";
import { EconomicsShell } from "./EconomicsShell";
import {
  type EconomicsLoadState,
  queryErrorStatus,
  selectedEconomicsStudyId,
} from "./model";
 
export default function EconomicsPage({
  isRevisionPreview = false,
}: {
  isRevisionPreview?: boolean;
}) {
  const flowsheetId = useFlowsheetId();
  const navigate = useNavigate();
  const access = useFlowsheetAccess();
  const dispatch = useAppDispatch();
  const [studyParam, setStudyParam] = useSearchParam(ECONOMICS_STUDY_PARAM);
  const studiesQuery = useEconomicsStudiesProjectListQuery(
    { flowsheet: flowsheetId },
    { skip: !flowsheetId },
  );
 
  const studies = studiesQuery.currentData ?? [];
  const studiesLoading =
    studiesQuery.isLoading ||
    (studiesQuery.isFetching && studiesQuery.currentData == null);
  const selectedStudyId = selectedEconomicsStudyId(
    studies,
    studyParam,
    flowsheetId,
  );
  const state: EconomicsLoadState = studiesLoading
    ? "loading"
    : studiesQuery.isError
      ? "error"
      : studies.length === 0
        ? "empty"
        : "ready";
  const selectStudy = (study: EconomicsStudyRead) => {
    if (study.flowsheet === flowsheetId) {
      setStudyParam(String(study.id));
    } else {
      navigate(
        `/project/${study.flowsheet}/flowsheet?content=economics&${ECONOMICS_STUDY_PARAM}=${study.id}`,
      );
    }
  };
 
  return (
    <EconomicsShell
      studies={studies}
      selectedStudyId={selectedStudyId}
      routeFlowsheetId={flowsheetId}
      state={state}
      canEdit={access?.can_edit ?? true}
      readOnly={access?.read_only ?? false}
      isRevisionPreview={isRevisionPreview}
      errorStatus={queryErrorStatus(studiesQuery.error)}
      onStudyChange={(studyId) => {
        const study = studies.find((candidate) => candidate.id === studyId);
        Iif (study) selectStudy(study);
      }}
      onStudyRecalculated={() => {
        studiesQuery.refetch();
        dispatch(api.util.invalidateTags(["SimulationObjects", "properties"]));
      }}
      onStudyConfigurationChanged={() => studiesQuery.refetch()}
      onStudyCreated={(study) => {
        selectStudy(study);
        studiesQuery.refetch();
      }}
      onStudyDeleted={(deletedStudyId) => {
        const remainingStudies = studies.filter(
          (study) => study.id !== deletedStudyId,
        );
        const nextStudy =
          remainingStudies.find((study) => study.flowsheet === flowsheetId) ??
          remainingStudies[0];
        if (nextStudy) {
          selectStudy(nextStudy);
        } else {
          setStudyParam("");
        }
      }}
    />
  );
}
 
export { EconomicsShell } from "./EconomicsShell";