-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFUpload.tsx
More file actions
163 lines (149 loc) · 5.25 KB
/
PDFUpload.tsx
File metadata and controls
163 lines (149 loc) · 5.25 KB
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
import { useState } from 'react';
import { Upload, FileText, Loader2, CheckCircle, AlertCircle } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { useToast } from '@/hooks/use-toast';
interface PDFUploadProps {
onUploadSuccess?: (fileName?: string) => void;
}
interface UploadResponse {
message: string;
filename?: string;
status: 'success' | 'error';
}
export function PDFUpload({ onUploadSuccess }: PDFUploadProps) {
const [selectedFile, setSelectedFile] = useState<File | null>(null);
const [isUploading, setIsUploading] = useState(false);
const [uploadStatus, setUploadStatus] = useState<'idle' | 'success' | 'error'>('idle');
const { toast } = useToast();
const handleFileSelect = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (file && file.type === 'application/pdf') {
setSelectedFile(file);
setUploadStatus('idle');
} else {
toast({
title: "Invalid file type",
description: "Please select a PDF file.",
variant: "destructive",
});
}
};
const handleUpload = async () => {
if (!selectedFile) return;
setIsUploading(true);
setUploadStatus('idle');
try {
const formData = new FormData();
formData.append('file', selectedFile);
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000';
const response = await fetch(`${apiBaseUrl}/api/v1/ingest`, {
method: 'POST',
body: formData,
});
const result: UploadResponse = await response.json();
if (response.ok) {
setUploadStatus('success');
toast({
title: "Upload successful",
description: result.message || "PDF uploaded and processed successfully.",
});
onUploadSuccess?.(selectedFile.name);
} else {
setUploadStatus('error');
toast({
title: "Upload failed",
description: result.message || "Failed to upload PDF.",
variant: "destructive",
});
}
} catch (error) {
setUploadStatus('error');
toast({
title: "Upload error",
description: "Network error. Please check if the backend server is running.",
variant: "destructive",
});
} finally {
setIsUploading(false);
}
};
const getStatusIcon = () => {
switch (uploadStatus) {
case 'success':
return <CheckCircle className="h-5 w-5 text-success" />;
case 'error':
return <AlertCircle className="h-5 w-5 text-destructive" />;
default:
return <FileText className="h-5 w-5 text-muted-foreground" />;
}
};
return (
<Card className="bg-gradient-card border-border shadow-elegant">
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Upload className="h-5 w-5 text-primary" />
PDF Upload
</CardTitle>
<CardDescription>
Upload a PDF document to process and make it queryable
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<div className="space-y-3">
<div className="flex items-center justify-center w-full">
<label
htmlFor="pdf-upload"
className="flex flex-col items-center justify-center w-full h-32 border-2 border-dashed border-border rounded-lg cursor-pointer bg-muted/50 hover:bg-muted/80 transition-colors"
>
<div className="flex flex-col items-center justify-center pt-5 pb-6">
<Upload className="w-8 h-8 mb-2 text-muted-foreground" />
<p className="mb-2 text-sm text-muted-foreground">
<span className="font-semibold">Click to upload</span> your PDF
</p>
<p className="text-xs text-muted-foreground">PDF files only</p>
</div>
<input
id="pdf-upload"
type="file"
accept=".pdf"
className="hidden"
onChange={handleFileSelect}
/>
</label>
</div>
{selectedFile && (
<div className="flex items-center gap-3 p-3 bg-muted rounded-lg">
{getStatusIcon()}
<div className="flex-1 min-w-0">
<p className="text-sm font-medium text-foreground truncate">
{selectedFile.name}
</p>
<p className="text-xs text-muted-foreground">
{(selectedFile.size / 1024 / 1024).toFixed(2)} MB
</p>
</div>
</div>
)}
<Button
onClick={handleUpload}
disabled={!selectedFile || isUploading}
className="w-full bg-gradient-primary hover:opacity-90 transition-opacity"
>
{isUploading ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Uploading...
</>
) : (
<>
<Upload className="mr-2 h-4 w-4" />
Upload PDF
</>
)}
</Button>
</div>
</CardContent>
</Card>
);
}