import { useState } from 'react' import { useNavigate } from 'react-router-dom' import { useTranslation } from 'react-i18next' import { useQuery } from '@tanstack/react-query' import { Button } from '@/components/ui/button' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import ResultsTable from '@/components/ResultsTable' import MarkdownPreview from '@/components/MarkdownPreview' import { useToast } from '@/components/ui/toast' import { useUploadStore } from '@/store/uploadStore' import { apiClient } from '@/services/api' import { FileText, Download, Languages, AlertCircle, TrendingUp, Clock, Layers } from 'lucide-react' export default function ResultsPage() { const { t } = useTranslation() const navigate = useNavigate() const { toast } = useToast() const { batchId } = useUploadStore() const [selectedFileId, setSelectedFileId] = useState(null) // Get batch status to show results const { data: batchStatus } = useQuery({ queryKey: ['batchStatus', batchId], queryFn: () => apiClient.getBatchStatus(batchId!), enabled: !!batchId, }) // Get OCR result for selected file const { data: ocrResult, isLoading: isLoadingResult } = useQuery({ queryKey: ['ocrResult', selectedFileId], queryFn: () => apiClient.getOCRResult(selectedFileId!), enabled: !!selectedFileId, }) const handleViewResult = (fileId: number) => { setSelectedFileId(fileId) } const handleDownloadPDF = async (fileId: number) => { try { const blob = await apiClient.exportPDF(fileId) const url = window.URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `ocr-result-${fileId}.pdf` document.body.appendChild(a) a.click() window.URL.revokeObjectURL(url) document.body.removeChild(a) toast({ title: t('export.exportSuccess'), description: 'PDF 已下載', variant: 'success', }) } catch (error: any) { toast({ title: t('export.exportError'), description: error.response?.data?.detail || t('errors.networkError'), variant: 'destructive', }) } } const handleExport = () => { navigate('/export') } // Show helpful message when no batch is selected if (!batchId) { return (
{t('results.title')}

{t('results.noBatchMessage', { defaultValue: '尚未選擇任何批次。請先上傳並處理檔案。' })}

) } const completedFiles = batchStatus?.files.filter((f) => f.status === 'completed') || [] return (
{/* Page Header */}

{t('results.title')}

批次 ID: {batchId} · 已完成 {completedFiles.length} 個檔案

{/* Results Table - Takes 2 columns */}
{/* Preview Panel - Takes 3 columns */}
{selectedFileId && ocrResult ? (
{/* Preview Card */} {/* Stats Grid */}

準確率

{((ocrResult.confidence || 0) * 100).toFixed(1)}%

處理時間

{(ocrResult.processing_time || 0).toFixed(2)}s

文字區塊

{ocrResult.json_data?.total_text_regions || 0}

) : (

{isLoadingResult ? t('common.loading') : '選擇左側檔案以查看詳細結果'}

)}
) }