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

80.73% Statements 88/109
81.08% Branches 60/74
22.22% Functions 2/9
86.9% Lines 73/84

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 233 234 235 236 237 238 239 240 241 242 243 244                                                                          269x     269x 269x                 269x 269x 269x       269x 269x 304x     269x 269x 269x 281x   269x     306x   269x 35x 573x   269x   5x   555x   269x 66x 66x 66x 427x   269x             353x   269x           353x   269x   269x     245x       23x 23x 23x 311x       37x                   37x 185x                               306x     306x   42x           353x   160x   343x 160x       749x   589x   160x                 909x   215x                   1344x   245x   20x     269x 306x   309x 160x 269x       909x 134x                   1073x   1004x   37x   20x     269x 306x   309x 37x       380x   343x   2474x      
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 { UnavailableRevisionOutput } from "@/pages/flowsheet-page/flowsheet/revisions/UnavailableRevisionOutput";
import { EconomicsComparisonPanel } from "../comparison-panel";
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,
  ProjectBaselinePanel,
} 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,
  isRevisionPreview = false,
  recalculationRefreshKey = 0,
  onStudyConfigurationChanged,
}: {
  study: EconomicsStudyRead;
  canEdit: boolean;
  isRevisionPreview?: 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">
          <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">
          <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">
          <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">
          <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>
            <ProjectBaselinePanel
              key={`project-baseline-${study.id}-${study.updated_at}`}
              study={study}
              canEdit={canEdit}
              onSaved={handleConfigurationSaved}
            />
            {isRevisionPreview ? (
              <UnavailableRevisionOutput message="Calculated economics results are not retained in versions." />
            ) : (
              <EconomicsResultsPanel
                study={study}
                refreshKey={resultRefreshKey}
                focusResultLineId={resultFocusLineId}
                focusRequestKey={resultFocusRequest}
                onNavigateToSource={navigateToSource}
              />
            )}
          </section>
        </TabsContent>
 
        <TabsContent value="comparison">
          <section className="min-w-0" aria-label="Comparison tab">
            <div className="mb-4 rounded-md border bg-card p-3">
              <StepSectionHeader
                title="Comparison"
                state={stepStates.comparison}
                detail={reviewResultsDetail(study)}
              />
            </div>
            {isRevisionPreview ? (
              <UnavailableRevisionOutput message="Calculated economics comparisons are not retained in versions." />
            ) : (
              <EconomicsComparisonPanel study={study} canEdit={canEdit} />
            )}
          </section>
        </TabsContent>
      </Tabs>
    </div>
  );
}