fix: prevent preview infinite loop and add document type filtering

- Remove onAutoConfigReceived callback that caused state update loop
- Add document analysis to check if file needs OCR track
- Only show preprocessing options for OCR-eligible files (images, scanned PDFs)
- Show informative message for editable PDFs that use direct text extraction
- Display text coverage percentage for editable documents

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
egg
2025-11-27 17:31:05 +08:00
parent 894d18b432
commit 2861f54838
2 changed files with 76 additions and 39 deletions

View File

@@ -1,20 +1,20 @@
import { useState, useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import { useTranslation } from 'react-i18next'
import { useMutation } from '@tanstack/react-query'
import { useMutation, useQuery } 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 { Play, CheckCircle, FileText, AlertCircle, Clock, Activity, Loader2, Info } from 'lucide-react'
import LayoutModelSelector from '@/components/LayoutModelSelector'
import PreprocessingSettings from '@/components/PreprocessingSettings'
import PreprocessingPreview from '@/components/PreprocessingPreview'
import TaskNotFound from '@/components/TaskNotFound'
import { useTaskValidation } from '@/hooks/useTaskValidation'
import type { LayoutModel, ProcessingOptions, PreprocessingMode, PreprocessingConfig } from '@/types/apiV2'
import type { LayoutModel, ProcessingOptions, PreprocessingMode, PreprocessingConfig, DocumentAnalysisResponse } from '@/types/apiV2'
export default function ProcessingPage() {
const { t } = useTranslation()
@@ -47,6 +47,20 @@ export default function ProcessingPage() {
})
const [showPreview, setShowPreview] = useState(false)
// Analyze document to determine if OCR is needed (only for pending tasks)
const { data: documentAnalysis, isLoading: isAnalyzing } = useQuery({
queryKey: ['documentAnalysis', taskId],
queryFn: () => apiClientV2.analyzeDocument(taskId!),
enabled: !!taskId && taskDetail?.status === 'pending',
staleTime: Infinity, // Cache indefinitely since document doesn't change
})
// Determine if preprocessing options should be shown
// Only show for OCR track files (images and non-editable PDFs)
const needsOcrTrack = documentAnalysis?.recommended_track === 'ocr' ||
documentAnalysis?.recommended_track === 'hybrid' ||
!documentAnalysis // Show by default while analyzing
// Start OCR processing
const processOCRMutation = useMutation({
mutationFn: () => {
@@ -360,36 +374,66 @@ export default function ProcessingPage() {
{/* Processing Options (only show when task is pending) */}
{isPending && (
<div className="space-y-6">
{/* Layout Model Selection */}
<LayoutModelSelector
value={layoutModel}
onChange={setLayoutModel}
disabled={processOCRMutation.isPending}
/>
{/* Document Analysis Loading */}
{isAnalyzing && (
<div className="flex items-center gap-2 p-4 bg-muted/30 rounded-lg border">
<Loader2 className="w-4 h-4 animate-spin text-primary" />
<span className="text-sm text-muted-foreground">...</span>
</div>
)}
{/* Preprocessing Settings */}
<PreprocessingSettings
mode={preprocessingMode}
config={preprocessingConfig}
onModeChange={setPreprocessingMode}
onConfigChange={setPreprocessingConfig}
onPreview={() => setShowPreview(!showPreview)}
disabled={processOCRMutation.isPending}
/>
{/* Direct Track Notice - Show when document is editable PDF */}
{documentAnalysis && documentAnalysis.recommended_track === 'direct' && (
<Card className="border-blue-200 bg-blue-50">
<CardContent className="pt-4">
<div className="flex items-start gap-3">
<Info className="w-5 h-5 text-blue-600 flex-shrink-0 mt-0.5" />
<div>
<p className="text-sm font-medium text-blue-800"> PDF</p>
<p className="text-sm text-blue-700 mt-1">
PDF 使
</p>
{documentAnalysis.text_coverage && (
<p className="text-xs text-blue-600 mt-2">
: {(documentAnalysis.text_coverage * 100).toFixed(1)}%
</p>
)}
</div>
</div>
</CardContent>
</Card>
)}
{/* Preprocessing Preview */}
{showPreview && taskId && (
<PreprocessingPreview
taskId={taskId}
mode={preprocessingMode}
config={preprocessingConfig}
onAutoConfigReceived={(autoConfig) => {
// Only update if user hasn't switched to manual mode
if (preprocessingMode === 'auto') {
setPreprocessingConfig(autoConfig)
}
}}
/>
{/* OCR Track Options - Only show when document needs OCR */}
{needsOcrTrack && !isAnalyzing && (
<>
{/* Layout Model Selection */}
<LayoutModelSelector
value={layoutModel}
onChange={setLayoutModel}
disabled={processOCRMutation.isPending}
/>
{/* Preprocessing Settings */}
<PreprocessingSettings
mode={preprocessingMode}
config={preprocessingConfig}
onModeChange={setPreprocessingMode}
onConfigChange={setPreprocessingConfig}
onPreview={() => setShowPreview(!showPreview)}
disabled={processOCRMutation.isPending}
/>
{/* Preprocessing Preview */}
{showPreview && taskId && (
<PreprocessingPreview
taskId={taskId}
mode={preprocessingMode}
config={preprocessingConfig}
/>
)}
</>
)}
</div>
)}