import React, { useState } from 'react'; import { Bookmark } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { feedbackService } from '../../services/feedback.service'; import { toast } from 'react-toastify'; interface PhotoFavoritesProps { photoId: string; gallerySlug: string; isFavorited: boolean; favoriteCount: number; isEnabled: boolean; onFavoriteChange?: (favorited: boolean) => void; } export const PhotoFavorites: React.FC = ({ photoId, gallerySlug, isFavorited, favoriteCount, isEnabled, onFavoriteChange }) => { const { t } = useTranslation(); const queryClient = useQueryClient(); const [isSubmitting, setIsSubmitting] = useState(false); const [animating, setAnimating] = useState(false); const submitFavoriteMutation = useMutation({ mutationFn: () => feedbackService.submitFeedback(gallerySlug, photoId, { feedback_type: 'favorite' }), onMutate: async () => { setIsSubmitting(true); setAnimating(true); // Optimistic update if (onFavoriteChange) { onFavoriteChange(!isFavorited); } }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['photo-feedback', gallerySlug, photoId] }); }, onError: (error: any) => { // Revert optimistic update if (onFavoriteChange) { onFavoriteChange(isFavorited); } if (error.response?.status === 429) { toast.error(t('feedback.rateLimited', 'Please wait before favoriting again')); } else { toast.error(t('feedback.favoriteError', 'Failed to update favorite')); } }, onSettled: () => { setIsSubmitting(false); setTimeout(() => setAnimating(false), 300); } }); const handleFavoriteClick = () => { if (!isEnabled || isSubmitting) return; submitFavoriteMutation.mutate(); }; if (!isEnabled) return null; return ( ); };