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

80.9% Statements 89/110
90.16% Branches 55/61
22.22% Functions 2/9
87.2% Lines 75/86

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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232                                                                    54x     54x               54x 54x 54x       54x 54x 63x     54x 54x 54x 57x   54x     61x   54x 9x 117x   54x   2x   113x   54x 10x 10x 10x 87x   54x             74x   54x           74x   54x   54x     48x       6x 6x 6x 64x       7x                   7x 28x                               61x     61x   10x           74x   35x     54x 54x     68x 33x       153x 120x 159x   37x     54x 54x   33x             186x 165x   43x     54x 54x   39x               249x 183x   26x     54x 54x     5x     54x 61x   64x 26x           184x 106x 132x   438x      
import { useEffect, useState } from "react";
import {
  Tabs,
  TabsContent,
  TabsList,
  TabsTrigger,
} from "@/ahuora-design-system/ui/tabs";
import type { EconomicsStudyRead } from "@/api/apiStore.gen";
import { useSearchParam } from "@/hooks/searchParams";
import { EconomicsCostItemsPanel } from "../cost-items-panel";
import {
  ECONOMICS_STEP_PARAM,
  ECONOMICS_UNIT_PARAM,
} from "../navigation/economicsNavigation";
import { EconomicsOperatingLinesPanel } from "../operating-lines-panel";
import {
  EconomicsResultsPanel,
  type EconomicsSourceNavigationTarget,
} from "../results-panel";
import { EconomicsSettingsPanel } from "../settings-panel";
import {
  SourceReturnBanner,
  StepSectionHeader,
  StudyOverview,
} from "./components";
import {
  ECONOMICS_STEPS,
  type EconomicsStepValue,
  economicsStepStates,
  reviewResultsDetail,
  selectedEconomicsStep,
  selectedEconomicsUnitId,
} from "./model";
 
export function EconomicsStudySurface({
  study,
  canEdit,
  recalculationRefreshKey = 0,
  onStudyConfigurationChanged,
}: {
  study: EconomicsStudyRead;
  canEdit: boolean;
  recalculationRefreshKey?: number;
  onStudyConfigurationChanged?: () => void;
}) {
  const [resultRefreshKey, setResultRefreshKey] = useState(0);
  const [configurationRefreshKey, setConfigurationRefreshKey] = useState(0);
  const [stepParam, setStepParam] = useSearchParam(
    ECONOMICS_STEP_PARAM,
    "operating-lines",
  );
  const [unitParam, setUnitParam] = useSearchParam(ECONOMICS_UNIT_PARAM);
  const [selectedStep, setSelectedStep] = useState<EconomicsStepValue>(
    selectedEconomicsStep(stepParam),
  );
  const [sourceReturnTarget, setSourceReturnTarget] =
    useState<EconomicsSourceNavigationTarget | null>(null);
  const [resultFocusLineId, setResultFocusLineId] = useState<number>();
  const [resultFocusRequest, setResultFocusRequest] = useState(0);
  const focusedSimulationObjectId = selectedEconomicsUnitId(unitParam);
  const focusedOperatingLineId =
    sourceReturnTarget?.destinationStep === "operating-lines"
      ? sourceReturnTarget.operatingLineId
      : undefined;
  const stepStates = economicsStepStates(study);
 
  useEffect(() => {
    setSelectedStep(selectedEconomicsStep(stepParam));
  }, [stepParam]);
 
  useEffect(() => {
    if (recalculationRefreshKey > 0) {
      setResultRefreshKey((current) => current + 1);
    }
  }, [recalculationRefreshKey]);
 
  const handleConfigurationSaved = () => {
    setConfigurationRefreshKey((current) => current + 1);
    setResultRefreshKey((current) => current + 1);
    onStudyConfigurationChanged?.();
  };
 
  const navigateToSource = (target: EconomicsSourceNavigationTarget) => {
    setSourceReturnTarget(target);
    if (target.economicsUnitId) {
      setUnitParam(String(target.economicsUnitId));
    }
    setSelectedStep(target.destinationStep);
    setStepParam(target.destinationStep);
  };
 
  const returnToResults = () => {
    setResultFocusLineId(sourceReturnTarget?.resultLineId);
    setSourceReturnTarget(null);
    setSelectedStep("project-overview");
    setStepParam("project-overview");
    setResultFocusRequest((current) => current + 1);
  };
 
  const dismissSourceReturn = () => {
    setSourceReturnTarget(null);
  };
 
  return (
    <div className="box-border min-h-full w-full min-w-0 max-w-full p-5">
      <Tabs
        value={selectedStep}
        onValueChange={(value) => {
          const nextStep = value as EconomicsStepValue;
          setSelectedStep(nextStep);
          setStepParam(nextStep);
        }}
        className="min-w-0"
        aria-label="Economics workflow steps"
      >
        <section
          className="mb-4 min-w-0 rounded-md border bg-card p-3"
          aria-label="Economics workflow navigation"
        >
          <div>
            <TabsList
              bg="blend"
              rounded="no"
              className="grid h-auto w-full grid-cols-2 gap-1 bg-transparent p-0 sm:grid-cols-3 xl:grid-cols-5"
            >
              {ECONOMICS_STEPS.map((step, index) => (
                <TabsTrigger
                  key={step.value}
                  value={step.value}
                  variant="underline"
                  aria-label={`${index + 1}. ${step.label}, ${
                    stepStates[step.value]
                  }`}
                  className="h-auto min-w-0 flex-col items-start gap-1 rounded-md px-3 py-2 text-left data-[state=inactive]:bg-muted/55 data-[state=inactive]:hover:bg-muted/75"
                >
                  <span>
                    {index + 1}. {step.label}
                  </span>
                  <span className="text-xs font-normal text-muted-foreground">
                    {stepStates[step.value]}
                  </span>
                </TabsTrigger>
              ))}
            </TabsList>
          </div>
        </section>
 
        {sourceReturnTarget && (
          <SourceReturnBanner
            returnLabel={sourceReturnTarget.returnLabel}
            onReturn={returnToResults}
            onDismiss={dismissSourceReturn}
          />
        )}
 
        <TabsContent
          value="settings"
          forceMount
          hidden={selectedStep !== "settings"}
          aria-hidden={selectedStep !== "settings"}
        >
          <section className="min-w-0" aria-label="Settings tab">
            <StudyOverview study={study} canEdit={canEdit} />
            <EconomicsSettingsPanel
              study={study}
              canEdit={canEdit}
              onConfigurationSaved={handleConfigurationSaved}
            />
          </section>
        </TabsContent>
 
        <TabsContent
          value="unit-costs"
          forceMount
          hidden={selectedStep !== "unit-costs"}
          aria-hidden={selectedStep !== "unit-costs"}
        >
          <section className="min-w-0" aria-label="Capital costs tab">
            <EconomicsCostItemsPanel
              study={study}
              canEdit={canEdit}
              focusedSimulationObjectId={focusedSimulationObjectId}
              onConfigurationSaved={handleConfigurationSaved}
            />
          </section>
        </TabsContent>
 
        <TabsContent
          value="operating-lines"
          forceMount
          hidden={selectedStep !== "operating-lines"}
          aria-hidden={selectedStep !== "operating-lines"}
        >
          <section className="min-w-0" aria-label="Operating costs tab">
            <EconomicsOperatingLinesPanel
              study={study}
              canEdit={canEdit}
              refreshKey={configurationRefreshKey}
              focusedOperatingLineId={focusedOperatingLineId}
              onConfigurationSaved={handleConfigurationSaved}
            />
          </section>
        </TabsContent>
 
        <TabsContent
          value="project-overview"
          forceMount
          hidden={selectedStep !== "project-overview"}
          aria-hidden={selectedStep !== "project-overview"}
        >
          <section className="min-w-0" aria-label="Project overview tab">
            <div className="mb-4 rounded-md border bg-card p-3">
              <StepSectionHeader
                title="Project overview"
                state={stepStates["project-overview"]}
                detail={reviewResultsDetail(study)}
              />
            </div>
            <EconomicsResultsPanel
              study={study}
              refreshKey={resultRefreshKey}
              focusResultLineId={resultFocusLineId}
              focusRequestKey={resultFocusRequest}
              onNavigateToSource={navigateToSource}
            />
          </section>
        </TabsContent>
      </Tabs>
    </div>
  );
}