All files / src/hooks PinchHooks.ts

70.49% Statements 86/122
68.08% Branches 32/47
57.14% Functions 8/14
80.3% Lines 53/66

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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186                                  1198x 1198x         1198x   1299x 1597x                                           1198x                                           23x 23x         23x 1x 23x 24x 24x   29x               254x 254x         254x 19x 254x 273x 272x   362x               23x 23x         23x 1x 23x 24x 24x   32x                               37x 37x   37x 53x 53x     60x   94x               124x 124x         124x 10x 124x 134x 134x   175x     139x 139x         139x 13x 139x 152x 151x   199x    
import {
  StreamDataProjectRead,
  useGraphicsgroupingsZonesRetrieveQuery,
  usePinchPinchinputListQuery,
  usePinchPinchoptionsListQuery,
  usePinchPinchoutputListQuery,
  usePinchPinchutilityListQuery,
  usePinchSegmentListQuery,
  usePinchStreamDataProjectListQuery,
} from "@/api/apiStore.gen";
import { useProjectId } from "@/hooks/project";
import { useSearchParam } from "@/hooks/searchParams";
 
/**
 * Gets all the 'StreamDataProject' objects related to the currently active flowsheet.
 * @returns Array of Projects - Type StreamDataProjectRead.
 */
export function useStreamDataProjects() {
  const flowsheetId: number = useProjectId();
  const {
    data: projects,
    isLoading,
    isError,
  } = usePinchStreamDataProjectListQuery({
    flowsheet: flowsheetId,
  });
  return { projects: projects as StreamDataProjectRead[], isLoading, isError };
}
 
/**
 * Provides a function to query current StreamDataProjects by ID.
 * @returns Function taking an id parameter which returns the project of type StreamDataProjectRead.
 */
export function useStreamDataProject() {
  const { projects, isLoading, isError } = useStreamDataProjects();
  return {
    getStreamDataProject: (projectID: number) =>
      projects.find((project) => project.id === projectID),
    isLoading,
    isError,
  };
}
 
/**
 * Provides the current selected StreamDataProject ID
 * @returns ID of current StreamDataProject
 */
export function useCurrentStreamDataProjectID() {
  const { projects, isLoading, isError } = useStreamDataProjects();
  return projects?.[0].id;
}
 
/**
 * Gets the currently selected StreamDataProject.
 * @returns Currently selected StreamDataProject of type StreamDataProjectRead.
 */
export function useCurrentStreamDataProject() {
  const currentProject = useCurrentStreamDataProjectID();
  const { getStreamDataProject, isLoading, isError } = useStreamDataProject();
  const project = currentProject
    ? getStreamDataProject(+currentProject)
    : undefined;
  return { project, isLoading, isError };
}
 
/**
 * Gets the inputs related to the current selected flowsheet.
 * TODO: Migrate to multi-project per flowsheet (by pinch search parameter).
 * @returns Selected StreamDataProjects input of type PinchInputRead
 */
export function useCurrentPinchInputs() {
  const project = useCurrentStreamDataProjectID();
  const {
    data: inputs,
    isLoading,
    isError,
  } = usePinchPinchinputListQuery(
    {
      projectOwner: +project,
    },
    { skip: !project },
  );
  return { inputs, isLoading, isError };
}
 
/**
 * Gets the streams related to the current selected flowsheet.
 * TODO: Migrate to multi-project per flowsheet (by pinch search parameter).
 * @returns Selected StreamDataProjects stream of type SegmentRead[]
 */
export function useCurrentSegments() {
  const project = useCurrentStreamDataProjectID();
  const {
    data: streams,
    isLoading,
    isError,
  } = usePinchSegmentListQuery(
    {
      projectOwner: +project,
    },
    { skip: !project },
  );
  return { streams, isLoading, isError };
}
 
/**
 * Gets the utilities related to the current selected flowsheet.
 * TODO: Migrate to multi-project per flowsheet (by pinch search parameter).
 * @returns Selected StreamDataProjects utilities of type PinchUtilitiyRead[]
 */
export function useCurrentPinchUtilities() {
  const project = useCurrentStreamDataProjectID();
  const {
    data: utilities,
    isLoading,
    isError,
  } = usePinchPinchutilityListQuery(
    {
      projectOwner: +project,
    },
    { skip: !project },
  );
  return { utilities, isLoading, isError };
}
 
/**
 * Gets the options related to the current selected flowsheet.
 * TODO: Migrate to multi-project per flowsheet (by pinch search parameter).
 * @returns Selected StreamDataProjects options of type MainOptionsRead[]
 */
// export function useCurrentPinchOptions() {
//     const project = useCurrentStreamDataProjectID();
//     const {data: options, isLoading, isError} = usePinchPinchoptionsListQuery({
//         projectOwner: +project,
//     }, {skip: !project});
//     return{ options, isLoading, isError};
// }
 
export function useCurrentPinchOptions() {
  const project = useCurrentStreamDataProjectID();
 
  const queryResult = usePinchPinchoptionsListQuery(
    { projectOwner: +project },
    { skip: !project },
  );
 
  const { data: options, isLoading, isError } = queryResult || {};
 
  return { options, isLoading, isError };
}
 
/**
 * Gets the outputs related to the current selected flowsheet.
 * TODO: Migrate to multi-project per flowsheet (by pinch search parameter).
 * @returns Selected StreamDataProjects outputs of type PinchOutputsRead[]
 */
export function useCurrentPinchOutputs() {
  const project = useCurrentStreamDataProjectID();
  const {
    data: outputs,
    isLoading,
    isError,
  } = usePinchPinchoutputListQuery(
    {
      projectOwner: +project,
    },
    { skip: !project },
  );
  return { outputs, isLoading, isError };
}
 
export function useCurrentStreamDataZones() {
  const project = useCurrentStreamDataProjectID();
  const {
    data: zones,
    isLoading,
    isError,
  } = useGraphicsgroupingsZonesRetrieveQuery(
    {
      projectOwner: +project,
    },
    { skip: !project },
  );
  return { zones, isLoading, isError };
}