All files / src/commands CommandBrowser.tsx

69.69% Statements 23/33
71.87% Branches 23/32
57.89% Functions 11/19
69.69% Lines 23/33

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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222                                    179x 179x   179x 179x 179x   179x 179x 179x 179x     179x 9x 9x 9x 9x       179x               2x               5x               2x                                                                                                                                 2x                 3x       1x                                             85x                                                                                           716x                      
import { CommandBinding, groupCommands, useCommands } from "just-search-it";
import { CommandFeature } from "./CommandFeature";
import { GroupTabs } from "./CommandTabs";
import { ClockIcon, Cross2Icon } from "@radix-ui/react-icons";
import { Button } from "@/ahuora-design-system/ui/button";
import { useState } from "react";
 
export function CommandBrowser({
  onClickHandler,
  onClickDelete,
  setIsSearching,
  recentCommandsArray,
}: {
  onClickHandler?: (command: CommandBinding<any>) => void;
  onClickDelete?: (command: CommandBinding<any>) => void;
  setIsSearching: React.Dispatch<React.SetStateAction<boolean>>;
  recentCommandsArray?: CommandBinding<any>[];
}) {
  const [activeToggle, setActiveToggle] = useState<string>("");
  const [isBrowserActive, setBrowserActive] = useState<boolean>(true);
 
  const commands = useCommands();
  const commandList = Object.values(commands);
  const commandGroups = groupCommands(commandList) || {};
 
  const addObjectName = "Add Object";
  const goToObjectName = "Go To Object";
  const switchToObjectName = "Switch To Object";
  const flowsheetControlName = "Flowsheet Controls";
 
  // Checks which content to display based on which filter button is clicked
  const handleClick = (name) => {
    setActiveToggle((prevString) => {
      const newValue = prevString === name ? "" : name;
      setBrowserActive(newValue === "");
      return newValue;
    });
  };
 
  return (
    <div className="pl-5">
      <div className="grid grid-row-2 gap-3 mt-2.5">
        <div className="flex gap-2">
          <CommandFilterButton
            label="Add Object Filter Button"
            name="Add Object"
            onClick={() => {
              handleClick(addObjectName);
            }}
            customClassName={`${activeToggle === addObjectName ? "bg-muted" : ""}`}
          />
          <CommandFilterButton
            label="Go To Object Filter Button"
            name="Go To Object"
            onClick={() => {
              handleClick(goToObjectName);
            }}
            customClassName={`${activeToggle === goToObjectName ? "bg-muted" : ""}`}
          />
          <CommandFilterButton
            label="Switch To Object Filter Button"
            name="Switch To Object"
            onClick={() => {
              handleClick(switchToObjectName);
            }}
            customClassName={`${activeToggle === switchToObjectName ? "bg-muted" : ""}`}
          />
        </div>
        <div className="">
          <CommandFilterButton
            label="Flowsheet Controls Filter Button"
            name="Flowsheet Controls"
            onClick={() => {
              handleClick(flowsheetControlName);
            }}
            customClassName={`${activeToggle === flowsheetControlName ? "bg-muted" : ""}`}
          />
        </div>
      </div>
      {activeToggle === addObjectName && (
        <GroupTabs
          commands={{
            "Add Object": commandGroups["Add Object"] || [],
          }}
          onClickHandler={onClickHandler}
        />
      )}
      {activeToggle === goToObjectName && (
        <GroupTabs
          commands={{
            "Go To Object": commandGroups["Go to object"] || [],
          }}
          onClickHandler={onClickHandler}
        />
      )}
      {activeToggle === switchToObjectName && (
        <GroupTabs
          commands={{
            "Switch To Object": commandGroups["Flowsheet"] || [],
          }}
          onClickHandler={onClickHandler}
        />
      )}
      {activeToggle === flowsheetControlName && (
        <GroupTabs
          commands={{
            "Display & Flowsheet Controls ":
              commandGroups["Display & flowsheet controls"] || [],
          }}
          onClickHandler={onClickHandler}
        />
      )}
      {isBrowserActive && (
        <>
          <p className="text-xs font-light opacity-40 mb-2 mt-4 ml-1">
            Quick Actions
          </p>
          <div className="w-[calc(100%-1.25rem)]">
            <CommandFeature
              commandBinding={commands["solve."]}
              onClick={() => onClickHandler?.(commands["solve."])}
            />
            <CommandFeature
              commandBinding={commands["shareFlowsheet."]}
              onClick={() => onClickHandler?.(commands["shareFlowsheet."])}
            />
            <CommandFeature
              commandBinding={commands["duplicateFlowsheet."]}
              onClick={() => onClickHandler?.(commands["duplicateFlowsheet."])}
            />
          </div>
          <p className="text-xs font-light opacity-40 mb-2 mt-3 ml-1">
            Analyse Your Flowsheet
          </p>
          <div className="w-[calc(100%-1.25rem)]">
            <CommandFeature
              commandBinding={commands["openPanel.pinch"]}
              onClick={() => onClickHandler?.(commands["openPanel.pinch"])}
            />
            <CommandFeature
              commandBinding={commands["openPanel.pGraph"]}
              onClick={() => onClickHandler?.(commands["openPanel.pGraph"])}
            />
            <CommandFeature
              commandBinding={commands["openPanel.summary"]}
              onClick={() => onClickHandler?.(commands["openPanel.summary"])}
            />
            <CommandFeature
              commandBinding={commands["openPanel.scenarios"]}
              onClick={() => onClickHandler?.(commands["openPanel.scenarios"])}
            />
            <CommandFeature
              commandBinding={commands["openPanel.logs"]}
              onClick={() => onClickHandler?.(commands["openPanel.logs"])}
            />
          </div>
          <div className="gap-5 w-[calc(100%-1.25rem)]">
            <div
              className={`flex flex-col clear-both ${recentCommandsArray?.length === 0 ? "hidden" : ""}`}
            >
              <p className="text-xs font-light opacity-40 mb-2 mt-3 ml-1">
                Recent Search
              </p>
              {recentCommandsArray?.map((item) => (
                <div className="flex items-center justify-start rounded-md w-full group bg-background transition-colors hover:bg-muted shadow-none">
                  <Button
                    aria-label={item.metadata.name}
                    onClick={() => {
                      item.run();
                      setIsSearching(false);
                      onClickHandler?.(item);
                    }}
                    style={{ color: "hsl(var(--foreground))" }}
                    className="justify-start gap-4 p-1 m-0 font-light text-sm rounded-md w-full bg-background transition-colors group-hover:bg-muted shadow-none"
                  >
                    <ClockIcon className="icon-ls" />
                    <span className="text-15 font-light">
                      {item.metadata.name}
                    </span>
                  </Button>
                  <Button
                    aria-label={"Delete" + item.metadata.name + "Command"}
                    onClick={() => onClickDelete?.(item)}
                    className="bg-background group-hover:bg-muted hover:!bg-accent shadow-none"
                    style={{ color: "hsl(var(--foreground))" }}
                  >
                    <Cross2Icon className="icon-ls opacity-[60%]" />
                    <span className="sr-only">Close</span>
                  </Button>
                </div>
              ))}
            </div>
          </div>
        </>
      )}
    </div>
  );
}
 
function CommandFilterButton({
  onClick,
  name,
  label,
  customClassName,
}: {
  onClick?: () => void;
  name?: string;
  label?: string;
  customClassName?: string;
}) {
  return (
    <Button
      aria-label={label}
      className={`text-xs font-light rounded-2xl border border-secondary color-foreground bg-background hover:bg-muted shadow-none ${customClassName}`}
      style={{ color: "hsl(var(--foreground))" }}
      onClick={onClick}
    >
      {name}
    </Button>
  );
}