All files / src/pages/flowsheet-page/economics/page EconomicsShell.tsx

88.88% Statements 32/36
67.39% Branches 31/46
40% Functions 2/5
91.3% Lines 21/23

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                                                      48x       48x 48x               48x   70x     48x 2x 2x 89x     48x           48x                   480x 48x         55x 62x 48x         192x 41x             253x 204x   144x      
import { useMemo, useState } from "react";
import { ScrollArea } from "@/ahuora-design-system/ui/scroll-area";
import { Separator } from "@/ahuora-design-system/ui/separator";
import type { EconomicsStudyRead } from "@/api/apiStore.gen";
import {
  EconomicsEmptyState,
  EconomicsErrorState,
  EconomicsHeader,
  EconomicsLoadingState,
} from "./components";
import { EconomicsStudySurface } from "./EconomicsStudySurface";
import type { EconomicsLoadState } from "./model";
 
export type EconomicsShellProps = {
  studies: EconomicsStudyRead[];
  selectedStudyId?: number;
  state: EconomicsLoadState;
  canEdit?: boolean;
  readOnly?: boolean;
  errorStatus?: string;
  onStudyChange: (studyId: number) => void;
  onStudyRecalculated?: () => void;
  onStudyConfigurationChanged?: () => void;
  onStudyCreated?: (studyId: number) => void;
  onStudyDeleted?: (studyId: number) => void;
};
 
export function EconomicsShell({
  studies,
  selectedStudyId,
  state,
  canEdit = true,
  readOnly = false,
  errorStatus,
  onStudyChange,
  onStudyRecalculated,
  onStudyConfigurationChanged,
  onStudyCreated,
  onStudyDeleted,
}: EconomicsShellProps) {
  const [recalculationRefreshKey, setRecalculationRefreshKey] = useState(0);
  const selectedStudy = useMemo(
    () => studies.find((study) => study.id === selectedStudyId),
    [studies, selectedStudyId],
  );
  const handleStudyRecalculated = () => {
    setRecalculationRefreshKey((current) => current + 1);
    onStudyRecalculated?.();
  };
 
  return (
    <main
      aria-label="Economics workspace"
      className="h-full min-w-0 overflow-hidden bg-background"
      data-testid="economics-shell"
    >
      <div className="flex h-full flex-col">
        <EconomicsHeader
          readOnly={readOnly}
          state={state}
          studies={studies}
          selectedStudy={selectedStudy}
          canEdit={canEdit}
          onStudyChange={onStudyChange}
          onStudyCreated={onStudyCreated}
          onStudyDeleted={onStudyDeleted}
          onStudyRecalculated={handleStudyRecalculated}
        />
        <Separator />
        <ScrollArea
          className="min-h-0 flex-1"
          aria-label="Economics panel content"
        >
          {state === "loading" && <EconomicsLoadingState />}
          {state === "error" && <EconomicsErrorState status={errorStatus} />}
          {state === "empty" && (
            <EconomicsEmptyState
              canEdit={canEdit}
              onStudyCreated={onStudyCreated}
            />
          )}
          {state === "ready" && selectedStudy && (
            <EconomicsStudySurface
              study={selectedStudy}
              canEdit={canEdit}
              recalculationRefreshKey={recalculationRefreshKey}
              onStudyConfigurationChanged={onStudyConfigurationChanged}
            />
          )}
        </ScrollArea>
      </div>
    </main>
  );
}