import React, { useCallback, useState } from 'react'; import { Card, CardContent } from '@/components/ui/card'; import { httpClient } from '@/app/infra/http/HttpClient'; import { toast } from 'sonner'; import { useTranslation } from 'react-i18next'; interface FileUploadZoneProps { kbId: string; onUploadSuccess: () => void; onUploadError: (error: string) => void; } export default function FileUploadZone({ kbId, onUploadSuccess, onUploadError, }: FileUploadZoneProps) { const { t } = useTranslation(); const [isDragOver, setIsDragOver] = useState(false); const [isUploading, setIsUploading] = useState(false); const handleUpload = useCallback( async (file: File) => { if (isUploading) return; setIsUploading(true); const toastId = toast.loading(t('knowledge.documentsTab.uploadingFile')); try { // Step 1: Upload file to server const uploadResult = await httpClient.uploadDocumentFile(file); // Step 2: Associate file with knowledge base await httpClient.uploadKnowledgeBaseFile(kbId, uploadResult.file_id); toast.success(t('knowledge.documentsTab.uploadSuccess'), { id: toastId, }); onUploadSuccess(); } catch (error) { console.error('File upload failed:', error); const errorMessage = t('knowledge.documentsTab.uploadError'); toast.error(errorMessage, { id: toastId }); onUploadError(errorMessage); } finally { setIsUploading(false); } }, [kbId, isUploading, onUploadSuccess, onUploadError], ); const handleDragOver = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragOver(true); }, []); const handleDragLeave = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragOver(false); }, []); const handleDrop = useCallback( (e: React.DragEvent) => { e.preventDefault(); setIsDragOver(false); const files = Array.from(e.dataTransfer.files); if (files.length > 0) { handleUpload(files[0]); } }, [handleUpload], ); const handleFileSelect = useCallback( (e: React.ChangeEvent) => { const files = e.target.files; if (files && files.length > 0) { handleUpload(files[0]); } }, [handleUpload], ); return (
); }