All files / src/pages/flowsheet-page/flowsheet/LeftSideBar/Scenarios ScenarioDataViewer.tsx

86.27% Statements 44/51
77.77% Branches 28/36
42.85% Functions 3/7
90.69% Lines 39/43

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                                          1116x   134x   134x 134x   134x 134x       134x   134x   134x 16x         16x 24x   16x             212x   134x 16x 16x       212x   134x   186x   134x 16x 16x 134x   134x       16x 16x 160x     88x   160x 26x                 134x   160x 186x                         212x   88x     134x                           398x 398x          
import { X } from "lucide-react";
import React, { useState } from "react";
import { ObjectPropertySelector } from "@/ahuora-design-system/inputs/PropertySelection";
import { AccessControlledButton as Button } from "@/ahuora-design-system/ui/accessControlled";
import DebouncedInput from "@/ahuora-design-system/ui/debounced-input";
import { Label } from "@/ahuora-design-system/ui/label";
import { ToolTipCover } from "@/ahuora-design-system/ui/tooltip";
import {
  api,
  DataColumnRead,
  PropertyValueRead,
  useCoreDataColumnDestroyMutation,
  useCoreDataColumnPartialUpdateMutation,
} from "@/api/apiStore.gen";
import { useAppDispatch } from "@/store/hooks";
 
type ScenarioDataViewerProps = {
  dataColumn: DataColumnRead;
  showValue?: boolean;
};
 
const ScenarioDataViewer: React.FC<ScenarioDataViewerProps> = ({
  dataColumn,
  showValue = false,
}) => {
  const [editDataColumn] = useCoreDataColumnPartialUpdateMutation();
  const [deleteDataColumn] = useCoreDataColumnDestroyMutation();
  const initialSimulationObjectId =
    (dataColumn.propertySimulationObject as number) | undefined;
  const [simulationObjectId, setSimulationObjectId] = useState<
    number | undefined
  >(initialSimulationObjectId);
 
  const [edited, setEdited] = useState(false);
 
  const dispatch = useAppDispatch();
 
  const onOptimistic = (updatedValue: Partial<DataColumnRead>) => {
    dispatch(
      api.util.updateQueryData(
        "coreDataColumnList",
        { scenario: dataColumn.scenario! },
        (currentDataColumns) => {
          const index = currentDataColumns.findIndex(
            (prop) => prop.id === dataColumn.id,
          );
          currentDataColumns[index] = {
            ...currentDataColumns[index],
            ...updatedValue,
          };
        },
      ),
    );
  };
 
  const onUpdate = (updatedValue: Partial<DataColumnRead>) => {
    onOptimistic(updatedValue);
    editDataColumn({
      id: dataColumn.id,
      patchedDataColumn: updatedValue,
    });
  };
 
  const onDelete = () => {
    deleteDataColumn({ id: dataColumn.id });
  };
 
  const handleSimulationObjectChange = (id: number) => {
    setSimulationObjectId(id);
    setEdited(true);
  };
 
  const handlePropertyChange = async (
    id: number,
    property: PropertyValueRead,
  ) => {
    setEdited(false);
    onUpdate({ property_value: id });
  };
 
  return (
    <>
      <div className="flex flex-row justify-between ">
        <Label className="text-foreground outlined">{dataColumn.name}</Label>
        <ToolTipCover content="Delete" asChild>
          <Button
            type="button"
            variant="ghost"
            size="icon"
            className="mr-1 h-6 w-6"
            aria-label="Delete"
            onClick={onDelete}
          >
            <X size={18} />
          </Button>
        </ToolTipCover>
      </div>
 
      {showValue && (
        <div className="flex flex-col w-[200px]">
          <label className="text-sm text-muted-foreground mb-1">
            DataColumn
          </label>
          <DebouncedInput
            placeholder="DataColumn"
            value={dataColumn.value}
            onUpdate={(value) => onUpdate({ value: value })}
          />
        </div>
      )}
 
      <div className="flex flex-col">
        <ObjectPropertySelector
          simulationObjectId={
            edited ? simulationObjectId : initialSimulationObjectId
          }
          propertyId={dataColumn.property_value}
          onSimulationObjectChange={handleSimulationObjectChange}
          onPropertyChange={handlePropertyChange}
          layout="flat"
          useValueId={true}
          filter={(value) =>
            value.propertyValue &&
            ((value.propertyValue.enabled &&
              !value.propertyValue.controlManipulatedId) ||
              value.propertyValue.controlSetPointId)
          }
        />
      </div>
    </>
  );
};
 
export default ScenarioDataViewer;