All files / src/commands SwitchCurrentGroup.tsx

96% Statements 24/25
75% Branches 3/4
100% Functions 6/6
100% Lines 23/23

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                                      33x     3761x 25268x   3761x 3761x 3761x 3761x   3761x 3761x 4632x 4632x           3761x   16x 16x 16x 16x 16x         3761x     4632x   4615x 4615x   4615x                 16x                  
import { GroupingRead } from "@/api/apiStore.gen";
import {
  useFlowsheetTabFromGroupId,
  useSwitchToGroup,
} from "@/hooks/projectTabs";
import { Group } from "lucide-react";
import {
  useFlowsheetUnitOps,
  useGetGroupFromSimobject,
  useUpdateCoordinatesOnSwitchGroup as useSwitchGroup,
} from "../hooks/flowsheetObjects";
import { RegisterCommand } from "./CommandProvider";
import { defineCommand } from "just-search-it";
 
type GroupEntry = {
  group?: GroupingRead;
  simulationObjectName: string;
};
 
const SwitchGroup = defineCommand<[groupId: number], void>("switchGroup");
 
function SwitchGroupCommands({ setFlowsheetSurfaceState, zoomToFit }) {
  const simulationObjects = useFlowsheetUnitOps()?.filter(
    (obj) => obj.objectType === "group",
  );
  const getGroupFromSimulationObject = useGetGroupFromSimobject();
  const switchGroup = useSwitchGroup(setFlowsheetSurfaceState, zoomToFit);
  const openTab = useSwitchToGroup();
  const getFlowsheetTabFromGroupId = useFlowsheetTabFromGroupId();
 
  const groupEntries: GroupEntry[] = [];
  simulationObjects?.forEach((obj) => {
    const group = getGroupFromSimulationObject(obj.id);
    groupEntries.push({
      group: group,
      simulationObjectName: obj.componentName!,
    });
  });
 
  const action = (group: GroupingRead) => {
    // Todo: Make this work with the flowsheet, which doesn't have a simulation object
    const flowsheetTab = getFlowsheetTabFromGroupId(group.id);
    if (flowsheetTab) {
      openTab(flowsheetTab);
      if (group) {
        switchGroup(group.id.toString());
      }
    }
  };
 
  return (
    <>
      {groupEntries.map((entry, index) => {
        if (!entry.group) return null;
        // Check if tab exists for that group
        const flowsheetTab = getFlowsheetTabFromGroupId(entry.group.id);
        Iif (!flowsheetTab) return null;
 
        return (
          <RegisterCommand
            key={entry.simulationObjectName}
            command={SwitchGroup}
            args={[entry.group.id]}
            name={`Switch to ${entry.simulationObjectName}`}
            description={`Switch to ${entry.simulationObjectName}`}
            group="Flowsheet"
            icon={<Group />}
            action={() => action(entry.group!)}
          />
        );
      })}
    </>
  );
}
 
export { SwitchGroupCommands, SwitchGroup };