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

55.81% Statements 24/43
57.89% Branches 11/19
50% Functions 6/12
61.53% Lines 24/39

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                  7x   7x 2x 2x 2x       7x         7x                 7x 4x 4x       7x                                                                       33x               45x   45x 2x 2x 2x   2x 2x       45x 2x 2x       45x                                                             33x                                                                      
import { Button } from "@/ahuora-design-system/ui/button";
 
import React, { useRef, forwardRef, useImperativeHandle } from "react";
interface Props {
  onUpload: (file: File) => void;
  className?: string;
}
 
export default function CSVUploader({ onUpload, className = "h-32" }: Props) {
  const fileInputRef = useRef<HTMLInputElement | null>(null);
 
  const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
    const file = e.target.files?.[0];
    if (file) {
      onUpload(file);
    }
  };
 
  const handleDragOver = (e: React.DragEvent<HTMLDivElement>) => {
    e.preventDefault();
    e.stopPropagation();
  };
 
  const handleDrop = (e: React.DragEvent<HTMLDivElement>) => {
    e.preventDefault();
    e.stopPropagation();
    const file = e.dataTransfer.files?.[0];
    Iif (file) {
      onUpload(file);
    }
  };
 
  const openFileDialog = () => {
    if (fileInputRef.current) {
      fileInputRef.current.click();
    }
  };
 
  return (
    <>
      <div className="w-full">
        <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=".csv, .xls, .xlsx"
            onChange={handleFileSelect}
            aria-label="Upload CSV/Excel file"
          />
          <span>Drag CSV file here, Or Click to Upload</span>
        </div>
      </div>
    </>
  );
}
 
interface Props {
  onUpload: (file: File) => void;
  fileExtension?: string;
  label?: string;
  className?: string;
  buttonText?: string;
  icon?: React.ReactNode;
  
}
 
export const CSVUploaderButton: React.FC<Props> = ({
  onUpload,
  fileExtension = ".csv",
  label = "upload-file",
  className = "",
  buttonText = "Upload CSV",
  icon,
}) => {
  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 (fileInputRef.current) {
      fileInputRef.current.click();
    }
  };
 
  return (
    <div className="mr-1">
      <Button
        variant="outline"
        className={`${className}`}
        onClick={openFileDialog}
      >{icon && <span className="">{icon}</span>}
        {buttonText}
      </Button>
      <input
        id="csv-upload"
        type="file"
        ref={fileInputRef}
        style={{ display: "none" }}
        accept={fileExtension}
        onChange={handleFileSelect}
        aria-label={label}
      />
    </div>
  );
};
 
 
interface Props {
  onUpload: (file: File) => void;
  fileExtension?: string;
  label?: string;
  className?: string;
  buttonText?: string;
}
 
export const CSVUploaderFile = forwardRef(({
  onUpload,
  fileExtension = ".csv",
  label = "upload-file",
  className = "",
}: Props, 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>
  );
});