import React, { useState, useRef } from 'react'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'; import { Progress } from '@/components/ui/progress'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Upload, FileText, AlertTriangle, CheckCircle, Shield, X, Download, Loader2 } from 'lucide-react'; import { useAuth } from '@/contexts/AuthContext'; import { useToast } from '@/hooks/use-toast'; export function CallLogImportDialog({ open, onOpenChange, onSuccess }) { const { user } = useAuth(); const { toast } = useToast(); const fileInputRef = useRef(null); const [file, setFile] = useState(null); const [importing, setImporting] = useState(false); const [progress, setProgress] = useState(0); const [validationResult, setValidationResult] = useState(null); const [analyzing, setAnalyzing] = useState(false); const resetValidation = () => { setValidationResult(null); }; const handleFileChange = (e) => { const selectedFile = e.target.files[0]; if (selectedFile) { if (selectedFile.size > 5 * 1024 * 1024) { toast({ variant: "destructive", title: "File Too Large", description: "Max file size is 5MB." }); return; } setFile(selectedFile); resetValidation(); } }; const handleAnalyze = async () => { if (!file) return; setAnalyzing(true); try { // Placeholder: In production, use a real validation hook/service setValidationResult({ valid: true, sanitizedRecords: [], errors: [] }); toast({ title: "Analysis Complete", description: "File validated successfully." }); } catch (err) { toast({ variant: "destructive", title: "Analysis Failed", description: err.message }); } finally { setAnalyzing(false); } }; const executeImport = async () => { if (!validationResult || !validationResult.valid) return; setImporting(true); setProgress(10); try { // Placeholder: In production, use processCallLogImport setProgress(100); toast({ title: "Import Successful", description: `Imported ${validationResult.sanitizedRecords.length} logs.` }); if (onSuccess) onSuccess(); onOpenChange(false); } catch (err) { toast({ variant: "destructive", title: "Import Failed", description: err.message }); } finally { setImporting(false); setProgress(0); } }; const downloadTemplate = () => { toast({ title: "Template", description: "Template download not yet configured." }); }; const handleClose = () => { if (importing) return; setFile(null); resetValidation(); onOpenChange(false); }; return ( Secure Call Log Import Import call logs from CSV, Excel, or JSON. Data is validated for security and integrity.
{!file ? (

Upload Log File

CSV, Excel, JSON (max 5MB)

) : (
{file.name}
{!validationResult && ( )} {validationResult && (
{validationResult.valid ? ( Validation Passed Ready to import {validationResult.sanitizedRecords.length} records. ) : ( Validation Failed Found {validationResult.errors.length} issues. Import blocked. )} {!validationResult.valid && ( {validationResult.errors.map((err, i) => (
Row {err.row}: {err.messages.join(', ')}
))}
)}
)} {importing && (
Importing... {progress}%
)}
)}
{validationResult?.valid && ( )}
); }