All files / src/flowsheet-cloning DuplicateWithResultsDialog.tsx

88.46% Statements 46/52
56.81% Branches 25/44
50% Functions 3/6
94.44% Lines 34/36

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                                              2x               2x 2x 2x   2x     2x         4x     1x 3x 1x 2x 3x 4x     2x         1x 2x   8x   1x   2x 1x 3x 3x 4x 6x   2x 1x             4x 2x     6x   2x 8x 6x 10x   6x      
import { useState } from "react";
import { Button } from "@/ahuora-design-system/ui/button";
import { Checkbox } from "@/ahuora-design-system/ui/checkbox";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/ahuora-design-system/ui/dialog";
import type { ScenarioResultCopySummary } from "@/api/apiStore.gen";
 
type DuplicateWithResultsDialogProps = {
  name: string;
  scope: "flowsheet" | "project";
  summary: ScenarioResultCopySummary;
  isDuplicating: boolean;
  onCancel: () => void;
  onConfirm: (includeScenarioResults: boolean) => Promise<void> | void;
};
 
/** Confirm a full duplicate only when retained scenario results offer a choice. */
export function DuplicateWithResultsDialog({
  name,
  scope,
  summary,
  isDuplicating,
  onCancel,
  onConfirm,
}: DuplicateWithResultsDialogProps) {
  const [includeScenarioResults, setIncludeScenarioResults] = useState(false);
  const checkboxId = `duplicate-${scope}-scenario-results`;
  const rowLabel = summary.row_count === 1 ? "row" : "rows";
  const propertyLabel =
    summary.property_count === 1 ? "property" : "properties";
 
  return (
    <div onClick={(event) => event.stopPropagation()}>
      <Dialog
        open
        onOpenChange={(open) => {
          Iif (!open && !isDuplicating) onCancel();
        }}
      >
        <DialogContent preventDefault={isDuplicating}>
          <DialogHeader>
            <DialogTitle>{`Duplicate ${scope}?`}</DialogTitle>
            <DialogDescription>
              {`Create a full copy of ${name}. Scenario results are excluded unless you choose to save them.`}
            </DialogDescription>
          </DialogHeader>
 
          <div className="flex items-start gap-3 rounded-md border bg-secondary/15 p-3">
            <Checkbox
              id={checkboxId}
              aria-label="Save all scenario results"
              checked={includeScenarioResults}
              disabled={isDuplicating}
              onCheckedChange={(checked) =>
                setIncludeScenarioResults(checked === true)
              }
            />
            <label htmlFor={checkboxId} className="min-w-0 cursor-pointer">
              <span className="block font-medium">
                Save all scenario results
              </span>
              <span className="mt-0.5 block text-sm text-muted-foreground">
                {`${summary.row_count.toLocaleString()} ${rowLabel} across ${summary.property_count.toLocaleString()} ${propertyLabel}`}
              </span>
            </label>
          </div>
 
          <DialogFooter>
            <Button
              type="button"
              variant="outline"
              disabled={isDuplicating}
              onClick={onCancel}
            >
              Cancel
            </Button>
            <Button
              type="button"
              disabled={isDuplicating}
              onClick={() => void onConfirm(includeScenarioResults)}
            >
              {isDuplicating ? "Duplicating..." : "Duplicate"}
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </div>
  );
}