All files / src/hooks projectTabs.ts

48.38% Statements 30/62
30.43% Branches 7/23
64.7% Functions 11/17
49.12% Lines 28/57

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                              49x 49x 49x 49x 49x 49x         49x       49x                       49x 11179x 11179x           11179x 11179x   11179x 49x 49x           11179x 11179x 11179x     11179x 11179x   12424x       11179x   49x     49x                                                                                             14434x 14434x 7669x    
import { useRunCommand } from "just-search-it";
import { useSearchParams } from "react-router-dom";
import {
  useFlowsheetTabs,
  useFlowsheetUnitOps,
  useGroup,
} from "@/hooks/flowsheetObjects";
import { LocalTab } from "@/hooks/localTabModels";
import { useProjectId } from "@/hooks/project";
import { useSearchParam } from "@/hooks/searchParams.ts";
import { LocalTabStateEnum, useLocalTabState } from "@/hooks/useLocalTabState";
import { SwitchGroup } from "../commands/SwitchCurrentGroup";
import { ContentTypes } from "../pages/flowsheet-page/flowsheet/LeftSideBar/LeftSideBarTabDefinitions";
 
// Numeric tab states
export enum TabStateEnum {
  Closed = 0,
  Open = 1,
  Unsaved = 2,
  Locked = 3,
  Root = 4,
}
 
// Converts numeric TabStateEnum into local string-based LocalTabStateEnum
function convertTabStateEnum(numState: TabStateEnum): LocalTabStateEnum {
  switch (numState) {
    case TabStateEnum.Closed:
      return LocalTabStateEnum.Closed;
    case TabStateEnum.Open:
      return LocalTabStateEnum.Open;
    case TabStateEnum.Unsaved:
      return LocalTabStateEnum.UnSaved;
    case TabStateEnum.Locked:
      return LocalTabStateEnum.Locked;
    case TabStateEnum.Root:
      return LocalTabStateEnum.Root;
    default:
      return LocalTabStateEnum.Open;
  }
}
 
export const useTabClick = () => {
  const switchGroup = useRunCommand(SwitchGroup);
  return (tab: LocalTab) => {
    switchGroup(tab.id);
  };
};
 
export function useSwitchToGroup() {
  const changeTabState = useChangeTabState();
  const [_, setParentGroup] = useSearchParam("parentGroup");
 
  return (tab: LocalTab) => {
    changeTabState(tab, TabStateEnum.Open);
    setParentGroup(`${tab.tabOwner || 0}`);
  };
}
 
// Change a tab’s state locally
export function useChangeTabState() {
  const { tabMap, updateTab } = useLocalTabState(useProjectId().toString());
  const [searchParams, setSearchParams] = useSearchParams();
  const tabClick = useTabClick();
 
  // Grab only tabs that are open or root from tabMap
  const allTabs = useFlowsheetTabs() || [];
  const tabs = allTabs.filter(
    (t) =>
      tabMap[t.id] === LocalTabStateEnum.Open ||
      tabMap[t.id] === LocalTabStateEnum.Root,
  );
 
  return (tab: LocalTab, newState: TabStateEnum) => {
    // update local tab state
    updateTab(tab.id.toString(), convertTabStateEnum(newState));
 
    // if closing
    Iif (newState === TabStateEnum.Closed) {
      const updatedSearchParams = new URLSearchParams(searchParams);
      updatedSearchParams.delete("parentGroup");
      setSearchParams(updatedSearchParams);
 
      // find a previous tab
      const currentIndex = tabs.findIndex((t) => t.id === tab.id);
      const previousTab = (() => {
        for (let i = currentIndex - 1; i >= 0; i--) {
          const state = tabMap[tabs[i].id];
          Iif (
            state === LocalTabStateEnum.Open ||
            state === LocalTabStateEnum.Root
          ) {
            return tabs[i];
          }
        }
        // fallback
        return tabs[1];
      })();
      Iif (previousTab) {
        tabClick(previousTab);
      }
    }
  };
}
 
// Show a name for the tab (e.g. group name, etc.)
export function useNameToShow() {
  const getGroup = useGroup();
  const unitops = useFlowsheetUnitOps();
  return (tab: LocalTab) => {
    let displayName = tab.name || "";
    Iif (tab.type === ContentTypes.flowsheet) {
      const group = getGroup(tab.tabOwner);
      const simulationObject = group?.simulationObject;
      const unitop = unitops?.find((uo) => uo.id === simulationObject);
      Iif (unitop) {
        displayName = unitop.componentName;
      }
    }
    return displayName;
  };
}
 
// Find a tab by groupId
export function useFlowsheetTabFromGroupId() {
  const flowsheetTabs = useFlowsheetTabs();
  return (groupId: number) =>
    flowsheetTabs?.find((tab) => tab.tabOwner === groupId);
}