All files / src/hooks project.ts

65.57% Statements 40/61
68.75% Branches 11/16
71.42% Functions 5/7
68.42% Lines 26/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                  1774645x         6778x 6778x   7045x 7019x       6778x 6778x 6778x   7233x 7233x       444836x 444836x 444836x 18127x 444836x 462963x 462138x 444836x       132x 132x 132x   132x 2x     2x             192x 196x                                
import {
  FlowsheetRead,
  useCoreFlowsheetsCreateMutation,
  useCoreFlowsheetsRetrieveQuery,
  useCoreFlowsheetsSetActiveCreateMutation,
  useCoreProjectsRetrieveQuery,
} from "@/api/apiStore.gen";
import { useUrlParam } from "@/hooks/params";
 
export function useFlowsheetId() {
  /* get the flowsheetId from the URL */
  return +useUrlParam("flowsheetId")!;
}
 
export function useCurrentFlowsheet() {
  const flowsheetId = useFlowsheetId();
  return useCoreFlowsheetsRetrieveQuery(
    { id: String(flowsheetId) },
    { skip: !flowsheetId },
  );
}
 
export function useCurrentProject() {
  const flowsheet = useCurrentFlowsheet();
  const projectId = flowsheet.currentData?.project;
  return useCoreProjectsRetrieveQuery(
    { id: String(projectId) },
    { skip: !projectId },
  );
}
 
export function useFlowsheet() {
  const flowsheetId = useFlowsheetId();
  const flowsheet = useCoreFlowsheetsRetrieveQuery(
    {
      id: String(flowsheetId),
    },
    { skip: !flowsheetId },
  );
  return flowsheet.currentData as FlowsheetRead | undefined;
}
 
export function useCreateFlowsheetInCurrentProject() {
  const flowsheet = useFlowsheet();
  const [createFlowsheet, result] = useCoreFlowsheetsCreateMutation();
 
  const createInCurrentProject = (name?: string) => {
    const projectId = flowsheet?.project;
    if (!projectId) {
      throw new Error("No current project found");
    }
    return createFlowsheet({
      flowsheet: {
        project: projectId,
        ...(name ? { name } : {}),
      },
    }).unwrap();
  };
  return { createInCurrentProject, ...result };
}
 
export function useSetActiveFlowsheet() {
  const [setActiveFlowsheet, result] =
    useCoreFlowsheetsSetActiveCreateMutation();
 
  const activateFlowsheet = (flowsheetId: number) => {
    return setActiveFlowsheet({
      id: String(flowsheetId),
      flowsheet: {},
    }).unwrap();
  };
 
  return { activateFlowsheet, ...result };
}