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

88.88% Statements 24/27
51.78% Branches 29/56
60% Functions 3/5
93.75% Lines 15/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                        393x         393x 393x 393x 393x 393x                           393x   393x 393x   393x                                                                                                                                                               3903x     169x     54x                                           555x 731x      
import { Maximize2, X } from "lucide-react";
import { useState } from "react";
import {
  AlertDialog,
  AlertDialogContent,
  AlertDialogHeader,
} from "@/ahuora-design-system/ui/alert-dialog";
import { Button } from "@/ahuora-design-system/ui/button";
import { ToolTipCover } from "@/ahuora-design-system/ui/tooltip";
import { cn } from "@/lib/utils";
import DataViewer from "../../../multi-steady-state/DataViewer";
 
export function PreviewData({
  hasFile,
  onRemove,
  fileName,
  optimization,
  canMutate = true,
  emptyLabel = "No File Chosen",
  previewAriaLabel = "Preview file data",
  removeTooltip = "Remove file",
  triggerVariant = "pill",
  triggerClassName,
}: {
  hasFile: boolean;
  onRemove: () => void;
  fileName: string;
  optimization: { id: number };
  canMutate?: boolean;
  emptyLabel?: string;
  previewAriaLabel?: string;
  removeTooltip?: string;
  triggerVariant?: "pill" | "button" | "icon";
  triggerClassName?: string;
}) {
  const [isPreviewOpen, setIsPreviewOpen] = useState(false);
 
  const handleOpen = () => setIsPreviewOpen(true);
  const handleClose = () => setIsPreviewOpen(false);
  const trigger =
    triggerVariant === "icon" ? (
      hasFile ? (
        <ToolTipCover content={fileName} asChild>
          <Button
            type="button"
            variant="outline"
            size="sm"
            onClick={handleOpen}
            aria-label={previewAriaLabel}
            className={cn("h-7 w-7 shrink-0 px-0", triggerClassName)}
          >
            <Maximize2 className="h-4 w-4" />
          </Button>
        </ToolTipCover>
      ) : null
    ) : triggerVariant === "button" ? (
      hasFile ? (
        <Button
          type="button"
          variant="outline"
          size="sm"
          onClick={handleOpen}
          aria-label={previewAriaLabel}
          className={cn(
            "rounded-md border border-input bg-background text-foreground",
            triggerClassName,
          )}
        >
          {fileName}
        </Button>
      ) : (
        <span
          className={cn(
            "min-w-0 truncate px-2 text-sm text-muted-foreground",
            triggerClassName,
          )}
        >
          {emptyLabel}
        </span>
      )
    ) : (
      <div
        className={cn(
          "inline-flex max-w-full items-center px-2 py-1 bg-secondary rounded-full gap-2",
          triggerClassName,
        )}
      >
        {!hasFile ? (
          <span className="text-sm truncate select-none">{emptyLabel}</span>
        ) : (
          <>
            <button
              className="text-sm truncate bg-transparent border-0 p-0 cursor-pointer hover:text-primary focus:outline-none"
              style={{ textDecoration: "none" }}
              onClick={handleOpen}
              type="button"
              aria-label={previewAriaLabel}
              tabIndex={0}
            >
              {fileName}
            </button>
            <ToolTipCover content="Maximise" asChild>
              <Maximize2
                size={17}
                className="cursor-pointer hover:stroke-zinc-400"
                onClick={handleOpen}
              />
            </ToolTipCover>
            {canMutate && (
              <ToolTipCover content={removeTooltip} asChild>
                <X
                  size={18}
                  className="cursor-pointer hover:stroke-rose-700"
                  onClick={onRemove}
                />
              </ToolTipCover>
            )}
          </>
        )}
      </div>
    );
 
  return (
    <>
      {trigger}
 
      {hasFile && (
        <AlertDialog open={isPreviewOpen} onOpenChange={setIsPreviewOpen}>
          <AlertDialogContent className="flex h-[calc(100vh-6rem)] max-h-[42rem] w-[calc(100vw-2rem)] max-w-6xl flex-col overflow-hidden p-6">
            <AlertDialogHeader>
              <div className="flex items-center justify-between pb-4">
                <h3 className="text-base font-semibold truncate max-w-[90%]">
                  Preview
                </h3>
                <button
                  onClick={handleClose}
                  className="text-muted-foreground hover:text-foreground transition"
                  aria-label="close-file-viewer"
                >
                  <X size={18} />
                </button>
              </div>
            </AlertDialogHeader>
            <div className="min-h-0 flex-1 overflow-hidden">
              <DataViewer scenario={optimization.id} />
            </div>
          </AlertDialogContent>
        </AlertDialog>
      )}
    </>
  );
}