All files / src/pages/flowsheet-page FlowsheetPage.tsx

100% Statements 25/25
92.59% Branches 25/27
100% Functions 8/8
100% Lines 24/24

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                                            8784x 8784x   8784x   8784x   8784x         1x                   8784x   8784x   4x   1x 1x   1x   4x     4x     8784x   3x   3x           8784x   1x         8784x   2x           8784x                                                               8784x       8784x                                                        
import { ReactFlowProvider } from "@xyflow/react";
import { useCallback } from "react";
import { useResizable } from "react-resizable-layout";
import { useLocalStorage, useWindowSize } from "usehooks-ts";
import { useSelectedGroup } from "@/hooks/flowsheetObjects";
import FlowsheetSurface from "@/pages/flowsheet-page/flowsheet/Canvas/FlowsheetSurface";
import { useSearchParam } from "../../hooks/searchParams";
import ScenarioResult from "./dynamics/ScenarioResult";
import { DiagnosticRunPanel } from "./flowsheet/Diagnostics/DiagnosticRunPanel";
import FlowsheetObjectsContent from "./flowsheet/LeftSideBar/FlowsheetObjectsContent";
import { ContentTypes } from "./flowsheet/LeftSideBar/LeftSideBarTabDefinitions";
import { ScenarioList } from "./flowsheet/LeftSideBar/Scenarios/ScenarioList";
import TasksPanel from "./flowsheet/LeftSideBar/TasksPanel";
import UnitOpContent from "./flowsheet/LeftSideBar/UnitOpContent";
import RightSideBarData from "./flowsheet/PropertiesSidebar/ObjectDetailsPanel";
import { useMonitoringTables } from "./flowsheet/PropertiesSidebar/useMonitoringTables";
import ResizableHandle from "./flowsheet/ResizableHandle";
import { ResizablePanel } from "./flowsheet/Resizables";
import PinchAnalysis from "./pinch-analysis/PinchAnalysis";
import { ProcessGraphLeftTabContent } from "./process-graph/LeftTabContent";
 
export default function FlowsheetPage() {
  const [tab] = useSearchParam("content", ContentTypes.unitOps);
  const selectedGroup = useSelectedGroup();
 
  const { width = 0 } = useWindowSize();
 
  const [size, setSize] = useLocalStorage("panelsize", 370);
 
  const resizable = useResizable({
    axis: "x",
    initial: size,
    min: 270,
    max: 1200,
    onResizeEnd: ({ position }) => setSize(position),
  });
 
  // Monitoring tables management for the selected group
  const {
    tables: monitoringTables,
    createTable,
    deleteTable,
    setTableVisibility,
    renameTable,
  } = useMonitoringTables(selectedGroup?.id);
 
  const handleCreateMonitoringTable = useCallback(() => {
    // Find the next available table number by parsing existing names
    const existingNumbers = monitoringTables
      .map((t) => {
        const match = t.title.match(/^Monitoring Table (\d+)$/);
        return match ? parseInt(match[1], 10) : 0;
      })
      .filter((n) => n > 0);
    const nextNumber =
      existingNumbers.length > 0 ? Math.max(...existingNumbers) + 1 : 1;
 
    // Create table directly without opening dialog - empty tables are allowed
    createTable(`Monitoring Table ${nextNumber}`);
  }, [createTable, monitoringTables]);
 
  const handleToggleMonitoringTableVisibility = useCallback(
    (tableId: number) => {
      const table = monitoringTables.find((t) => t.id === tableId);
      if (table) {
        setTableVisibility(tableId, !table.visible);
      }
    },
    [setTableVisibility, monitoringTables],
  );
 
  const handleDeleteMonitoringTable = useCallback(
    (tableId: number) => {
      deleteTable(tableId);
    },
    [deleteTable],
  );
 
  const handleRenameMonitoringTable = useCallback(
    (tableId: number, newName: string) => {
      renameTable(tableId, newName);
    },
    [renameTable],
  );
 
  const LeftSideBar =
    tab == ContentTypes.pinch ? (
      <PinchAnalysis />
    ) : tab == ContentTypes.solverLogs ? (
      <TasksPanel />
    ) : tab == ContentTypes.unitOps ? (
      <UnitOpContent />
    ) : tab == ContentTypes.scenarios ? (
      <ScenarioList />
    ) : tab == ContentTypes.objectDetails ? (
      <RightSideBarData
        onCreateMonitoringTable={handleCreateMonitoringTable}
        onToggleMonitoringTableVisibility={
          handleToggleMonitoringTableVisibility
        }
        onDeleteMonitoringTable={handleDeleteMonitoringTable}
        monitoringTables={monitoringTables}
      />
    ) : tab == ContentTypes.pGraph ? (
      <ProcessGraphLeftTabContent />
    ) : tab == ContentTypes.objectList ? (
      <FlowsheetObjectsContent />
    ) : tab == ContentTypes.diagnostic ? (
      <DiagnosticRunPanel />
    ) : tab == ContentTypes.flowsheet ? null : (
      console.warn(
        "Unexpected tab/ ContentType:",
        tab,
        "Please add it to FlowsheetPage.tsx or CONTENT_MAP",
      )
    );
 
  // Calculate middle panel width
  const middleWidth = !LeftSideBar
    ? width - 55 // 55 for the left sidebar tabs
    : width - resizable.position - 5 - 55; // 5 for the handle bar size, 55 for the left sidebar tabs
 
  return (
    <>
      {LeftSideBar && (
        <>
          <ResizablePanel width={resizable.position}>
            {LeftSideBar}
          </ResizablePanel>
          <ResizableHandle
            isDragging={resizable.isDragging}
            {...resizable.separatorProps}
            id="left-sidebar-handle"
          ></ResizableHandle>
        </>
      )}
      <div className="w-full h-min-screen flex flex-col relative">
        <ReactFlowProvider>
          <FlowsheetSurface
            width={middleWidth}
            monitoringTables={monitoringTables}
            onHideTable={handleToggleMonitoringTableVisibility}
            onRenameTable={handleRenameMonitoringTable}
          />
        </ReactFlowProvider>
        <ScenarioResult />
      </div>
    </>
  );
}