| 1 | import React from 'react'
|
|---|
| 2 | import { Heart } from 'lucide-react'
|
|---|
| 3 | import { useNavigate } from 'react-router-dom'
|
|---|
| 4 | import { useAuthStore } from '../../store/authStore'
|
|---|
| 5 | import { useStoryStore } from '../../store/storyStore'
|
|---|
| 6 | import { useNotificationStore } from '../../store/notificationStore'
|
|---|
| 7 | import { useUIStore } from '../../store/uiStore'
|
|---|
| 8 |
|
|---|
| 9 | interface LikeButtonProps {
|
|---|
| 10 | storyId: number
|
|---|
| 11 | authorUserId: number
|
|---|
| 12 | totalLikes: number
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | export const LikeButton: React.FC<LikeButtonProps> = ({ storyId, authorUserId, totalLikes }) => {
|
|---|
| 16 | const navigate = useNavigate()
|
|---|
| 17 | const { currentUser } = useAuthStore()
|
|---|
| 18 | const { toggleLike, isLiked } = useStoryStore()
|
|---|
| 19 | const { addNotification } = useNotificationStore()
|
|---|
| 20 | const { addToast } = useUIStore()
|
|---|
| 21 |
|
|---|
| 22 | const liked = currentUser ? isLiked(currentUser.user_id, storyId) : false
|
|---|
| 23 |
|
|---|
| 24 | const handleClick = () => {
|
|---|
| 25 | if (!currentUser) {
|
|---|
| 26 | addToast('Please sign in to like stories.', 'info')
|
|---|
| 27 | navigate('/login')
|
|---|
| 28 | return
|
|---|
| 29 | }
|
|---|
| 30 | toggleLike(currentUser.user_id, storyId)
|
|---|
| 31 | if (!liked && currentUser.user_id !== authorUserId) {
|
|---|
| 32 | addNotification({
|
|---|
| 33 | user_id: authorUserId,
|
|---|
| 34 | type: 'like',
|
|---|
| 35 | title: 'New Like',
|
|---|
| 36 | message: `${currentUser.username} liked your story.`,
|
|---|
| 37 | link: `/story/${storyId}`,
|
|---|
| 38 | })
|
|---|
| 39 | }
|
|---|
| 40 | addToast(liked ? 'Removed from likes' : 'Added to likes!', liked ? 'info' : 'success')
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | return (
|
|---|
| 44 | <button
|
|---|
| 45 | onClick={handleClick}
|
|---|
| 46 | className={`flex items-center gap-2 px-4 py-2 rounded-xl border transition-all duration-200 ${
|
|---|
| 47 | liked
|
|---|
| 48 | ? 'bg-rose-500/20 border-rose-500/40 text-rose-400 hover:bg-rose-500/30'
|
|---|
| 49 | : 'bg-slate-800 border-slate-700 text-slate-400 hover:border-rose-500/40 hover:text-rose-400 hover:bg-rose-500/10'
|
|---|
| 50 | }`}
|
|---|
| 51 | >
|
|---|
| 52 | <Heart size={16} className={liked ? 'fill-rose-400' : ''} />
|
|---|
| 53 | <span className="text-sm font-medium">{totalLikes.toLocaleString()}</span>
|
|---|
| 54 | </button>
|
|---|
| 55 | )
|
|---|
| 56 | }
|
|---|