import { useState, useEffect } from 'react' import { useNavigate } from 'react-router-dom' import { useTranslation } from 'react-i18next' import { useMutation } from '@tanstack/react-query' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Progress } from '@/components/ui/progress' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { useToast } from '@/components/ui/toast' import { apiClientV2 } from '@/services/apiV2' import { Play, CheckCircle, FileText, AlertCircle, Clock, Activity, Loader2 } from 'lucide-react' import PPStructureParams from '@/components/PPStructureParams' import TaskNotFound from '@/components/TaskNotFound' import { useTaskValidation } from '@/hooks/useTaskValidation' import type { PPStructureV3Params, ProcessingOptions } from '@/types/apiV2' export default function ProcessingPage() { const { t } = useTranslation() const navigate = useNavigate() const { toast } = useToast() // Use shared hook for task validation const { taskId, taskDetail, isLoading: isValidating, isNotFound, clearAndReset } = useTaskValidation({ refetchInterval: (query) => { const data = query.state.data if (!data) return 2000 if (data.status === 'completed' || data.status === 'failed') { return false } return 2000 }, }) // PP-StructureV3 parameters state const [ppStructureParams, setPpStructureParams] = useState({}) // Start OCR processing const processOCRMutation = useMutation({ mutationFn: () => { const options: ProcessingOptions = { use_dual_track: true, language: 'ch', } // Only include pp_structure_params if user has customized them if (Object.keys(ppStructureParams).length > 0) { options.pp_structure_params = ppStructureParams } return apiClientV2.startTask(taskId!, options) }, onSuccess: () => { toast({ title: '開始處理', description: 'OCR 處理已開始', variant: 'success', }) }, onError: (error: any) => { toast({ title: t('errors.processingFailed'), description: error.response?.data?.detail || t('errors.networkError'), variant: 'destructive', }) }, }) // Auto-redirect when completed useEffect(() => { if (taskDetail?.status === 'completed') { setTimeout(() => { navigate('/tasks') }, 1000) } }, [taskDetail?.status, navigate]) const handleStartProcessing = () => { processOCRMutation.mutate() } const handleViewResults = () => { navigate('/tasks') } const getStatusBadge = (status: string) => { switch (status) { case 'completed': return {t('processing.completed')} case 'processing': return {t('processing.processing')} case 'failed': return {t('processing.failed')} default: return {t('processing.pending')} } } const getProgressPercentage = (status: string) => { switch (status) { case 'completed': return 100 case 'processing': return 50 case 'failed': return 100 default: return 0 } } // Show loading while validating task if (isValidating) { return (

載入任務資訊...

) } // Show message when task was deleted if (isNotFound) { return } // Show helpful message when no task is selected if (!taskId) { return (
{t('processing.title')}

{t('processing.noBatchMessage', { defaultValue: '尚未選擇任何任務。請先上傳檔案以建立任務。' })}

) } const isProcessing = taskDetail?.status === 'processing' const isCompleted = taskDetail?.status === 'completed' const isPending = !taskDetail || taskDetail.status === 'pending' return (
{/* Page Header */}

{t('processing.title')}

任務 ID: {taskId} {taskDetail?.filename && ` · ${taskDetail.filename}`}

{isCompleted && (
處理完成
)} {isProcessing && (
處理中
)}
{/* Overall Progress */}
{t('processing.progress')}
{taskDetail && getStatusBadge(taskDetail.status)}
{/* Progress bar */}
{t('processing.status')} {taskDetail ? getProgressPercentage(taskDetail.status) : 0}%
{/* Task Info */} {taskDetail && (

檔案名稱

{taskDetail.filename || '未知檔案'}

{taskDetail.processing_time_ms && (

處理時間

{(taskDetail.processing_time_ms / 1000).toFixed(2)}s

)}
)} {/* Error message */} {taskDetail?.error_message && (

處理失敗

{taskDetail.error_message}

)} {/* Action buttons */} {(isPending || isCompleted) && (
{isPending && ( )} {isCompleted && ( )}
)}
{/* Task Details Card */} {taskDetail && (
任務詳情
任務狀態 {getStatusBadge(taskDetail.status)}
建立時間 {new Date(taskDetail.created_at).toLocaleString('zh-TW')}
{taskDetail.updated_at && (
更新時間 {new Date(taskDetail.updated_at).toLocaleString('zh-TW')}
)} {taskDetail.completed_at && (
完成時間 {new Date(taskDetail.completed_at).toLocaleString('zh-TW')}
)}
)} {/* PP-StructureV3 Parameters (only show when task is pending) */} {isPending && ( )}
) }