All files / src/pages/flowsheet-page/dynamics ScenarioResult.tsx

82.43% Statements 61/74
41.93% Branches 13/31
33.33% Functions 2/6
93.61% Lines 44/47

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                            103x 103x   1321x 1321x 1321x 1321x 2387x 1487x   1321x 1321x 1321x 1321x 1321x       1321x       1321x   1321x               1457x 1729x   1032x       1x                 1x       145x               58x   45x 13x     45x 58x         13x     45x 135x 13x     45x   58x   45x 58x 61x 225x       21x       21x     98x 119x   21x 196x   119x            
import { ChevronUp, GripHorizontal, X } from "lucide-react";
import { useRef } from "react";
import { useResizable } from "react-resizable-layout";
import { useLocalStorage, useWindowSize } from "usehooks-ts";
import { Button } from "@/ahuora-design-system/ui/button";
import {
  StateNameEnum,
  useCoreScenarioRetrieveQuery,
} from "@/api/apiStore.gen";
import { useFlowsheetRevisionPreview } from "@/hooks/flowsheetRevisionPreview";
import { cn } from "@/lib/utils";
import { useSearchParam } from "../../../hooks/searchParams";
import ResultPanel from "./results-panel/ResultPanel";
 
const MIN_DRAWER_HEIGHT = 320;
const DEFAULT_DRAWER_HEIGHT = 420;
 
const ScenarioResult = () => {
  const [scenarioId] = useSearchParam("scenario");
  const { isPreview } = useFlowsheetRevisionPreview();
  const { data: scenario } = useCoreScenarioRetrieveQuery(
    { id: Number(scenarioId) },
    { skip: !scenarioId || isPreview },
  );
  const [open, setOpen] = useLocalStorage("scenario-result-open", false);
  const drawerRef = useRef<HTMLDivElement>(null);
  const { height: windowHeight = 900 } = useWindowSize();
  const viewportHeight = windowHeight || 900;
  const maxDrawerHeight = Math.max(
    MIN_DRAWER_HEIGHT,
    Math.floor(viewportHeight * 0.8),
  );
  const [drawerHeight, setDrawerHeight] = useLocalStorage(
    "scenario-result-height",
    DEFAULT_DRAWER_HEIGHT,
  );
  const resizable = useResizable({
    axis: "y",
    initial: Math.min(
      Math.max(drawerHeight, MIN_DRAWER_HEIGHT),
      maxDrawerHeight,
    ),
    min: MIN_DRAWER_HEIGHT,
    max: maxDrawerHeight,
    reverse: true,
    containerRef: drawerRef,
    onResizeEnd: ({ position }) => setDrawerHeight(position),
  });
 
  Iif (!scenarioId) return null;
 
  if (isPreview) {
    return (
      <div className="absolute w-full flex justify-center bottom-1 left-0 z-1">
        <Button
          variant="secondary"
          size="sm"
          className="select-none bg-background rounded-full"
          disabled
        >
          Scenario results are not retained in versions.
        </Button>
      </div>
    );
  }
 
  Iif (!scenario || scenario.state_name == StateNameEnum.SteadyState)
    return null;
 
  if (open) {
    return (
      <div
        ref={drawerRef}
        className="absolute bottom-0 left-0 w-full z-10 bg-background flex"
        style={{ height: resizable.position }}
      >
        <div
          className={cn(
            "absolute left-0 top-0 flex h-3 w-full cursor-row-resize items-start justify-center border-t border-border/80 bg-transparent transition-colors",
            "hover:border-primary hover:bg-primary/20",
            resizable.isDragging && "border-primary bg-primary/20",
          )}
          aria-label="Resize scenario results"
          data-testid="scenario-result-resize-handle"
          {...resizable.separatorProps}
        >
          <GripHorizontal
            aria-hidden="true"
            className="pointer-events-none mt-0.5 h-3 w-5 text-muted-foreground"
          />
        </div>
        <Button
          variant="ghost"
          size="icon"
          className={cn("select-none", "absolute right-3 top-3 z-20")}
          aria-label="Close scenario results"
          onClick={() => setOpen(false)}
        >
          <X size={16} />
        </Button>
        <ResultPanel scenario={scenario} />
      </div>
    );
  } else {
    return (
      <div className="absolute w-full flex justify-center bottom-1 left-0 z-1">
        <Button
          variant="secondary"
          size="sm"
          className={cn(
            "select-none bg-background rounded-full",
            "absolute bottom-1 left-1/2 -translate-x-1/2 z-1",
          )}
          onClick={() => setOpen(true)}
        >
          <p>Scenario results</p>
          <ChevronUp />
        </Button>
      </div>
    );
  }
};
 
export default ScenarioResult;