import { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { Helmet } from 'react-helmet-async'; import { Table } from 'lucide-react'; import FileUploader from '@/components/shared/FileUploader'; import ProgressBar from '@/components/shared/ProgressBar'; import AdSlot from '@/components/layout/AdSlot'; import { useFileUpload } from '@/hooks/useFileUpload'; import { useTaskPolling } from '@/hooks/useTaskPolling'; import { generateToolSchema } from '@/utils/seo'; import { useFileStore } from '@/stores/fileStore'; import { dispatchRatingPrompt } from '@/utils/ratingPrompt'; interface ExtractedTable { page: number; table_index: number; headers: string[]; rows: string[][]; } export default function TableExtractor() { const { t } = useTranslation(); const [phase, setPhase] = useState<'upload' | 'processing' | 'done'>('upload'); const [tables, setTables] = useState([]); const { file, uploadProgress, isUploading, taskId, error: uploadError, selectFile, startUpload, reset, } = useFileUpload({ endpoint: '/pdf-ai/extract-tables', maxSizeMB: 20, acceptedTypes: ['pdf'], }); const { status, result, error: taskError } = useTaskPolling({ taskId, onComplete: (r) => { setPhase('done'); const raw = r.tables; if (Array.isArray(raw)) setTables(raw as ExtractedTable[]); dispatchRatingPrompt('extract-tables'); }, onError: () => setPhase('done'), }); const storeFile = useFileStore((s) => s.file); const clearStoreFile = useFileStore((s) => s.clearFile); useEffect(() => { if (storeFile) { selectFile(storeFile); clearStoreFile(); } }, []); // eslint-disable-line react-hooks/exhaustive-deps const handleUpload = async () => { const id = await startUpload(); if (id) setPhase('processing'); }; const handleReset = () => { reset(); setPhase('upload'); setTables([]); }; const schema = generateToolSchema({ name: t('tools.tableExtractor.title'), description: t('tools.tableExtractor.description'), url: `${window.location.origin}/tools/extract-tables`, }); return ( <> {t('tools.tableExtractor.title')} — {t('common.appName')}

{t('tools.tableExtractor.title')}

{t('tools.tableExtractor.description')}

{phase === 'upload' && (
{file && !isUploading && ( )}
)} {phase === 'processing' && !result && ( )} {phase === 'done' && tables.length > 0 && (

{t('tools.tableExtractor.tablesFound', { count: tables.length })}

{tables.map((tbl, idx) => (
{t('tools.tableExtractor.tablePage', { page: tbl.page, index: tbl.table_index + 1 })}
{tbl.headers.length > 0 && ( {tbl.headers.map((h, hi) => ( ))} )} {tbl.rows.map((row, ri) => ( {row.map((cell, ci) => ( ))} ))}
{h}
{cell}
))}
)} {phase === 'done' && result && tables.length === 0 && !taskError && (

{t('tools.tableExtractor.noTables')}

)} {phase === 'done' && taskError && (

{taskError}

)} ); }