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

90% Statements 18/20
100% Branches 8/8
75% Functions 3/4
90% Lines 18/20

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                                                176x 176x   176x 176x       176x   176x     10x         10x 15x   10x                   10x 10x                     10x 10x             10x 10x     176x                                                                 2268x                        
import { X } from "lucide-react";
import React, { useState } from "react";
import { ObjectPropertySelector } from "@/ahuora-design-system/inputs/PropertySelection";
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>
          <X size={18} className="mr-1  cursor-pointer" onClick={onDelete} />
        </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;