import React, { useState, useCallback } from "react"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Progress } from "@/components/ui/progress"; import { Upload, FileText, X } from "lucide-react"; import { useSystemNotifications } from "@/hooks/useSystemNotifications"; import { useAgentGraph } from "@/context/AgentGraphContext"; import { api } from "@/lib/api"; interface UploadDialogProps { open: boolean; onOpenChange: (open: boolean) => void; } export function UploadDialog({ open, onOpenChange }: UploadDialogProps) { const [isDragging, setIsDragging] = useState(false); const [uploadProgress, setUploadProgress] = useState(0); const [isUploading, setIsUploading] = useState(false); const [selectedFile, setSelectedFile] = useState(null); const { notifySuccess, notifyError } = useSystemNotifications(); const { actions } = useAgentGraph(); const handleDragOver = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(true); }, []); const handleDragLeave = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(false); }, []); const handleDrop = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(false); const files = Array.from(e.dataTransfer.files); if (files.length > 0) { setSelectedFile(files[0] || null); } }, []); const handleFileSelect = useCallback( (e: React.ChangeEvent) => { const files = e.target.files; if (files && files.length > 0) { setSelectedFile(files[0] || null); } }, [] ); const handleUpload = async () => { if (!selectedFile) return; setIsUploading(true); setUploadProgress(0); try { const trace = await api.traces.upload(selectedFile, (progress) => { setUploadProgress(progress); }); notifySuccess( "Upload Successful", `${selectedFile.name} has been uploaded successfully.` ); // Refresh traces list const traces = await api.traces.list(); actions.setTraces(traces); // Select the uploaded trace actions.setSelectedTrace(trace); // Close dialog onOpenChange(false); setSelectedFile(null); setUploadProgress(0); } catch (error) { notifyError( "Upload Failed", error instanceof Error ? error.message : "Failed to upload file" ); } finally { setIsUploading(false); } }; const handleClose = () => { if (!isUploading) { onOpenChange(false); setSelectedFile(null); setUploadProgress(0); } }; return ( Upload Trace File Upload a trace file to analyze with AgentGraph pipeline
{!selectedFile ? (

Drop your trace file here

or click to browse files

) : (

{selectedFile.name}

{(selectedFile.size / 1024 / 1024).toFixed(2)} MB

{!isUploading && ( )}
{isUploading && (

Uploading... {Math.round(uploadProgress)}%

)}
)}
); }