import { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { Helmet } from 'react-helmet-async'; import { FileOutput } from 'lucide-react'; import FileUploader from '@/components/shared/FileUploader'; import ProgressBar from '@/components/shared/ProgressBar'; import DownloadButton from '@/components/shared/DownloadButton'; 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'; export default function ExtractPages() { const { t } = useTranslation(); const [phase, setPhase] = useState<'upload' | 'processing' | 'done'>('upload'); const [pages, setPages] = useState(''); const { file, uploadProgress, isUploading, taskId, error: uploadError, selectFile, startUpload, reset, } = useFileUpload({ endpoint: '/pdf-tools/extract-pages', maxSizeMB: 20, acceptedTypes: ['pdf'], extraData: { pages }, }); const { status, result, error: taskError } = useTaskPolling({ taskId, onComplete: () => setPhase('done'), 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 () => { if (!pages.trim()) return; const id = await startUpload(); if (id) setPhase('processing'); }; const handleReset = () => { reset(); setPhase('upload'); setPages(''); }; const schema = generateToolSchema({ name: t('tools.extractPages.title'), description: t('tools.extractPages.description'), url: `${window.location.origin}/tools/extract-pages`, }); return ( <> {t('tools.extractPages.title')} — {t('common.appName')}

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

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

{phase === 'upload' && (
{file && !isUploading && ( <>
setPages(e.target.value)} placeholder={t('tools.extractPages.pagesPlaceholder')} className="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm dark:border-slate-600 dark:bg-slate-700 dark:text-slate-200" />

{t('tools.extractPages.pagesHint')}

)}
)} {phase === 'processing' && !result && ( )} {phase === 'done' && result && result.status === 'completed' && ( )} {phase === 'done' && taskError && (

{taskError}

)}
); }