All files / src/pages/flowsheet-page/dynamics/results-panel ResultPanel.tsx

75% Statements 51/68
87.03% Branches 47/54
16.66% Functions 1/6
83.01% Lines 44/53

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                                                                99x 99x 99x       99x 135x   135x 99x 99x   99x     117x         4x           99x     99x         117x   99x     135x   99x     171x   99x 99x   99x 99x   99x 2x 99x   99x 2x       2x         195x     99x       213x       58x                     99x                                           267x 41x                 304x 21x             183x 21x           162x 369x   27x             180x 215x      
import { useCallback, useState } from "react";
import { useLocalStorage } from "usehooks-ts";
import { Checkbox } from "@/ahuora-design-system/ui/checkbox";
import { Label as FormLabel } from "@/ahuora-design-system/ui/label";
import {
  Tabs,
  TabsContent,
  TabsList,
  TabsTrigger,
} from "@/ahuora-design-system/ui/tabs";
import {
  ScenarioRead,
  useLazyMssDownloadResultsRetrieveQuery,
} from "@/api/apiStore.gen";
import {
  useCurrentObjectId,
  useFlowsheetUnitOps,
} from "@/hooks/flowsheetObjects";
import { useSearchParam } from "@/hooks/searchParams";
import { SelectedPropertyInfo } from "../../flowsheet/PropertiesSidebar/ObjectDetailsPanel";
import SelectPropertiesDialog from "../../flowsheet/PropertiesSidebar/SelectPropertiesDialog";
import { ResultObjectPicker } from "./components/ResultObjectPicker";
import type { ResultMetricNavigationTarget } from "./utils/resultMetricNavigation";
import GraphResultView from "./views/GraphResultView";
import HistogramResultView from "./views/HistogramResultView";
import { ObjectResultsTable } from "./views/ObjectResultsTable";
 
type HighlightedMetricState = {
  selectedObjectId: number | undefined;
  target: ResultMetricNavigationTarget | null;
};
 
export default function ResultPanel({ scenario }: { scenario: ScenarioRead }) {
  const [tab, setTab] = useLocalStorage<string>("result-sheet-tab", "graph");
  const [includeZeroBaseline, setIncludeZeroBaseline] = useLocalStorage(
    "scenario-graph-include-zero-baseline",
    false,
  );
  const currentObjectId = useCurrentObjectId();
  const selectedObjectId = Number.isFinite(currentObjectId)
    ? currentObjectId
    : undefined;
  const objects = useFlowsheetUnitOps() ?? [];
  const [, setObjectId] = useSearchParam("object");
  const [highlightedMetricState, setHighlightedMetricState] =
    useState<HighlightedMetricState>({
      selectedObjectId,
      target: null,
    });
  if (highlightedMetricState.selectedObjectId !== selectedObjectId) {
    // Highlighted rows belong to the result object that produced the metric.
    // Resetting here prevents children from rendering a stale row target after
    // URL-driven object changes, without synchronizing derived state in an effect.
    setHighlightedMetricState({
      selectedObjectId,
      target: null,
    });
  }
  const highlightedMetricTarget =
    highlightedMetricState.selectedObjectId === selectedObjectId
      ? highlightedMetricState.target
      : null;
  const clearMetricTarget = useCallback(() => {
    setHighlightedMetricState({
      selectedObjectId,
      target: null,
    });
  }, [selectedObjectId]);
 
  function handleMetricTargetSelect(target: ResultMetricNavigationTarget) {
    setHighlightedMetricState({ selectedObjectId, target });
    setTab("table");
  }
 
  function handleObjectChange(objectId: string) {
    clearMetricTarget();
    setObjectId(objectId);
  }
 
  const [isDialogOpen, setIsDialogOpen] = useState(false);
  const [tempSelectedProperties, setTempSelectedProperties] = useState<
    SelectedPropertyInfo[]
  >([]);
  const [export_data] = useLazyMssDownloadResultsRetrieveQuery();
 
  function handleDownload() {
    setIsDialogOpen(true);
  }
 
  function handleSave() {
    const properties = tempSelectedProperties
      .map((prop) => prop.propertyId)
      .filter((propertyId): propertyId is number => propertyId !== undefined);
 
    export_data({
      flowsheet: scenario.flowsheet,
      properties,
      scenario: scenario.id,
    });
  }
 
  const resultObjectPicker = (
    <ResultObjectPicker
      objects={objects}
      onObjectChange={handleObjectChange}
      selectedObjectId={selectedObjectId}
    />
  );
 
  return (
    <>
      <Tabs
        defaultValue="graph"
        className="flex min-w-0 flex-1 flex-col"
        value={tab}
        onValueChange={setTab}
      >
        <TabsList className="w-fit m-auto rounded my-4">
          <TabsTrigger value="graph">Graph view</TabsTrigger>
          <TabsTrigger value="histogram">Histogram view</TabsTrigger>
          <TabsTrigger value="table">Table view</TabsTrigger>
        </TabsList>
        {tab !== "table" && (
          <div className="flex shrink-0 flex-wrap items-center justify-between gap-2 px-4 pb-3">
            {resultObjectPicker}
            {tab === "graph" && (
              <div className="inline-flex h-8 items-center gap-2 rounded-md border bg-background px-2">
                <Checkbox
                  id="scenario-graph-include-zero-baseline"
                  checked={includeZeroBaseline}
                  onCheckedChange={(checked) =>
                    setIncludeZeroBaseline(checked === true)
                  }
                />
                <FormLabel
                  htmlFor="scenario-graph-include-zero-baseline"
                  className="cursor-pointer text-xs font-medium leading-none"
                >
                  Include zero baseline
                </FormLabel>
              </div>
            )}
          </div>
        )}
        <TabsContent value="table" className="min-w-0 overflow-auto">
          <ObjectResultsTable
            handleDownload={handleDownload}
            highlightedMetricTarget={highlightedMetricTarget}
            objectPicker={resultObjectPicker}
            onClearMetricTarget={clearMetricTarget}
            scenario={scenario}
            selectedObjectId={selectedObjectId}
          />
        </TabsContent>
        <TabsContent value="graph" className="overflow-auto">
          <GraphResultView
            scenario={scenario}
            onMetricTargetSelect={handleMetricTargetSelect}
            includeZeroBaseline={includeZeroBaseline}
            selectedObjectId={selectedObjectId}
          />
        </TabsContent>
        <TabsContent value="histogram" className="overflow-auto">
          <HistogramResultView
            scenario={scenario}
            onMetricTargetSelect={handleMetricTargetSelect}
            selectedObjectId={selectedObjectId}
          />
        </TabsContent>
      </Tabs>
 
      <SelectPropertiesDialog
        isDialogOpen={isDialogOpen}
        setIsDialogOpen={setIsDialogOpen}
        tempSelectedProperties={tempSelectedProperties}
        setTempSelectedProperties={setTempSelectedProperties}
        handleSave={handleSave}
        description="Choose which properties you want to download for this result."
      />
    </>
  );
}