All files / src/pages/flowsheet-page/economics/results-panel/navigation sourceNavigation.tsx

0% Statements 0/32
3.84% Branches 1/26
0% Functions 0/6
0% Lines 0/20

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                                                                                                                                                                       
import { ArrowUpRight } from "lucide-react";
import { Button } from "@/ahuora-design-system/ui/button";
import type {
  EconomicsResultLineRead,
  EconomicsStudyFullRead,
} from "@/api/apiStore.gen";
import type {
  EconomicsSourceNavigationTarget,
  SourceNavigationHandler,
} from "../model/types";
 
export function SourceNavigationButton({
  line,
  fullStudy,
  returnLabel,
  onNavigate,
}: {
  line: EconomicsResultLineRead;
  fullStudy?: EconomicsStudyFullRead;
  returnLabel: string;
  onNavigate?: SourceNavigationHandler;
}) {
  const target = sourceNavigationTarget(line, fullStudy, returnLabel);
  Iif (!target || !onNavigate) return null;
  const label =
    target.destinationStep === "unit-costs"
      ? `Open capital line source for ${line.label}`
      : `Open operating line source for ${line.label}`;
  return (
    <Button
      type="button"
      variant="tertiary"
      size="sm"
      className="ml-1.5 h-6 w-6 shrink-0 p-0 text-muted-foreground hover:bg-muted/80 hover:text-foreground"
      aria-label={label}
      title={label}
      onClick={() => onNavigate(target)}
    >
      <ArrowUpRight className="size-3.5" aria-hidden="true" />
    </Button>
  );
}
 
function sourceNavigationTarget(
  line: EconomicsResultLineRead,
  fullStudy: EconomicsStudyFullRead | undefined,
  returnLabel: string,
): EconomicsSourceNavigationTarget | undefined {
  Iif (!fullStudy) return undefined;
  if (line.source_operating_line) {
    const operatingLine = fullStudy.operating_lines.find(
      (item) => item.id === line.source_operating_line,
    );
    if (operatingLine && !operatingLine.costable_item) {
      return {
        destinationStep: "operating-lines",
        operatingLineId: operatingLine.id,
        resultLineId: line.id,
        returnLabel,
      };
    }
  }
 
  const capitalLineCostableItemId = line.source_capital_line
    ? fullStudy.capital_lines.find(
        (item) => item.id === line.source_capital_line,
      )?.costable_item
    : undefined;
  const costableItemId = line.source_costable_item ?? capitalLineCostableItemId;
  const costableItem = costableItemId
    ? fullStudy.costable_items.find((item) => item.id === costableItemId)
    : undefined;
  if (costableItem?.simulation_object) {
    return {
      destinationStep: "unit-costs",
      economicsUnitId: costableItem.simulation_object,
      resultLineId: line.id,
      returnLabel,
    };
  }
 
  return undefined;
}