File size: 9,452 Bytes
78ca83f
935fdc2
78ca83f
935fdc2
 
 
78ca83f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
935fdc2
89a3828
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import React, { useState, useEffect } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { Upload, FileText, Image, FileSpreadsheet, X, Sparkles, AlertCircle } from "lucide-react";
import { cn } from "@/lib/utils";
import { Input } from "@/components/ui/input";

// Allowed file types
const ALLOWED_TYPES = [
  "application/pdf",
  "image/png",
  "image/jpeg",
  "image/jpg",
  "image/tiff",
  "image/tif"
];

// Allowed file extensions (for fallback validation)
const ALLOWED_EXTENSIONS = [".pdf", ".png", ".jpg", ".jpeg", ".tiff", ".tif"];

// Maximum file size: 4 MB
const MAX_FILE_SIZE = 4 * 1024 * 1024; // 4 MB in bytes

export default function UploadZone({ onFileSelect, selectedFile, onClear, keyFields = "", onKeyFieldsChange = () => {} }) {
  const [isDragging, setIsDragging] = useState(false);
  const [error, setError] = useState(null);

  const validateFile = (file) => {
    // Reset error
    setError(null);

    // Check file type
    const fileExtension = "." + file.name.split(".").pop().toLowerCase();
    const isValidType = ALLOWED_TYPES.includes(file.type) || ALLOWED_EXTENSIONS.includes(fileExtension);
    
    if (!isValidType) {
      setError("Only PDF, PNG, JPG, and TIFF files are allowed.");
      return false;
    }

    // Check file size
    if (file.size > MAX_FILE_SIZE) {
      const fileSizeMB = (file.size / 1024 / 1024).toFixed(2);
      setError(`File size exceeds 4 MB limit. Your file is ${fileSizeMB} MB.`);
      return false;
    }

    return true;
  };

  const handleFileSelect = (file) => {
    if (validateFile(file)) {
      setError(null);
      onFileSelect(file);
    }
  };

  const handleDragOver = (e) => {
    e.preventDefault();
    setIsDragging(true);
  };

  const handleDragLeave = () => {
    setIsDragging(false);
  };

  const handleDrop = (e) => {
    e.preventDefault();
    setIsDragging(false);
    const file = e.dataTransfer.files[0];
    if (file) {
      handleFileSelect(file);
    }
  };

  const getFileIcon = (type) => {
    if (type?.includes("image")) return Image;
    if (type?.includes("spreadsheet") || type?.includes("excel")) return FileSpreadsheet;
    return FileText;
  };

  const FileIcon = selectedFile ? getFileIcon(selectedFile.type) : FileText;

  // Clear error when file is cleared
  useEffect(() => {
    if (!selectedFile) {
      setError(null);
    }
  }, [selectedFile]);

  return (
    <div className="w-full">
      <AnimatePresence mode="wait">
        {!selectedFile ? (
          <motion.div
            key="upload"
            initial={{ opacity: 0, y: 10 }}
            animate={{ opacity: 1, y: 0 }}
            exit={{ opacity: 0, y: -10 }}
            transition={{ duration: 0.2 }}
            onDragOver={handleDragOver}
            onDragLeave={handleDragLeave}
            onDrop={handleDrop}
            className={cn(
              "relative group cursor-pointer",
              "border-2 border-dashed rounded-2xl",
              "transition-all duration-300 ease-out",
              isDragging
                ? "border-indigo-400 bg-indigo-50/50"
                : "border-slate-200 hover:border-indigo-300 hover:bg-slate-50/50"
            )}
          >
            <label className="flex flex-col items-center justify-center py-16 px-8 cursor-pointer">
              <motion.div
                animate={isDragging ? { scale: 1.1, y: -5 } : { scale: 1, y: 0 }}
                className={cn(
                  "h-16 w-16 rounded-2xl flex items-center justify-center mb-6 transition-colors duration-300",
                  isDragging
                    ? "bg-indigo-100"
                    : "bg-gradient-to-br from-slate-100 to-slate-50 group-hover:from-indigo-100 group-hover:to-violet-50"
                )}
              >
                <Upload
                  className={cn(
                    "h-7 w-7 transition-colors duration-300",
                    isDragging ? "text-indigo-600" : "text-slate-400 group-hover:text-indigo-500"
                  )}
                />
              </motion.div>

              <div className="text-center">
                <p className="text-lg font-semibold text-slate-700 mb-1">
                  {isDragging ? "Drop your file here" : "Drop your file here, or browse"}
                </p>
                <p className="text-sm text-slate-400">
                  Supports PDF, PNG, JPG, TIFF up to 4MB
                </p>
              </div>

              <div className="flex items-center gap-2 mt-6">
                <div className="flex -space-x-1">
                  {[
                    "bg-red-100 text-red-600",
                    "bg-blue-100 text-blue-600",
                    "bg-green-100 text-green-600",
                    "bg-amber-100 text-amber-600",
                  ].map((color, i) => (
                    <div
                      key={i}
                      className={`h-8 w-8 rounded-lg ${color.split(" ")[0]} flex items-center justify-center border-2 border-white`}
                    >
                      <FileText className={`h-4 w-4 ${color.split(" ")[1]}`} />
                    </div>
                  ))}
                </div>
                <span className="text-xs text-slate-400 ml-2">Multiple formats supported</span>
              </div>

              <input
                type="file"
                className="hidden"
                accept=".pdf,.png,.jpg,.jpeg,.tiff,.tif"
                onChange={(e) => {
                  const file = e.target.files[0];
                  if (file) {
                    handleFileSelect(file);
                  }
                  // Reset input so same file can be selected again after error
                  e.target.value = "";
                }}
              />
            </label>

            {/* Decorative gradient border on hover */}
            <div className="absolute inset-0 -z-10 rounded-2xl bg-gradient-to-r from-indigo-500 via-violet-500 to-purple-500 opacity-0 group-hover:opacity-10 blur-xl transition-opacity duration-500" />
          </motion.div>
        ) : (
          <motion.div
            key="selected"
            initial={{ opacity: 0, scale: 0.95 }}
            animate={{ opacity: 1, scale: 1 }}
            exit={{ opacity: 0, scale: 0.95 }}
            className="grid grid-cols-1 lg:grid-cols-2 gap-3"
          >
            {/* File Info Box */}
            <div className="relative bg-gradient-to-br from-indigo-50 to-violet-50 rounded-xl p-3 border border-indigo-100">
              <div className="flex items-center gap-3">
                <div className="h-10 w-10 rounded-lg bg-white shadow-sm flex items-center justify-center flex-shrink-0">
                  <FileIcon className="h-5 w-5 text-indigo-600" />
                </div>
                <div className="flex-1 min-w-0">
                  <p className="font-medium text-slate-800 truncate text-sm">{selectedFile.name}</p>
                  <div className="flex items-center gap-2 text-xs text-slate-500">
                    <span>{(selectedFile.size / 1024 / 1024).toFixed(2)} MB</span>
                    <span className="text-indigo-500"></span>
                    <span className="text-indigo-600 flex items-center gap-1">
                      <Sparkles className="h-3 w-3" />
                      Ready for extraction
                    </span>
                  </div>
                </div>
                <button
                  onClick={onClear}
                  className="h-8 w-8 rounded-lg bg-white hover:bg-red-50 border border-slate-200 hover:border-red-200 flex items-center justify-center text-slate-400 hover:text-red-500 transition-colors"
                >
                  <X className="h-4 w-4" />
                </button>
              </div>
            </div>

            {/* Key Fields Box */}
            <div className="relative bg-white rounded-xl p-3 border border-slate-200">
              <label className="block text-xs font-medium text-slate-600 mb-1.5">
                <span className="font-bold">Key Fields</span> <span className="font-normal">(if required)</span>
              </label>
              <Input
                type="text"
                value={keyFields || ""}
                onChange={(e) => {
                  if (onKeyFieldsChange) {
                    onKeyFieldsChange(e.target.value);
                  }
                }}
                placeholder="Invoice Number, Invoice Date, PO Number, Supplier Name, Total Amount, Payment terms, Additional Notes"
                className="h-8 text-xs border-slate-200 focus:border-indigo-300 focus:ring-indigo-200"
              />
            </div>
          </motion.div>
        )}
      </AnimatePresence>
      
      {/* Error Message */}
      {error && (
        <motion.div
          initial={{ opacity: 0, y: -10 }}
          animate={{ opacity: 1, y: 0 }}
          exit={{ opacity: 0, y: -10 }}
          className="mt-3 p-3 bg-red-50 border border-red-200 rounded-xl flex items-start gap-2"
        >
          <AlertCircle className="h-4 w-4 text-red-600 flex-shrink-0 mt-0.5" />
          <p className="text-sm text-red-700 flex-1">{error}</p>
          <button
            onClick={() => setError(null)}
            className="text-red-600 hover:text-red-800 transition-colors"
          >
            <X className="h-4 w-4" />
          </button>
        </motion.div>
      )}
    </div>
  );
}