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

88.09% Statements 37/42
81.81% Branches 45/55
50% Functions 2/4
96.66% Lines 29/30

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                                                3003x                   1897x 1897x 1897x 1897x 2308x   1897x   56x 2975x         2659x   585x                           3652x 1897x                       9485x 1113x                       6349x 401x                       3100x 8057x 4977x 1897x     1897x 1897x 2308x 1897x     3794x 3794x   13279x 5691x      
import { ArrowRightToLine, PlusCircle, Trash2, Unlink } from "lucide-react";
 
import SelectInput from "@/ahuora-design-system/inputs/SelectInput";
import { Label } from "@/ahuora-design-system/ui/label";
import { ToolTipCover } from "@/ahuora-design-system/ui/tooltip";
import { SimulationObjectRetrieveRead } from "@/api/apiStore.gen";
import { useSearchParam } from "@/hooks/searchParams";
 
type OptionsObject = {
  name: string;
  id: number;
};
 
type ConnectionProps = {
  displayName: string; // connection label
  currentObject?: SimulationObjectRetrieveRead; // current connection simulation object, so we can go to the object
  current?: number; // current connection id
  options: OptionsObject[];
  onConnectionChange: (id: number) => void;
  onDisconnect: () => void;
  onAdd?: () => void;
  onDelete?: () => void;
  canMutate?: boolean;
};
export function Connection(props: ConnectionProps) {
  const {
    displayName,
    currentObject,
    current,
    options,
    onConnectionChange,
    onDisconnect,
    onAdd,
    onDelete,
    canMutate = true,
  } = props;
  const labelId = `displayName-${displayName}`;
  const [, setSearchParam] = useSearchParam("object");
  const currentObjectId = currentObject?.id?.toString();
 
  const goToObject = () => {
    Iif (!currentObjectId) return;
    setSearchParam(currentObjectId);
  };
 
  return (
    <div className="w-full">
      <div className="flex flex-row items-center justify-between">
        <Label htmlFor={labelId}>{displayName}</Label>
        <div className="flex flex-row items-center gap-2">
          {currentObject && (
            <ToolTipCover
              content={`View ${currentObject.componentName} details.`}
              asChild
            >
              <button
                type="button"
                className="ml-2 cursor-pointer hover:opacity-50"
                onClick={goToObject}
                aria-label={`View ${displayName} details`}
              >
                <ArrowRightToLine size={16} />
              </button>
            </ToolTipCover>
          )}
          {current && (
            <ToolTipCover content={`Disconnect ${displayName}.`} asChild>
              <button
                type="button"
                onClick={onDisconnect}
                aria-label={`Disconnect ${displayName}.`}
                disabled={!canMutate}
                className="ml-2 inline-flex disabled:cursor-not-allowed disabled:opacity-50"
              >
                <Unlink size={16} className="h-4 w-4 text-red-500" />
              </button>
            </ToolTipCover>
          )}
          {onAdd && !current && (
            <ToolTipCover content={`Add a new ${displayName}.`} asChild>
              <button
                type="button"
                onClick={onAdd}
                aria-label={`Add a new ${displayName}.`}
                disabled={!canMutate}
                className="ml-2 inline-flex disabled:cursor-not-allowed disabled:opacity-50"
              >
                <PlusCircle size={16} className="h-4 w-4 text-green-500" />
              </button>
            </ToolTipCover>
          )}
          {onDelete && (
            <ToolTipCover content={`Delete ${displayName}.`} asChild>
              <button
                type="button"
                onClick={onDelete}
                aria-label={`Delete ${displayName}.`}
                disabled={!canMutate}
                className="ml-2 inline-flex disabled:cursor-not-allowed disabled:opacity-50"
              >
                <Trash2 size={16} className="h-4 w-4 text-red-500" />
              </button>
            </ToolTipCover>
          )}
        </div>
      </div>
      <div className="flex flex-row" aria-label="sidebar-connections">
        <SelectInput
          id={labelId}
          disabled={!canMutate || options.length == 0}
          title={`Select ${displayName}`}
          value={current ? current.toString() : ""}
          data={options.map((option) => ({
            value: option.id.toString(),
            label: option.name,
          }))}
          handleChange={(value) => onConnectionChange(parseInt(value))}
        />
      </div>
    </div>
  );
}