All files / src/components/common/GlobalVariable index.tsx

0% Statements 0/28
0% Branches 0/17
0% Functions 0/7
0% Lines 0/28

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                                                                                                                                                                                                                     
import React, { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import {
  currentPlaybookSelector,
  deleteVariable,
  updateGlobalVariable,
} from "../../../store/features/playbook/playbookSlice.ts";
import AddVariableOverlay from "./AddVariableOverlay.js";
import { Add, CloseRounded } from "@mui/icons-material";
import useIsPrefetched from "../../../hooks/playbooks/useIsPrefetched.ts";
import CustomButton from "../CustomButton/index.tsx";
import CustomInput from "../../Inputs/CustomInput.tsx";
import { InputTypes } from "../../../types/inputs/inputTypes.ts";
import { Tooltip } from "@mui/material";
 
function GlobalVariables() {
  const [isAddVariableOpen, setIsAddVariableOpen] = useState(false);
  const playbook = useSelector(currentPlaybookSelector);
  const dispatch = useDispatch();
  const isPrefetched = useIsPrefetched();
 
  const variables = playbook?.global_variable_set ?? {};
  const globalVariables = Object.keys(variables);
 
  const openOverlay = () => {
    setIsAddVariableOpen(true);
  };
 
  const handleDelete = (key) => {
    dispatch(deleteVariable({ name: key }));
  };
 
  Iif (isPrefetched && globalVariables.length === 0) {
    return null;
  }
 
  return (
    <div
      className={`w-[330px] my-0 text-sm p-1 border rounded min-h-[100px] bg-white`}>
      <div style={{ paddingLeft: 0 }} className="flex items-center gap-2 p-1">
        {!isPrefetched && (
          <CustomButton onClick={openOverlay}>
            <Add fontSize="small" /> Variable
          </CustomButton>
        )}
        {globalVariables?.length > 0 &&
          `(${globalVariables?.length} variable${
            globalVariables?.length > 1 ? "s" : ""
          })`}
      </div>
      {!isPrefetched && <hr />}
      <div className="flex items-center flex-wrap gap-1 mt-2">
        {globalVariables?.length > 0 ? (
          globalVariables.map((key) => (
            <div key={key} className={`flex gap-1 flex-wrap p-1`}>
              <div className="bg-violet-100 p-1 flex items-center rounded w-[80px]">
                <Tooltip title={key}>
                  <p className="text-xs text-center text-ellipsis overflow-hidden">
                    {key}
                  </p>
                </Tooltip>
              </div>
              <div className="flex gap-2 items-center">
                <CustomInput
                  inputType={InputTypes.TEXT}
                  value={variables[key]}
                  placeholder={"Enter variable value"}
                  className="!w-[200px]"
                  handleChange={(val) => {
                    dispatch(updateGlobalVariable({ name: key, value: val }));
                  }}
                />
                {!isPrefetched && (
                  <CloseRounded
                    onClick={() => handleDelete(key)}
                    className="text-black cursor-pointer hover:text-red-500 transition-all !text-sm"
                  />
                )}
              </div>
            </div>
          ))
        ) : (
          <p className="text-gray-400 text-xs">
            Variables defined in the playbook will be visible here. Read more
            about variables{" "}
            <a
              href="https://docs.drdroid.io/docs/global-variables"
              target="_blank"
              rel="noreferrer"
              className="text-violet-600">
              here
            </a>
            .
          </p>
        )}
      </div>
 
      <AddVariableOverlay
        isOpen={isAddVariableOpen}
        close={() => setIsAddVariableOpen(false)}
      />
    </div>
  );
}
 
export default GlobalVariables;