All files / src/pages/flowsheet-page/economics/cost-curves/authoring DriverSpecEditor.tsx

62.06% Statements 36/58
33.33% Branches 25/75
19.04% Functions 4/21
65.85% Lines 27/41

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                        16x                             16x   20x 16x 2x   2x     48x 16x     64x 16x                           48x 16x               48x       16x                     48x 32x 16x 16x 16x 16x 16x                                                                                                       1x                     1x         128x 32x 18x 64x      
import { Plus, Trash2 } from "lucide-react";
import { Badge } from "@/ahuora-design-system/ui/badge";
import { Button } from "@/ahuora-design-system/ui/button";
import { Label } from "@/ahuora-design-system/ui/label";
import type { CostCurveDriverSpec, UnitOption } from "@/api/apiStore.gen";
import { RoleEnum } from "@/api/apiStore.gen";
import { UnitSelectField } from "../../shared/ui/EconomicsFormFields";
import { unitOptionsForSpec } from "../model/unitOptions";
import { TextField } from "../ui/CostCurveFields";
import { FieldError } from "../ui/FieldError";
import { createFormulaInputSpec } from "./helpers";
 
export function DriverSpecEditor({
  modeId,
  specs,
  defaultUnitOptions,
  discreteFamily,
  error,
  onChange,
}: {
  modeId: string;
  specs: CostCurveDriverSpec[];
  defaultUnitOptions: UnitOption[];
  discreteFamily: boolean;
  error?: string;
  onChange: (specs: CostCurveDriverSpec[]) => void;
}) {
  const formulaSpecs = specs.filter(
    (spec) => spec.role === RoleEnum.FormulaInput,
  );
  const updateSpec = (index: number, patch: Partial<CostCurveDriverSpec>) => {
    onChange(
      specs.map((spec, specIndex) =>
        specIndex === index ? { ...spec, ...patch } : spec,
      ),
    );
  };
  const addFormulaInput = () => {
    const nextIndex = formulaSpecs.length + 1;
    onChange([...specs, createFormulaInputSpec(nextIndex)]);
  };
  const removeSpec = (index: number) => {
    const nextSpecs = specs.filter((_, specIndex) => specIndex !== index);
    const hasPrimaryFormula = nextSpecs.some(
      (spec) => spec.role === RoleEnum.FormulaInput && spec.primary,
    );
    onChange(
      hasPrimaryFormula
        ? nextSpecs
        : nextSpecs.map((spec) =>
            spec.role === RoleEnum.FormulaInput
              ? { ...spec, primary: true }
              : spec,
          ),
    );
  };
  const setPrimary = (index: number) => {
    onChange(
      specs.map((spec, specIndex) =>
        spec.role === RoleEnum.FormulaInput
          ? { ...spec, primary: specIndex === index }
          : spec,
      ),
    );
  };
  return (
    <section className="grid gap-2" aria-label="Cost curve inputs">
      <div className="flex items-center justify-between gap-2">
        <Label>Curve inputs</Label>
        {!discreteFamily && (
          <Button
            type="button"
            size="sm"
            variant="outline"
            onClick={addFormulaInput}
          >
            <Plus className="size-4" aria-hidden="true" />
            Add input
          </Button>
        )}
      </div>
      <div className="grid gap-2">
        {specs.map((spec, index) => {
          const readOnly = discreteFamily;
          const fieldBase = `curve-driver-spec-${modeId}-${index}`;
          const formulaInput = spec.role === RoleEnum.FormulaInput;
          return (
            <div
              key={`${modeId}-${index}`}
              className="grid gap-3 rounded-md border p-3"
            >
              <div className="flex items-center justify-between gap-2">
                <Badge variant="secondary" size="xs" borderRadius="round">
                  {formulaInput ? "formula input" : "sizing input"}
                </Badge>
                <div className="flex items-center gap-2">
                  {formulaInput && (
                    <label className="flex items-center gap-2 text-xs text-muted-foreground">
                      <input
                        type="checkbox"
                        checked={Boolean(spec.primary)}
                        disabled={readOnly}
                        onChange={() => setPrimary(index)}
                      />
                      Primary
                    </label>
                  )}
                  {!readOnly && specs.length > 1 && (
                    <Button
                      type="button"
                      size="sm"
                      variant="ghost"
                      className="h-7 px-2 text-muted-foreground hover:text-destructive"
                      aria-label={`Remove ${spec.label || spec.key}`}
                      onClick={() => removeSpec(index)}
                    >
                      <Trash2 className="size-4" aria-hidden="true" />
                    </Button>
                  )}
                </div>
              </div>
              <div className="grid gap-3 md:grid-cols-2">
                <TextField
                  id={`${fieldBase}-label`}
                  label="Input label"
                  value={spec.label}
                  error={undefined}
                  onChange={(label) => updateSpec(index, { label })}
                  disabled={readOnly}
                />
                {formulaInput && (
                  <TextField
                    id={`${fieldBase}-symbol`}
                    label="Variable symbol"
                    value={spec.variable_symbol ?? ""}
                    error={undefined}
                    onChange={(variable_symbol) =>
                      updateSpec(index, { variable_symbol })
                    }
                    disabled={readOnly}
                  />
                )}
                <UnitSelectField
                  id={`${fieldBase}-unit`}
                  label={formulaInput ? "Input unit" : "Selector unit"}
                  unit={spec.unit}
                  units={unitOptionsForSpec(spec, defaultUnitOptions)}
                  disabled={readOnly}
                  onUnitChange={(unit) => updateSpec(index, { unit })}
                />
              </div>
            </div>
          );
        })}
      </div>
      <FieldError id={`curve-driver-specs-${modeId}-error`} message={error} />
    </section>
  );
}