All files / src/components/Playbooks/task/taskConfiguration/comparison TimeseriesOffestSelection.tsx

0% Statements 0/21
0% Branches 0/4
0% Functions 0/3
0% Lines 0/21

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                                                                                                                               
import useCurrentTask from "../../../../../hooks/playbooks/task/useCurrentTask";
import useIsPrefetched from "../../../../../hooks/playbooks/useIsPrefetched";
import { updateCardById } from "../../../../../utils/execution/updateCardById";
import CustomInput from "../../../../Inputs/CustomInput";
import { InputTypes } from "../../../../../types";
import {
  CUSTOM_KEY,
  DROPDOWN_KEY,
  TIMESERIES_OFFSET_KEY,
  timeseriesOffsetOptions,
} from "./utils";
import getNestedValue from "../../../../../utils/common/getNestedValue";
 
type TimeseriesOffestSelectionProps = {
  id: string;
};
 
function TimeseriesOffestSelection({ id }: TimeseriesOffestSelectionProps) {
  const [task] = useCurrentTask(id);
  const ui_value = getNestedValue(task, DROPDOWN_KEY);
  const value = getNestedValue(task, TIMESERIES_OFFSET_KEY);
  const isPrefetched = useIsPrefetched();
 
  const handleChange = (optionId: string) => {
    updateCardById(DROPDOWN_KEY, optionId, id);
    if (optionId !== CUSTOM_KEY) {
      const hours = optionId.split("-")[1];
      handleTimerangeChange(hours);
    } else {
      handleTimerangeChange("");
    }
  };
 
  const handleTimerangeChange = (value: string) => {
    updateCardById(TIMESERIES_OFFSET_KEY, value, id);
  };
 
  return (
    <div className="mt-1 flex flex-col gap-2">
      <CustomInput
        inputType={InputTypes.DROPDOWN}
        options={timeseriesOffsetOptions}
        value={ui_value}
        handleChange={handleChange}
        placeholder="Select a time range"
        disabled={!!isPrefetched}
      />
      {ui_value === CUSTOM_KEY && (
        <CustomInput
          inputType={InputTypes.TEXT}
          type="number"
          value={value}
          handleChange={handleTimerangeChange}
          placeholder="Enter number of hours"
          disabled={!!isPrefetched}
          label="Number of hours"
        />
      )}
    </div>
  );
}
 
export default TimeseriesOffestSelection;