All files / src/pages/flowsheet-page/multi-steady-state CSVUploader.tsx

77.87% Statements 88/113
56.25% Branches 54/96
62.5% Functions 5/8
80.85% Lines 76/94

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                                  91x   91x 91x 91x 91x   91x   91x   91x       7x   7x     7x   273x   91x           98x   91x                   273x   91x         2x   98x     91x       91x         91x           91x       91x       364x 98x 637x     273x                             396x   396x 396x 396x 396x     396x 396x 396x   396x   396x 8x   8x     8x   792x   396x         8x   422x       26x   396x           396x           422x   578x 448x 396x       396x       1584x 1188x                             103x       116x 116x   116x 116x 116x       116x   116x 4x 4x 4x 232x     116x   4x   116x     116x 116x       116x       464x 348x         103x  
import React, { forwardRef, useImperativeHandle, useRef } from "react";
import {
  AccessControlledButton,
  AccessControlledProps,
} from "@/ahuora-design-system/ui/accessControlled";
import { ToolTipCover } from "@/ahuora-design-system/ui/tooltip";
 
interface CSVUploaderProps {
  onUpload: (file: File) => void;
  className?: string;
  accept?: string;
  promptText?: string;
  inputAriaLabel?: string;
  tooltipContent?: string;
  disabled?: boolean;
}
 
export default function CSVUploader({
  onUpload,
  className = "h-32",
  accept = ".csv,text/csv",
  promptText = "Drag a CSV file here, or click to upload",
  inputAriaLabel = "Upload CSV file",
  tooltipContent,
  disabled = false,
}: CSVUploaderProps) {
  const fileInputRef = useRef<HTMLInputElement | null>(null);
 
  const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
    if (disabled) {
      return;
    }
    const file = e.target.files?.[0];
    if (file) {
      onUpload(file);
    }
    if (fileInputRef.current) {
      fileInputRef.current.value = "";
    }
  };
 
  const handleDragOver = (e: React.DragEvent<HTMLDivElement>) => {
    if (disabled) {
      return;
    }
    e.preventDefault();
    e.stopPropagation();
  };
 
  const handleDrop = (e: React.DragEvent<HTMLDivElement>) => {
    if (disabled) {
      return;
    }
    e.preventDefault();
    e.stopPropagation();
    const file = e.dataTransfer.files?.[0];
    if (file) {
      onUpload(file);
    }
  };
 
  const openFileDialog = () => {
    if (disabled) {
      return;
    }
    if (fileInputRef.current) {
      fileInputRef.current.click();
    }
  };
 
  return (
    <>
      <div className="w-full">
        <ToolTipCover
          asChild
          content={tooltipContent ?? ""}
          delay={150}
          side="top"
        >
          <div
            className={`w-full border-2 border-dashed border-muted flex items-center justify-center cursor-pointer ${className}`}
            onDragOver={handleDragOver}
            onDrop={handleDrop}
            onClick={openFileDialog}
            aria-label="CSV file drop area"
          >
            <input
              id="csv-upload"
              type="file"
              ref={fileInputRef}
              style={{ display: "none" }}
              accept={accept}
              onChange={handleFileSelect}
              aria-label={inputAriaLabel}
            />
            <span>{promptText}</span>
          </div>
        </ToolTipCover>
      </div>
    </>
  );
}
 
interface CSVUploaderButtonProps extends AccessControlledProps {
  onUpload: (file: File) => void;
  fileExtension?: string;
  label?: string;
  className?: string;
  buttonText?: string;
  icon?: React.ReactNode;
  tooltipContent?: string;
  disabled?: boolean;
}
 
export const CSVUploaderButton: React.FC<CSVUploaderButtonProps> = ({
  onUpload,
  fileExtension = ".csv,text/csv",
  label = "upload-file",
  className = "",
  buttonText = "Upload CSV",
  icon,
  tooltipContent,
  disabled = false,
  writeAccessOnly = true,
  hideIfNoAccess = false,
}) => {
  const fileInputRef = useRef<HTMLInputElement | null>(null);
 
  const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
    const file = e.target.files?.[0];
    if (file) {
      onUpload(file);
    }
    if (fileInputRef.current) {
      fileInputRef.current.value = "";
    }
  };
 
  const openFileDialog = () => {
    if (disabled) {
      return;
    }
    if (fileInputRef.current) {
      fileInputRef.current.click();
    }
  };
 
  return (
    <div className="mr-1">
      <ToolTipCover
        asChild
        content={tooltipContent ?? ""}
        delay={150}
        side="top"
      >
        <AccessControlledButton
          variant="outline"
          className={`${className}`}
          onClick={openFileDialog}
          disabled={disabled}
          writeAccessOnly={writeAccessOnly}
          hideIfNoAccess={hideIfNoAccess}
        >
          {icon && <span className="">{icon}</span>}
          {buttonText}
        </AccessControlledButton>
      </ToolTipCover>
      <input
        id="csv-upload"
        type="file"
        ref={fileInputRef}
        style={{ display: "none" }}
        accept={fileExtension}
        onChange={handleFileSelect}
        aria-label={label}
      />
    </div>
  );
};
 
export type CSVUploaderFileRef = {
  openFileDialog: () => void;
};
 
interface CSVUploaderFileProps {
  onUpload: (file: File) => void;
  fileExtension?: string;
  label?: string;
  className?: string;
}
 
export const CSVUploaderFile = forwardRef<
  CSVUploaderFileRef,
  CSVUploaderFileProps
>(
  (
    {
      onUpload,
      fileExtension = ".csv,text/csv",
      label = "upload-file",
      className = "",
    }: CSVUploaderFileProps,
    ref,
  ) => {
    const fileInputRef = useRef<HTMLInputElement | null>(null);
 
    const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
      const file = e.target.files?.[0];
      Iif (file) onUpload(file);
      Iif (fileInputRef.current) fileInputRef.current.value = "";
    };
 
    // Expose openFileDialog to parent via ref
    useImperativeHandle(ref, () => ({
      openFileDialog: () => {
        Iif (fileInputRef.current) fileInputRef.current.click();
      },
    }));
 
    return (
      <div className={className}>
        <input
          id="csv-upload-file"
          type="file"
          ref={fileInputRef}
          style={{ display: "none" }}
          accept={fileExtension}
          onChange={handleFileSelect}
          aria-label={label}
        />
      </div>
    );
  },
);
 
CSVUploaderFile.displayName = "CSVUploaderFile";