File size: 12,224 Bytes
ff0e173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
'use client';

import * as React from 'react';
import UploadedFileCard from './UploadedFileCard';
import FileTypeIcon from './FileTypeIcon';
import { KBFile } from '@/lib/kb-data';

interface FileUploadPanelProps {
  files: KBFile[];
  onUpload: (file: File) => Promise<void>;
  onDelete: (id: string) => void;
  deletingIds: Set<string>;
}

type UploadStatus = 'uploading' | 'done' | 'error';

interface PendingUpload {
  id: string;
  name: string;
  file: File;
  progress: number;
  status: UploadStatus;
}

let uploadSeq = 0;

export default function FileUploadPanel({ files, onUpload, onDelete, deletingIds }: FileUploadPanelProps) {
  const [isDragOver, setIsDragOver] = React.useState(false);
  const [uploads, setUploads] = React.useState<PendingUpload[]>([]);
  const fileInputRef = React.useRef<HTMLInputElement>(null);

  const dismissUpload = (id: string) =>
    setUploads((prev) => prev.filter((u) => u.id !== id));

  // Upload a real file: parsing + embedding happens server-side, so we show an
  // indeterminate progress bar that creeps toward 90% until the POST resolves.
  const startUpload = (file: File) => {
    const fileId = `up-${uploadSeq++}`;

    setUploads((prev) => [
      ...prev,
      { id: fileId, name: file.name, file, progress: 8, status: 'uploading' },
    ]);

    let prog = 8;
    const interval = setInterval(() => {
      prog = Math.min(90, prog + Math.floor(Math.random() * 8) + 4);
      setUploads((prev) =>
        prev.map((u) => (u.id === fileId && u.status === 'uploading' ? { ...u, progress: prog } : u))
      );
    }, 300);

    const succeed = () => {
      clearInterval(interval);
      // Don't show a separate "file uploaded" card. The file is already added
      // to the knowledge-base list below, so just remove the progress toast and
      // let the file card itself signal completion.
      dismissUpload(fileId);
    };

    const fail = () => {
      clearInterval(interval);
      setUploads((prev) =>
        prev.map((u) => (u.id === fileId ? { ...u, status: 'error' } : u))
      );
    };

    Promise.resolve(onUpload(file)).then(succeed, fail);
  };

  const retryUpload = (u: PendingUpload) => {
    dismissUpload(u.id);
    startUpload(u.file);
  };

  const handleDragOver = (e: React.DragEvent) => {
    e.preventDefault();
    setIsDragOver(true);
  };

  const handleDragLeave = (e: React.DragEvent) => {
    e.preventDefault();
    setIsDragOver(false);
  };

  const handleDrop = (e: React.DragEvent) => {
    e.preventDefault();
    setIsDragOver(false);
    
    if (e.dataTransfer.files && e.dataTransfer.files.length > 0) {
      const droppedFiles = Array.from(e.dataTransfer.files);
      const eligibleFiles = droppedFiles.filter(f => {
        const ext = f.name.split('.').pop()?.toLowerCase() || '';
        return ['pdf', 'docx', 'doc', 'xlsx', 'xls', 'csv'].includes(ext);
      });

      if (eligibleFiles.length > 0) {
        eligibleFiles.forEach((f) => startUpload(f));
      } else {
        alert('Please drop accepted file types (PDF, DOCX, XLSX, XLS, CSV).');
      }
    }
  };

  const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    if (e.target.files && e.target.files.length > 0) {
      const selected = Array.from(e.target.files);
      selected.forEach((f) => startUpload(f));
      // Reset input value to allow uploading same file again if deleted
      if (fileInputRef.current) fileInputRef.current.value = '';
    }
  };

  const triggerBrowse = () => {
    fileInputRef.current?.click();
  };

  return (
    <div className="flex flex-col h-full">
      {/* Panel Headers */}
      <div>
        <h2 className="text-xl font-semibold text-white tracking-tight">Upload documents</h2>
        <p className="text-sm text-white/50 mt-1 leading-relaxed">
          Drag and drop PDFs, Word documents, and spreadsheets into your knowledge base.
        </p>
      </div>

      {/* Hidden File Input */}
      <input
        ref={fileInputRef}
        type="file"
        multiple
        accept=".pdf,.docx,.doc,.xlsx,.xls,.csv"
        className="hidden"
        onChange={handleFileChange}
      />

      {/* Drop Zone Box */}
      <div
        onDragOver={handleDragOver}
        onDragLeave={handleDragLeave}
        onDrop={handleDrop}
        className={`mt-6 border-2 border-dashed rounded-2xl flex flex-col items-center justify-center p-8 text-center transition-all duration-300 ${
          isDragOver
            ? 'border-white/40 bg-white/5 hover:border-white/50'
            : 'border-white/10 bg-black/20 hover:border-white/20'
        }`}
      >
        {/* Document Icons – fanned-out brand logos */}
        <div className="flex items-end justify-center mb-5">
          <FileTypeIcon
            type="PDF"
            size={44}
            className="-rotate-12 translate-y-1 drop-shadow-xl transition-transform duration-300"
          />
          <FileTypeIcon
            type="DOCX"
            size={54}
            className="-mx-2 z-10 drop-shadow-2xl transition-transform duration-300"
          />
          <FileTypeIcon
            type="EXCEL"
            size={44}
            className="rotate-12 translate-y-1 drop-shadow-xl transition-transform duration-300"
          />
        </div>

        <p className="text-sm font-medium text-white/90">Drop your files here</p>
        <p className="text-xs text-white/40 mt-1">PDF, DOCX, XLSX, XLS, or CSV up to 25MB</p>

        <button
          type="button"
          onClick={triggerBrowse}
          className="mt-5 inline-flex items-center justify-center h-9 px-4 rounded-full text-xs font-semibold bg-white/5 border border-white/10 text-white hover:bg-white/10 transition-colors duration-150 active:scale-[0.98] cursor-pointer"
        >
          Browse files
        </button>
      </div>

      {/* Files List Heading */}
      <div className="mt-8">
        <h3 className="text-xs font-bold text-white/40 uppercase tracking-wider">
          Knowledge Base (Files)
        </h3>

        {/* Upload status toasts (uploading / done / error) */}
        {uploads.length > 0 && (
          <div className="mt-3 space-y-2.5">
            {uploads.map((u) => (
              <UploadToast
                key={u.id}
                upload={u}
                onCancel={() => dismissUpload(u.id)}
                onRetry={() => retryUpload(u)}
              />
            ))}
          </div>
        )}

        {/* Stable List of Files */}
        {files.length === 0 && uploads.length === 0 ? (
          <div className="mt-3 p-8 border border-white/5 rounded-2xl bg-black/20 text-center text-xs text-white/30">
            No documents uploaded yet. Place your documents here to augment AI knowledge.
          </div>
        ) : (
          <div className="mt-3 space-y-2.5 max-h-[300px] overflow-y-auto pr-1">
            {files.map((file) => (
              <UploadedFileCard
                key={file.id}
                file={file}
                onDelete={onDelete}
                isDeleting={deletingIds.has(file.id)}
              />
            ))}
          </div>
        )}
      </div>

      {/* Bottom informational footline */}
      <p className="text-[11px] text-white/30 mt-auto pt-6 leading-normal flex items-center gap-1.5">
        <svg className="w-3.5 h-3.5 flex-shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
        </svg>
        Documents are indexed for AI search after upload.
      </p>
    </div>
  );
}

const TOAST_CONFIG: Record<
  UploadStatus,
  { glow: string; ring: string; bar: string; title: string; desc: string; icon: React.ReactNode }
> = {
  uploading: {
    glow: 'radial-gradient(circle, rgba(99,102,241,0.6) 0%, rgba(59,130,246,0.25) 45%, transparent 72%)',
    ring: 'border-indigo-400/60 text-indigo-300',
    bar: 'from-indigo-400 to-violet-400',
    title: 'Just a minute…',
    desc: 'Your file is uploading right now. Hang tight while we finish.',
    icon: (
      <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
        <path strokeLinecap="round" strokeLinejoin="round" d="M12 5v14m0 0l5-5m-5 5l-5-5" />
      </svg>
    ),
  },
  done: {
    glow: 'radial-gradient(circle, rgba(16,185,129,0.55) 0%, rgba(20,184,166,0.2) 45%, transparent 72%)',
    ring: 'border-emerald-400/60 text-emerald-300',
    bar: 'from-emerald-400 to-teal-400',
    title: 'Your file was uploaded!',
    desc: 'Added to your knowledge base and queued for AI indexing.',
    icon: (
      <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2.5}>
        <path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" />
      </svg>
    ),
  },
  error: {
    glow: 'radial-gradient(circle, rgba(244,63,94,0.55) 0%, rgba(225,29,72,0.2) 45%, transparent 72%)',
    ring: 'border-rose-400/60 text-rose-300',
    bar: 'from-rose-400 to-pink-400',
    title: 'We are so sorry!',
    desc: "There was an error and your file couldn't be uploaded. Try again?",
    icon: (
      <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2.5}>
        <path strokeLinecap="round" strokeLinejoin="round" d="M6 6l12 12M18 6L6 18" />
      </svg>
    ),
  },
};

interface UploadToastProps {
  upload: PendingUpload;
  onCancel: () => void;
  onRetry: () => void;
}

function UploadToast({ upload, onCancel, onRetry }: UploadToastProps) {
  const { name, progress, status } = upload;
  const cfg = TOAST_CONFIG[status];
  const btn =
    'inline-flex items-center justify-center h-8 px-4 rounded-lg text-xs font-semibold bg-white/[0.06] border border-white/10 text-white hover:bg-white/10 transition-colors duration-150 cursor-pointer';

  return (
    <div className="relative overflow-hidden rounded-2xl border border-white/10 bg-[#161616]/90 backdrop-blur-md p-4">
      {/* Soft corner glow tinted by status */}
      <div
        className="pointer-events-none absolute -top-12 -right-10 h-32 w-52 blur-3xl opacity-70"
        style={{ background: cfg.glow }}
      />

      <div className="relative flex gap-3.5">
        {/* Status icon ring */}
        <div className={`mt-0.5 h-9 w-9 flex-shrink-0 rounded-full border-2 flex items-center justify-center ${cfg.ring} ${status === 'uploading' ? 'animate-pulse' : ''}`}>
          {cfg.icon}
        </div>

        <div className="flex-1 min-w-0">
          <p className="text-sm font-bold text-white leading-tight">{cfg.title}</p>
          <p className="text-xs text-white/50 mt-1 leading-relaxed">{cfg.desc}</p>
          <p className="text-[11px] text-white/40 mt-1 truncate font-mono" title={name}>
            {name}
          </p>

          {status === 'uploading' && (
            <div className="mt-3 flex items-end gap-3">
              <div className="flex-1">
                <div className="flex justify-end mb-1">
                  <span className="text-[11px] font-semibold text-white/70 font-mono">{progress}%</span>
                </div>
                <div className="w-full h-1.5 rounded-full bg-white/10 overflow-hidden">
                  <div
                    className={`h-full rounded-full bg-gradient-to-r ${cfg.bar} transition-all duration-200`}
                    style={{ width: `${progress}%` }}
                  />
                </div>
              </div>
              <button onClick={onCancel} className={btn}>
                Cancel
              </button>
            </div>
          )}

          {status === 'error' && (
            <div className="mt-3 flex items-center gap-2">
              <button onClick={onRetry} className={btn}>
                Retry
              </button>
              <button onClick={onCancel} className={btn}>
                Cancel
              </button>
            </div>
          )}

          {status === 'done' && (
            <div className="mt-3">
              <button onClick={onCancel} className={btn}>
                Done
              </button>
            </div>
          )}
        </div>
      </div>
    </div>
  );
}