All files / src/pages/flowsheet-page/economics/operating-lines-panel OperatingLinesEditorList.tsx

75% Statements 24/32
78.78% Branches 26/33
43.75% Functions 7/16
81.81% Lines 18/22

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                                2007x                                                           2007x   2007x 15x                                         2x     7222x       2619x         15x                                         26091x         2007x             16056x     16056x     15x 15x   15x       2007x 16056x   16056x      
import { Badge } from "@/ahuora-design-system/ui/badge";
import type {
  EconomicsDefaultRateRead,
  EconomicsSettingsProfileRead,
  OperatingCostLineRead,
  PatchedOperatingCostLine,
} from "@/api/apiStore.gen";
import { OperatingLineEditor } from "./editor/OperatingLineEditor";
import {
  isOutletSuggestion,
  OPERATING_LINE_TYPES,
  operatingLineType,
} from "./model/types";
import { OutletSuggestionCard } from "./suggestions/OutletSuggestionCard";
import { EmptyState, SectionHeading } from "./ui/fields";
 
export function OperatingLinesEditorList({
  ariaLabel,
  title,
  help,
  emptyText,
  lines,
  canEdit,
  defaultRates,
  assumptions,
  annualOperatingHours,
  focusedOperatingLineId,
  onPatchLine,
  onDeleteLine,
}: {
  ariaLabel: string;
  title: string;
  help: string;
  emptyText: string;
  lines: OperatingCostLineRead[];
  canEdit: boolean;
  defaultRates: EconomicsDefaultRateRead[];
  assumptions?: EconomicsSettingsProfileRead | null;
  annualOperatingHours: string;
  focusedOperatingLineId?: number;
  onPatchLine: (
    lineId: number,
    patch: PatchedOperatingCostLine,
  ) => Promise<void>;
  onDeleteLine: (lineId: number) => Promise<void>;
}) {
  const groups = groupOperatingLines(lines);
 
  const renderLine = (line: OperatingCostLineRead) =>
    isOutletSuggestion(line) ? (
      <OutletSuggestionCard
        key={line.id}
        line={line}
        canEdit={canEdit}
        defaultRates={defaultRates}
        assumptions={assumptions}
        annualOperatingHours={annualOperatingHours}
        isFocused={line.id === focusedOperatingLineId}
        onPatch={(patch) => onPatchLine(line.id, patch)}
        onDelete={() => onDeleteLine(line.id)}
      />
    ) : (
      <OperatingLineEditor
        key={line.id}
        line={line}
        canEdit={canEdit}
        defaultRates={defaultRates}
        assumptions={assumptions}
        annualOperatingHours={annualOperatingHours}
        isFocused={line.id === focusedOperatingLineId}
        onPatch={(patch) => onPatchLine(line.id, patch)}
        onDelete={() => onDeleteLine(line.id)}
      />
    );
 
  return (
    <div className="space-y-3" aria-label={ariaLabel}>
      <SectionHeading title={title} count={lines.length} help={help} />
      {lines.length === 0 ? (
        <EmptyState text={emptyText} />
      ) : (
        <div className="space-y-4">
          {groups.map(({ type, lines: groupLines }) => (
            <section
              key={type.value}
              className="space-y-2"
              aria-label={`${type.label} operating lines`}
            >
              <div className="flex flex-wrap items-center gap-2 border-b pb-1.5">
                <div className="flex min-w-0 flex-wrap items-center gap-2">
                  <h5 className="text-xs font-semibold text-muted-foreground">
                    {type.label}
                  </h5>
                  <Badge variant="secondary" size="xs" borderRadius="round">
                    {groupLines.length}
                  </Badge>
                </div>
              </div>
              <div className="space-y-2">{groupLines.map(renderLine)}</div>
            </section>
          ))}
        </div>
      )}
    </div>
  );
}
 
function groupOperatingLines(lines: OperatingCostLineRead[]) {
  const groups = new Map<
    string,
    {
      type: ReturnType<typeof operatingLineType>;
      lines: OperatingCostLineRead[];
    }
  >();
  const orderedCategories = OPERATING_LINE_TYPES.map((type) => type.value);
 
  for (const type of OPERATING_LINE_TYPES) {
    groups.set(type.value, { type, lines: [] });
  }
  for (const line of lines) {
    const type = operatingLineType(line.category, line.economic_effect);
    const group = groups.get(type.value);
    if (group) {
      group.lines.push(line);
    }
  }
 
  return orderedCategories
    .map((category) => groups.get(category))
    .filter((group): group is NonNullable<typeof group> =>
      Boolean(group && group.lines.length > 0),
    );
}