All files / src/pages/flowsheet-page/flowsheet/LeftSideBar/Scenarios ScenarioCard.tsx

93.33% Statements 28/30
67.56% Branches 25/37
66.66% Functions 2/3
96.15% Lines 25/26

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                                          10x             10x 10x 10x 10x 10x   10x 1x   1x 1x       22x               16x         1x 1x                                                                   1x             64x 28x                 10x     6x   16x 6x   16x 22x   24x 30x      
import { ChevronRight, Trash2 } from "lucide-react";
import { useState } from "react";
import { AccessControlledButton as Button } from "@/ahuora-design-system/ui/accessControlled";
import {
  AlertDialog,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/ahuora-design-system/ui/alert-dialog";
import { Badge } from "@/ahuora-design-system/ui/badge";
import {
  ScenarioRead,
  useCoreScenarioDestroyMutation,
} from "@/api/apiStore.gen";
import { useFlowsheetAccess } from "@/hooks/flowsheetAccess";
import { ScenarioNameBadge } from "./ScenarioNameBadge";
 
export function ScenarioCard({
  scenario,
  onClick,
}: {
  scenario: ScenarioRead;
  onClick?: () => void;
}) {
  const access = useFlowsheetAccess();
  const [open, setOpen] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [deleteScenario, { isLoading: isDeleting }] =
    useCoreScenarioDestroyMutation();
 
  const handleDelete = async () => {
    setError(null);
    try {
      await deleteScenario({ id: scenario.id }).unwrap();
      setOpen(false);
    } catch {
      setError("Could not delete the scenario.");
    }
  };
 
  return (
    <div
      className="flex flex-col rounded-lg bg-muted  "
      aria-label="scenario-card-container"
    >
      <div className="flex flex-row justify-between items-center bg-accent rounded-t-lg p-2">
        <ScenarioNameBadge scenario={scenario} />
        {access?.can_edit !== false && (
          <AlertDialog
            open={open}
            onOpenChange={(nextOpen) => {
              setOpen(nextOpen);
              Iif (nextOpen) setError(null);
            }}
          >
            <AlertDialogTrigger asChild>
              <Button
                type="button"
                size="xs"
                variant="ghost"
                aria-label={`Delete scenario ${scenario.displayName}`}
                className="text-muted-foreground hover:text-destructive"
              >
                <Trash2 className="size-4" aria-hidden="true" />
              </Button>
            </AlertDialogTrigger>
            <AlertDialogContent className="sm:max-w-md">
              <AlertDialogHeader>
                <AlertDialogTitle>Delete scenario?</AlertDialogTitle>
                <AlertDialogDescription>
                  Delete {scenario.displayName}. This action cannot be undone.
                </AlertDialogDescription>
              </AlertDialogHeader>
              {error && (
                <p className="text-xs text-destructive" role="alert">
                  {error}
                </p>
              )}
              <AlertDialogFooter>
                <AlertDialogCancel disabled={isDeleting}>
                  Cancel
                </AlertDialogCancel>
                <Button
                  type="button"
                  variant="destructive"
                  disabled={isDeleting}
                  onClick={() => void handleDelete()}
                >
                  {isDeleting ? "Deleting scenario" : "Delete scenario"}
                </Button>
              </AlertDialogFooter>
            </AlertDialogContent>
          </AlertDialog>
        )}
      </div>
 
      <div
        className="flex flex-col p-2.5 hover:shadow-lg cursor-pointer "
        onClick={onClick}
      >
        <div className="flex flex-row justify-between items-center   ">
          <p></p>
          <ChevronRight size={24} />{" "}
        </div>
 
        <div className="flex flex-row gap-2 p-2  items-center   ">
          {scenario.enable_dynamics && (
            <Badge variant="secondary">Dynamic</Badge>
          )}
          {scenario.enable_optimization && (
            <Badge variant="secondary">Optimisation</Badge>
          )}
        </div>
        {/* {optimization.enable_optimization && <Badge variant="secondary">Multi Steady State</Badge>} */}
      </div>
    </div>
  );
}