All files / src/pages/flowsheet-page/flowsheet/LeftSideBar UnitOpContent.tsx

83.78% Statements 31/37
74.07% Branches 20/27
71.42% Functions 10/14
87.09% Lines 27/31

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                                        33x   33x 389x 389x 389x 3929x 389x     389x 389x     389x   389x 3929x       18283x         39x       3929x 3890x       389x 3501x 18x 18x       389x         389x       389x                   1167x                                                                                                                           3528x                             14730x       39x                                  
import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
} from "@/ahuora-design-system/ui/accordion";
import { Button } from "@/ahuora-design-system/ui/button";
import { ChevronsDownUpIcon, Search, X } from "lucide-react";
import { ToolTipCover } from "@/ahuora-design-system/ui/tooltip";
import { Input } from "@/ahuora-design-system/ui/input";
import { useState } from "react";
import { small } from "@/data/iconSizes";
import { ObjectCard } from "./ObjectCard";
import { TemplateCard } from "./TemplateCard";
import objects from "@/data/objects.json";
import { ScrollArea } from "../../../../ahuora-design-system/ui/scroll-area";
import { Tabs, TabsTrigger } from "../../../../ahuora-design-system/ui/tabs";
import { TabsList } from "@radix-ui/react-tabs";
import { usePanelData } from "./PanelItems";
 
const TAB_VALUES = ["All", "Chemical", "Power"];
 
const UnitOpContent = () => {
  const [tabFilter, setTabFilter] = useState<string>("All");
  const [searchedObj, setSearchedObj] = useState<string>("");
  const panelData = usePanelData();
  const initialOpenAccordions = panelData.map((tab) => tab.label);
  const [openAccordions, setOpenAccordions] = useState<string[]>(
    initialOpenAccordions
  );
  const isAnyAccordionOpen = openAccordions.length > 0;
  const closeAllAccordions = () => setOpenAccordions([]);
 
  // Normalize tab filter for comparison
  const normalizedTabFilter = tabFilter.toLowerCase();
 
  const accordionsDisplay = panelData
    .map((tab) => ({
      ...tab,
      objects: tab.objects.filter(
        (obj) =>
          obj in objects &&
          obj.toLowerCase().includes(searchedObj.toLowerCase())
      ),
      // Filter templates by search term
      templates: tab.templates?.filter((template) =>
        template.name.toLowerCase().includes(searchedObj.toLowerCase())
      ),
    }))
    .filter((tab) => {
      if (tab.type === "template") return normalizedTabFilter === "all";
      if (
        tab.objects.length === 0 &&
        (!tab.templates || tab.templates.length === 0)
      )
        return false;
      if (normalizedTabFilter === "all") return true;
      Iif (normalizedTabFilter === "chemical") return tab.type === "chemical";
      if (normalizedTabFilter === "power") return tab.type === "energy";
      return false;
    });
 
  const accordionsToOpen = searchedObj
    ? accordionsDisplay.map((tab) => tab.label)
    : [];
 
  const mergedOpenAccordions =
    searchedObj.length > 0
      ? [...new Set([...openAccordions, ...accordionsToOpen])]
      : openAccordions;
 
  return (
    <Tabs
      value={tabFilter}
      onValueChange={setTabFilter}
      className="h-full w-full"
    >
      <div className="flex flex-col h-full w-full pt-2">
        {/* Sub-tab header */}
        <TabsList className="flex flex-row bg-muted text-secondary-foreground rounded-t-lg shadow-sm p-1 mx-2">
          {TAB_VALUES.map((tab) => (
            <TabsTrigger value={tab} key={tab}>
              {tab}
            </TabsTrigger>
          ))}
        </TabsList>
 
        {/* Header with search and collapse button */}
        <div className="px-4 items-center flex justify-between gap-4">
          <div className="relative flex-1 my-2">
            <Input
              name="search"
              placeholder="Search operations"
              type="text"
              startIcon={Search}
              onChange={(e) => setSearchedObj(e.target.value)}
              value={searchedObj}
              className="pr-10"
              aria-label="search-unitOpPanel"
            />
            {searchedObj && (
              <Button
                variant="ghost"
                size="icon"
                className="absolute right-1 top-1/2 transform -translate-y-1/2 hover:bg-transparent hover:text-neutral-500"
                onClick={() => setSearchedObj("")}
                aria-label="Clear search"
              >
                <X size={small} />
              </Button>
            )}
          </div>
          <ToolTipCover
            asChild
            content={
              isAnyAccordionOpen
                ? "Collapses all open accordions."
                : "Collapses all open accordions. No current accordions are open."
            }
          >
            <Button
              variant="ghost"
              size="icon"
              disabled={searchedObj.length > 0 || !isAnyAccordionOpen}
              className="hover:bg-transparent hover:text-neutral-500 w-max"
              onClick={closeAllAccordions}
              aria-label="Collapse all accordions"
            >
              <ChevronsDownUpIcon size={18} />
            </Button>
          </ToolTipCover>
        </div>
 
        {/* Accordion content */}
        <ScrollArea>
          <Accordion
            type="multiple"
            value={mergedOpenAccordions}
            onValueChange={setOpenAccordions}
            defaultValue={mergedOpenAccordions}
          >
            {accordionsDisplay.length !== 0 ? (
              accordionsDisplay.map((tab) => (
                <AccordionItem
                  key={tab.label}
                  value={tab.label}
                  className="p-1"
                >
                  <AccordionTrigger
                    variant="default"
                    aria-label={`objectAccordion-${tab.label}`}
                  >
                    {tab.label}
                  </AccordionTrigger>
                  <AccordionContent className="p-2 mt-1.5">
                    {/* Use same flex layout for both unit ops and templates */}
                    <div className="flex flex-row flex-wrap">
                      {tab.objects.length > 0 &&
                        tab.objects.map((o) => <ObjectCard key={o} type={o} />)}
                      {tab.templates &&
                        tab.templates.length > 0 &&
                        tab.templates.map((template) => (
                          <TemplateCard key={template.id} template={template} />
                        ))}
                    </div>
                  </AccordionContent>
                </AccordionItem>
              ))
            ) : (
              <p className="italic text-foreground ml-2">No current projects</p>
            )}
          </Accordion>
        </ScrollArea>
      </div>
    </Tabs>
  );
};
 
export default UnitOpContent;