File size: 4,982 Bytes
6242ddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useCallback, useRef } from 'react';
import { Upload, FileText, X } from 'lucide-react';

interface FileUploadProps {
  onUpload: (file: File, source?: string) => Promise<void>;
  loading?: boolean;
}

const ACCEPTED_TYPES = [
  '.csv',
  '.json',
  '.xlsx',
  '.xls',
  '.zip',
  'text/csv',
  'application/json',
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  'application/vnd.ms-excel',
  'application/zip',
];

export function FileUpload({ onUpload, loading }: FileUploadProps) {
  const [dragOver, setDragOver] = useState(false);
  const [selectedFile, setSelectedFile] = useState<File | null>(null);
  const [source, setSource] = useState('');
  const [error, setError] = useState<string | null>(null);
  const inputRef = useRef<HTMLInputElement>(null);

  const validateFile = (file: File): boolean => {
    const ext = '.' + file.name.split('.').pop()?.toLowerCase();
    if (!['.csv', '.json', '.xlsx', '.xls', '.zip'].includes(ext)) {
      setError(`Unsupported format: ${ext}. Use CSV, JSON, Excel, or ZIP.`);
      return false;
    }
    if (file.size > 500 * 1024 * 1024) {
      setError('File too large. Maximum 500MB.');
      return false;
    }
    setError(null);
    return true;
  };

  const handleDrop = useCallback((e: React.DragEvent) => {
    e.preventDefault();
    setDragOver(false);
    const file = e.dataTransfer.files[0];
    if (file && validateFile(file)) {
      setSelectedFile(file);
    }
  }, []);

  const handleFileSelect = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
    const file = e.target.files?.[0];
    if (file && validateFile(file)) {
      setSelectedFile(file);
    }
  }, []);

  const handleSubmit = async () => {
    if (!selectedFile) return;
    try {
      await onUpload(selectedFile, source || undefined);
      setSelectedFile(null);
      setSource('');
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Upload failed');
    }
  };

  const formatSize = (bytes: number) => {
    if (bytes < 1024) return `${bytes} B`;
    if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
    return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
  };

  return (
    <div>
      <div
        className={`upload-zone ${dragOver ? 'drag-over' : ''}`}
        onDragOver={(e) => { e.preventDefault(); setDragOver(true); }}
        onDragLeave={() => setDragOver(false)}
        onDrop={handleDrop}
        onClick={() => inputRef.current?.click()}
        role="button"
        tabIndex={0}
        aria-label="Upload file"
        onKeyDown={(e) => e.key === 'Enter' && inputRef.current?.click()}
      >
        <Upload size={32} style={{ color: 'var(--accent)' }} />
        <h3>Drop files here or click to browse</h3>
        <p>Supports CSV, JSON, Excel (.xlsx/.xls), and ZIP files up to 500MB</p>
        <p style={{ fontSize: 11, color: 'var(--text-muted)', marginTop: 8 }}>
          Files &gt;10MB will be uploaded in chunks automatically
        </p>
        <input
          ref={inputRef}
          type="file"
          accept={ACCEPTED_TYPES.join(',')}
          onChange={handleFileSelect}
          style={{ display: 'none' }}
          aria-hidden="true"
        />
      </div>

      {error && (
        <div className="alert alert-danger mt-4">
          <span>{error}</span>
          <button onClick={() => setError(null)} className="btn-icon" style={{ border: 'none', marginLeft: 'auto' }}>×</button>
        </div>
      )}

      {selectedFile && (
        <div className="card mt-4">
          <div className="card-body flex items-center justify-between">
            <div className="flex items-center gap-3">
              <FileText size={20} style={{ color: 'var(--accent)' }} />
              <div>
                <div style={{ fontWeight: 600, fontSize: 14 }}>{selectedFile.name}</div>
                <div style={{ fontSize: 12, color: 'var(--text-muted)' }}>{formatSize(selectedFile.size)}</div>
              </div>
            </div>
            <button className="btn-icon" onClick={() => setSelectedFile(null)} aria-label="Remove file">
              <X size={16} />
            </button>
          </div>
          <div className="card-body" style={{ paddingTop: 0 }}>
            <div className="filter-group" style={{ maxWidth: 300 }}>
              <label className="label" htmlFor="source-input">Data Source (optional)</label>
              <input
                id="source-input"
                className="input"
                placeholder="e.g., app_store, survey, support"
                value={source}
                onChange={(e) => setSource(e.target.value)}
              />
            </div>
            <button
              className="btn btn-primary mt-4"
              onClick={handleSubmit}
              disabled={loading}
            >
              {loading ? 'Uploading…' : 'Start Analysis'}
            </button>
          </div>
        </div>
      )}
    </div>
  );
}