import { useState, useCallback } from 'react'; import { useNavigate } from 'react-router-dom'; import { useDropzone } from 'react-dropzone'; import { useUploadCat } from '@/hooks/useCats'; import { useAuth } from '@/hooks/useAuth'; import { useToast } from '@/components/Toast'; import { Button } from '@/components/ui/button'; import { Avatar, AvatarFallback } from '@/components/ui/avatar'; import { ArrowLeft, Upload, X, ImageIcon } from 'lucide-react'; export default function UploadPage() { const navigate = useNavigate(); const { user, updateUser } = useAuth(); const [file, setFile] = useState(null); const [preview, setPreview] = useState(null); const [caption, setCaption] = useState(''); const uploadMutation = useUploadCat(); const { toast } = useToast(); const onDrop = useCallback((accepted: File[]) => { const f = accepted[0]; if (f) { setFile(f); if (preview) URL.revokeObjectURL(preview); setPreview(URL.createObjectURL(f)); } }, [preview]); const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop, accept: { 'image/*': ['.jpg', '.jpeg', '.png', '.gif', '.webp'] }, maxFiles: 1, maxSize: 10 * 1024 * 1024, }); const handleUpload = async () => { if (!file) return; const fd = new FormData(); fd.append('image', file); fd.append('caption', caption); try { const result = await uploadMutation.mutateAsync(fd); if (user) updateUser({ ...user, points: (user.points || 0) + 10 }); toast('+10 баллов! 🎉', 'success'); navigate(`/cat/${result.cat.id}`); } catch { toast('Ошибка загрузки', 'error'); } }; const clearFile = () => { setFile(null); if (preview) URL.revokeObjectURL(preview); setPreview(null); }; return (

Новый пост

{!preview ? (

{isDragActive ? 'Отпустите фото' : 'Перетащите фото сюда'}

или нажмите, чтобы выбрать

JPG, PNG, GIF, WebP · до 10 МБ

) : (
Preview
{user?.username?.charAt(0).toUpperCase() || '?'}

{user?.username}