import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Helmet } from 'react-helmet-async'; import { ImageIcon } 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'; type OutputFormat = 'jpg' | 'png' | 'webp'; export default function ImageConverter() { const { t } = useTranslation(); const [phase, setPhase] = useState<'upload' | 'processing' | 'done'>('upload'); const [format, setFormat] = useState('jpg'); const [quality, setQuality] = useState(85); const { file, uploadProgress, isUploading, taskId, error: uploadError, selectFile, startUpload, reset, } = useFileUpload({ endpoint: '/image/convert', maxSizeMB: 10, acceptedTypes: ['png', 'jpg', 'jpeg', 'webp'], extraData: { format, quality: quality.toString() }, }); const { status, result, error: taskError } = useTaskPolling({ taskId, onComplete: () => setPhase('done'), onError: () => setPhase('done'), }); const handleUpload = async () => { const id = await startUpload(); if (id) setPhase('processing'); }; const handleReset = () => { reset(); setPhase('upload'); }; const formats: { value: OutputFormat; label: string }[] = [ { value: 'jpg', label: 'JPG' }, { value: 'png', label: 'PNG' }, { value: 'webp', label: 'WebP' }, ]; const schema = generateToolSchema({ name: t('tools.imageConvert.title'), description: t('tools.imageConvert.description'), url: `${window.location.origin}/tools/image-converter`, }); return ( <> {t('tools.imageConvert.title')} — {t('common.appName')}

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

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

{phase === 'upload' && (
{file && !isUploading && ( <> {/* Format Selector */}
{formats.map((f) => ( ))}
{/* Quality Slider (for lossy formats) */} {format !== 'png' && (
setQuality(Number(e.target.value))} className="w-full accent-primary-600" />
)} )}
)} {phase === 'processing' && !result && ( )} {phase === 'done' && result && result.status === 'completed' && ( )} {phase === 'done' && taskError && (

{taskError}

)}
); }