File size: 3,240 Bytes
609c821
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useCallback, useState } from 'react';
import { Icons } from '../constants';

interface FileUploadProps {
  onFileSelect: (file: File) => void;
  disabled: boolean;
}

const FileUpload: React.FC<FileUploadProps> = ({ onFileSelect, disabled }) => {
  const [isDragging, setIsDragging] = useState(false);

  const handleDragOver = useCallback((e: React.DragEvent) => {
    e.preventDefault();
    if (!disabled) setIsDragging(true);
  }, [disabled]);

  const handleDragLeave = useCallback((e: React.DragEvent) => {
    e.preventDefault();
    if (!disabled) setIsDragging(false);
  }, [disabled]);

  const handleDrop = useCallback((e: React.DragEvent) => {
    e.preventDefault();
    if (disabled) return;
    setIsDragging(false);

    const files = e.dataTransfer.files;
    if (files.length > 0) {
      const file = files[0];
      if (validateFile(file)) {
        onFileSelect(file);
      }
    }
  }, [disabled, onFileSelect]);

  const handleFileInput = (e: React.ChangeEvent<HTMLInputElement>) => {
    if (disabled || !e.target.files) return;
    const file = e.target.files[0];
    if (file && validateFile(file)) {
      onFileSelect(file);
    }
  };

  const validateFile = (file: File) => {
    const validTypes = ['application/pdf', 'image/jpeg', 'image/png', 'image/webp'];
    // For browser based base64, we limit to ~20MB strictly to prevent browser crash.
    // In a real app with File API, this could be 2GB.
    const maxSize = 50 * 1024 * 1024;

    if (!validTypes.includes(file.type)) {
      alert("Invalid file type. Please upload PDF, JPEG, PNG, or WEBP.");
      return false;
    }
    if (file.size > maxSize) {
      alert("File too large (Max 50MB).");
      return false;
    }
    return true;
  };

  return (
    <div
      className={`relative border-2 border-dashed rounded-xl p-12 transition-all duration-300 flex flex-col items-center justify-center cursor-pointer group
        ${isDragging
          ? 'border-petro-500 bg-petro-900/20'
          : 'border-industrial-800 hover:border-petro-600/50 hover:bg-industrial-900'
        }
        ${disabled ? 'opacity-50 cursor-not-allowed' : ''}
      `}
      onDragOver={handleDragOver}
      onDragLeave={handleDragLeave}
      onDrop={handleDrop}
      onClick={() => !disabled && document.getElementById('fileInput')?.click()}
    >
      <input
        type="file"
        id="fileInput"
        className="hidden"
        onChange={handleFileInput}
        accept=".pdf,.jpg,.jpeg,.png,.webp"
        disabled={disabled}
      />

      <div className={`p-4 rounded-full mb-4 transition-colors ${isDragging ? 'bg-petro-500/20 text-petro-400' : 'bg-industrial-800 text-gray-400 group-hover:text-petro-400 group-hover:bg-petro-900/20'}`}>
        <Icons.Upload />
      </div>

      <h3 className="text-xl font-display font-semibold text-gray-200 mb-2">
        Upload Document
      </h3>
      <p className="text-gray-400 text-center max-w-sm">
        Drag & drop or click to select Oil & Gas reports, drilling logs, or production data.
      </p>
      <p className="text-xs text-gray-500 mt-4 uppercase tracking-wider">
        Supported: PDF, JPG, PNG (Max 50MB)
      </p>
    </div>
  );
};

export default FileUpload;