All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar/Connections AddPortCommands.tsx

85.71% Statements 12/14
66.66% Branches 2/3
66.66% Functions 2/3
90.9% Lines 10/11

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                33x   33x     757x 757x 757x     757x 757x   748x 993x 86x                            
import { defineCommand } from "just-search-it";
import { RegisterCommand } from "../../../../../commands/CommandProvider";
import { useAddPort } from "../../../../../hooks/connections";
import { useCurrentObject } from "../../../../../hooks/flowsheetObjects";
import objects from "@/data/objects.json";
import { ObjectType } from "../../../../../data/ObjectTypes.gen";
import { SquareDot } from "lucide-react";
 
objects as Record<string, ObjectType>;
 
export const AddPort = defineCommand<[number, string], null>("addPort");
 
export default function AddPortCommands() {
  const currentObject = useCurrentObject();
  const addPort = useAddPort();
  Iif (!currentObject) return null;
 
  const unitConfig: ObjectType =
    objects[currentObject.objectType as keyof typeof objects];
  if (!unitConfig?.ports) return null; // e.g recycle
 
  return Object.entries(unitConfig!.ports).map(([key, port]) => {
    if (!port.many) return null;
    return (
      <RegisterCommand
        key={key}
        command={AddPort}
        args={[currentObject.id, key]}
        name={`Add Port: ${port.displayName}`}
        description={`Add a ${port.displayName} to the ${currentObject.componentName}.`}
        group="Connections"
        icon={<SquareDot />}
        action={() => addPort(currentObject.id, key)}
      />
    );
  });
}