All files / src/pages/flowsheet-page/economics/cost-items-panel/unit-costs/category-group BulkSetupPanel.tsx

0% Statements 0/19
0.75% Branches 1/132
0% Functions 0/4
0% Lines 0/16

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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
import { CheckCircle2, ChevronDown } from "lucide-react";
import { Badge } from "@/ahuora-design-system/ui/badge";
import { Button } from "@/ahuora-design-system/ui/button";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/ahuora-design-system/ui/select";
import type {
  BulkEquipmentSetupResult,
  CapitalCostLineRead,
  CostCurveRead,
} from "@/api/apiStore.gen";
import { cn } from "@/lib/utils";
import type { RequiredDriverSpec } from "../../../cost-curves/model/driverSpecs";
import { costBasisLabel } from "../../model/capitalLineHelpers";
import type { UnitCostRow } from "../../model/capitalLineTypes";
import { FieldLabel } from "../../ui/CapitalLineUi";
import { BulkCustomSelectionTable } from "./BulkCustomSelectionTable";
import { BulkDriverInputsEditor } from "./BulkDriverInputsEditor";
import { BulkWarningBadge } from "./BulkWarningBadge";
import type { BulkApplyState, BulkDriverInputMode } from "./bulkSetupHelpers";
import { BulkTargetMode } from "./bulkSetupHelpers";
 
export function BulkSetupPanel({
  groupLabel,
  controlIdPrefix,
  open,
  selectedCurveId,
  selectedCurve,
  groupCurves,
  targetMode,
  selectedDriverSpecs,
  driverInputModes,
  driverInputManualValues,
  selectedMappingsCount,
  compatibleTargetsCount,
  incompatibleTargetCount,
  overwriteCount,
  driverInputReadyCount,
  driverInputPreservedCount,
  driverInputOverwriteCount,
  driverInputMissingCount,
  previewLoading,
  previewError,
  applyState,
  applyDisabled,
  assignableRows,
  curves,
  capitalLines,
  previewResultsByMapping,
  customSelection,
  canEdit,
  busy,
  onToggleOpen,
  onCurveChange,
  onTargetChange,
  onDriverInputModesChange,
  onDriverInputManualValuesChange,
  onCustomSelectionChange,
  onApply,
}: {
  groupLabel: string;
  controlIdPrefix: string;
  open: boolean;
  selectedCurveId: string;
  selectedCurve?: CostCurveRead;
  groupCurves: CostCurveRead[];
  targetMode: BulkTargetMode;
  selectedDriverSpecs: RequiredDriverSpec[];
  driverInputModes: Record<string, BulkDriverInputMode>;
  driverInputManualValues: Record<string, string>;
  selectedMappingsCount: number;
  compatibleTargetsCount: number;
  incompatibleTargetCount: number;
  overwriteCount: number;
  driverInputReadyCount: number;
  driverInputPreservedCount: number;
  driverInputOverwriteCount: number;
  driverInputMissingCount: number;
  previewLoading: boolean;
  previewError: string;
  applyState: BulkApplyState;
  applyDisabled: boolean;
  assignableRows: UnitCostRow[];
  curves: CostCurveRead[];
  capitalLines: CapitalCostLineRead[];
  previewResultsByMapping: Map<number, BulkEquipmentSetupResult>;
  customSelection: Record<number, boolean>;
  canEdit: boolean;
  busy: boolean;
  onToggleOpen: () => void;
  onCurveChange: (curveId: string) => void;
  onTargetChange: (target: BulkTargetMode) => void;
  onDriverInputModesChange: (
    modes: Record<string, BulkDriverInputMode>,
  ) => void;
  onDriverInputManualValuesChange: (
    manualValues: Record<string, string>,
  ) => void;
  onCustomSelectionChange: (customSelection: Record<number, boolean>) => void;
  onApply: () => void;
}) {
  return (
    <>
      <button
        type="button"
        className="mt-3 flex w-full min-w-0 items-center justify-between gap-3 rounded-md border px-3 py-2 text-left text-sm hover:bg-muted/30"
        aria-label={`Configure multiple units for ${groupLabel}`}
        aria-expanded={open}
        aria-controls={`${controlIdPrefix}-bulk-setup`}
        onClick={onToggleOpen}
      >
        <span className="min-w-0">
          <span className="block font-medium">Configure multiple units</span>
          <span className="block truncate text-xs text-muted-foreground">
            Apply a compatible curve and sizing setup across this category
          </span>
        </span>
        <ChevronDown
          className={cn(
            "size-4 shrink-0 text-muted-foreground transition-transform",
            open ? "rotate-180" : null,
          )}
          aria-hidden="true"
        />
      </button>
      {open && (
        <div
          id={`${controlIdPrefix}-bulk-setup`}
          className="mt-2 grid gap-3 rounded-md border bg-muted/10 p-3"
          aria-label={`Bulk setup for ${groupLabel}`}
        >
          <div className="grid gap-3 sm:grid-cols-2 sm:items-end">
            <div className="grid gap-1">
              <FieldLabel
                htmlFor={`${controlIdPrefix}-curve`}
                label="Cost curve"
                help="Only curves compatible with at least one unit in this category are shown."
              />
              <Select
                value={selectedCurveId}
                disabled={!canEdit || busy || groupCurves.length === 0}
                onValueChange={onCurveChange}
              >
                <SelectTrigger
                  id={`${controlIdPrefix}-curve`}
                  aria-label={`Bulk cost curve for ${groupLabel}`}
                >
                  <SelectValue placeholder="Select cost curve" />
                </SelectTrigger>
                <SelectContent>
                  {groupCurves.map((curve) => (
                    <SelectItem key={curve.id} value={String(curve.id)}>
                      {curve.name} ยท {costBasisLabel(curve.cost_basis)}
                    </SelectItem>
                  ))}
                </SelectContent>
              </Select>
            </div>
            <div className="grid gap-1">
              <FieldLabel
                htmlFor={`${controlIdPrefix}-target`}
                label="Apply to"
                help="Missing setup targets enabled units missing either a curve or sizing value; all enabled overwrites compatible curve assignments."
              />
              <Select
                value={targetMode}
                disabled={!canEdit || busy}
                onValueChange={(value) =>
                  onTargetChange(value as BulkTargetMode)
                }
              >
                <SelectTrigger
                  id={`${controlIdPrefix}-target`}
                  aria-label={`Bulk apply target for ${groupLabel}`}
                >
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value={BulkTargetMode.Missing}>
                    Missing setup
                  </SelectItem>
                  <SelectItem value={BulkTargetMode.Included}>
                    All enabled
                  </SelectItem>
                  <SelectItem value={BulkTargetMode.Custom}>
                    Custom selection
                  </SelectItem>
                </SelectContent>
              </Select>
            </div>
          </div>
          {selectedCurve && (
            <BulkDriverInputsEditor
              groupLabel={groupLabel}
              specs={selectedDriverSpecs}
              modes={driverInputModes}
              manualValues={driverInputManualValues}
              canEdit={canEdit}
              busy={busy}
              onModesChange={onDriverInputModesChange}
              onManualValuesChange={onDriverInputManualValuesChange}
            />
          )}
          <Button
            type="button"
            className="h-7 w-56 justify-center px-3 text-xs"
            disabled={applyDisabled}
            onClick={onApply}
          >
            <CheckCircle2 className="size-3.5" aria-hidden="true" />
            Apply setup to {selectedMappingsCount}{" "}
            {selectedMappingsCount === 1 ? "unit" : "units"}
          </Button>
          {groupCurves.length === 0 && (
            <div className="text-xs text-muted-foreground">
              No matching cost curves are available for this category yet.
            </div>
          )}
          {selectedCurve && (
            <div className="grid gap-2 rounded-md border bg-background/70 p-2 text-xs">
              <div className="flex flex-wrap items-center gap-2">
                <Badge variant="muted" size="xs" borderRadius="round">
                  {compatibleTargetsCount} matching targets
                </Badge>
                {incompatibleTargetCount > 0 && (
                  <Badge variant="muted" size="xs" borderRadius="round">
                    {incompatibleTargetCount} skipped by category/subtype
                  </Badge>
                )}
                {overwriteCount > 0 && (
                  <BulkWarningBadge>
                    {overwriteCount} existing curve
                    {overwriteCount === 1 ? "" : "s"} will be overwritten
                  </BulkWarningBadge>
                )}
                {previewLoading && (
                  <Badge variant="muted" size="xs" borderRadius="round">
                    Checking setup
                  </Badge>
                )}
                {driverInputReadyCount > 0 && (
                  <Badge variant="success" size="xs" borderRadius="round">
                    {driverInputReadyCount} driver input
                    {driverInputReadyCount === 1 ? "" : "s"} ready
                  </Badge>
                )}
                {driverInputPreservedCount > 0 && (
                  <Badge variant="muted" size="xs" borderRadius="round">
                    {driverInputPreservedCount} existing input
                    {driverInputPreservedCount === 1 ? "" : "s"} preserved
                  </Badge>
                )}
                {driverInputOverwriteCount > 0 && (
                  <BulkWarningBadge>
                    {driverInputOverwriteCount} driver input
                    {driverInputOverwriteCount === 1 ? "" : "s"} will be
                    overwritten
                  </BulkWarningBadge>
                )}
                {driverInputMissingCount > 0 && (
                  <BulkWarningBadge>
                    {driverInputMissingCount} driver input
                    {driverInputMissingCount === 1 ? "" : "s"} need attention
                  </BulkWarningBadge>
                )}
              </div>
              {previewError && (
                <div className="text-xs text-destructive" role="alert">
                  {previewError}
                </div>
              )}
            </div>
          )}
          {targetMode === BulkTargetMode.Custom && (
            <BulkCustomSelectionTable
              rows={assignableRows}
              curves={curves}
              selectedCurve={selectedCurve}
              capitalLines={capitalLines}
              previewResultsByMapping={previewResultsByMapping}
              customSelection={customSelection}
              canEdit={canEdit}
              busy={busy}
              onSelectionChange={onCustomSelectionChange}
            />
          )}
          {applyState.kind !== "idle" && (
            <div
              className={
                applyState.kind === "error"
                  ? "text-xs text-destructive"
                  : "text-xs text-muted-foreground"
              }
              role={applyState.kind === "error" ? "alert" : "status"}
            >
              {applyState.message}
            </div>
          )}
        </div>
      )}
    </>
  );
}