'use client'; import { useState, useEffect, useRef } from 'react'; import { CheckCircle, Download, Plus, Loader2 } from 'lucide-react'; import { Card, CardContent } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { useNotification } from '@/components/ui/notification'; interface TranslationCompleteProps { jobId: string; fileName: string | null; onNewTranslation: () => void; } const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000'; export function TranslationComplete({ jobId, fileName, onNewTranslation, }: TranslationCompleteProps) { const [isDownloading, setIsDownloading] = useState(false); const { success, error } = useNotification(); const blobUrlRef = useRef(null); const handleDownload = async () => { setIsDownloading(true); try { const token = localStorage.getItem('token'); const headers: Record = {}; if (token) { headers['Authorization'] = `Bearer ${token}`; } const response = await fetch(`${API_BASE}/api/v1/download/${jobId}`, { headers }); if (!response.ok) { let errorMessage = 'Download failed'; try { const errorData = await response.json(); errorMessage = errorData.message || errorData.error || errorMessage; } catch { // Response not JSON } throw new Error(errorMessage); } const contentDisposition = response.headers.get('Content-Disposition'); let downloadFilename = 'translated_document'; if (contentDisposition) { const filenameMatch = contentDisposition.match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']+)/i); if (filenameMatch && filenameMatch[1]) { downloadFilename = filenameMatch[1]; } } else if (fileName) { const ext = fileName.split('.').pop() || ''; const baseName = fileName.replace(/\.[^.]+$/, ''); downloadFilename = `${baseName}_translated.${ext}`; } const blob = await response.blob(); const url = URL.createObjectURL(blob); blobUrlRef.current = url; const a = document.createElement('a'); a.href = url; a.download = downloadFilename; document.body.appendChild(a); a.click(); document.body.removeChild(a); setTimeout(() => { if (blobUrlRef.current) { URL.revokeObjectURL(blobUrlRef.current); blobUrlRef.current = null; } }, 1000); success({ title: 'Download Complete', description: `${downloadFilename} has been downloaded successfully.`, }); } catch (err) { error({ title: 'Download Failed', description: err instanceof Error ? err.message : 'Failed to download the translated file.', }); } finally { setIsDownloading(false); setTimeout(() => { if (blobUrlRef.current) { URL.revokeObjectURL(blobUrlRef.current); blobUrlRef.current = null; } }, 5000); } }; useEffect(() => { return () => { if (blobUrlRef.current) { URL.revokeObjectURL(blobUrlRef.current); blobUrlRef.current = null; } }; }, []); return (

Translation Complete!

{fileName ? `"${fileName}" has been translated successfully.` : 'Your document has been translated successfully.'}

); }