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 | 93x 93x 113x 113x 93x 10x 10x 279x 93x 372x 93x 279x 93x 279x 93x 279x 186x 93x 93x 93x 93x 93x 5x 5x 930x 186x 103x 372x | import { Plus, Trash2 } from "lucide-react";
import { Badge } from "@/ahuora-design-system/ui/badge";
import { Button } from "@/ahuora-design-system/ui/button";
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 showRoleBadge = new Set(specs.map((spec) => spec.role)).size > 1;
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">
<h4 className="text-sm font-medium">Inputs</h4>
{!discreteFamily && (
<Button
type="button"
size="sm"
variant="outline"
onClick={addFormulaInput}
>
<Plus className="size-4" aria-hidden="true" />
Add input
</Button>
)}
</div>
<div className="grid divide-y">
{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 py-3 first:pt-0 last:pb-0"
>
<div
className={
showRoleBadge || (!readOnly && specs.length > 1)
? "flex items-center justify-between gap-2"
: "hidden"
}
>
{showRoleBadge ? (
<Badge variant="secondary" size="xs" borderRadius="round">
{formulaInput ? "formula input" : "sizing input"}
</Badge>
) : (
<span />
)}
<div className="flex items-center gap-2">
{formulaInput && formulaSpecs.length > 1 && (
<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 ${formulaInput ? "md:grid-cols-3" : "md:grid-cols-2"}`}
>
<TextField
id={`${fieldBase}-label`}
label="Label"
value={spec.label}
error={undefined}
onChange={(label) => updateSpec(index, { label })}
disabled={readOnly}
/>
{formulaInput && (
<TextField
id={`${fieldBase}-symbol`}
label="Symbol"
value={spec.variable_symbol ?? ""}
error={undefined}
onChange={(variable_symbol) =>
updateSpec(index, { variable_symbol })
}
disabled={readOnly}
/>
)}
<UnitSelectField
id={`${fieldBase}-unit`}
label="Unit"
ariaLabel="Unit"
unit={spec.unit}
units={unitOptionsForSpec(spec, defaultUnitOptions)}
disabled={readOnly}
layout="field"
onUnitChange={(unit) => updateSpec(index, { unit })}
/>
</div>
</div>
);
})}
</div>
<FieldError id={`curve-driver-specs-${modeId}-error`} message={error} />
</section>
);
}
|