import { useState } from 'react'; import { Cat } from '@/api/endpoints'; import { useAuth } from '@/hooks/useAuth'; import { useLikeCat, useUnlikeCat } from '@/hooks/useLikes'; import { Avatar, AvatarFallback } from '@/components/ui/avatar'; import { Heart, MessageCircle } from 'lucide-react'; import { useToast } from '@/components/Toast'; interface CatCardProps { cat: Cat; onOpen: (id: number) => void; } export default function CatCard({ cat, onOpen }: CatCardProps) { const { user, updateUser } = useAuth(); const [liked, setLiked] = useState(false); const [likeCount, setLikeCount] = useState(cat.likes_count); const [animating, setAnimating] = useState(false); const [showHeart, setShowHeart] = useState(false); const { toast } = useToast(); const likeMutation = useLikeCat(cat.id); const unlikeMutation = useUnlikeCat(cat.id); const isOwner = user && cat.user_id === user.id; const handleDoubleClick = () => { setShowHeart(true); setTimeout(() => setShowHeart(false), 500); if (!liked && !isOwner) handleLikeAction(); }; const handleLikeAction = async () => { if (isOwner || !user) return; setAnimating(true); setTimeout(() => setAnimating(false), 450); if (liked) { setLiked(false); setLikeCount((c) => c - 1); try { const res = await unlikeMutation.mutateAsync(); if (res.ownerPoints !== undefined && cat.user_id === user.id) updateUser({ ...user, points: res.ownerPoints }); } catch { setLiked(true); setLikeCount((c) => c + 1); } } else { setLiked(true); setLikeCount((c) => c + 1); toast('Нравится ♥', 'like'); try { await likeMutation.mutateAsync(); } catch { setLiked(false); setLikeCount((c) => c - 1); } } }; const handleLike = () => handleLikeAction(); const formatDate = (dateStr: string) => { const d = new Date(dateStr); const now = new Date(); const diff = now.getTime() - d.getTime(); const days = Math.floor(diff / (1000 * 60 * 60 * 24)); if (days === 0) return 'Сегодня'; if (days === 1) return 'Вчера'; return d.toLocaleDateString('ru-RU', { day: 'numeric', month: 'long' }); }; return (