import { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Helmet } from 'react-helmet-async';
import { Eraser } 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';
import { useConfig } from '@/hooks/useConfig';
export default function RemoveBackground() {
const { t } = useTranslation();
const { limits } = useConfig();
const [phase, setPhase] = useState<'upload' | 'processing' | 'done'>('upload');
const {
file, uploadProgress, isUploading, taskId,
error: uploadError, selectFile, startUpload, reset,
} = useFileUpload({
endpoint: '/remove-bg',
maxSizeMB: limits.image ?? 10,
acceptedTypes: ['png', 'jpg', 'jpeg', 'webp'],
});
const { status, result, error: taskError } = useTaskPolling({
taskId,
onComplete: () => setPhase('done'),
onError: () => setPhase('done'),
});
// Accept file from homepage smart upload
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');
};
const schema = generateToolSchema({
name: t('tools.removeBg.title'),
description: t('tools.removeBg.description'),
url: `${window.location.origin}/tools/remove-background`,
});
return (
<>
{t('tools.removeBg.description')}
{t('tools.removeBg.success')}