Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 5,252 Bytes
d15f07c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | import { useState } from 'react'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import api from '../lib/api'
import { UserPlusIcon, UserMinusIcon } from '@heroicons/react/24/outline'
import { CheckIcon } from '@heroicons/react/24/solid'
interface FollowButtonProps {
type: 'user' | 'leader' | 'organization' | 'tag'
// Organizations (text master_org_id) and tags (text tag_id) are text-keyed;
// users and leaders by integer id.
id: number | string
initialFollowing?: boolean
initialCount?: number
showCount?: boolean
compact?: boolean
onFollowChange?: (following: boolean, count: number) => void
}
export default function FollowButton({
type,
id,
initialFollowing = false,
initialCount = 0,
showCount = true,
compact = false,
onFollowChange
}: FollowButtonProps) {
const [isFollowing, setIsFollowing] = useState(initialFollowing)
const [followerCount, setFollowerCount] = useState(initialCount)
const [isHovered, setIsHovered] = useState(false)
const queryClient = useQueryClient()
const followMutation = useMutation({
mutationFn: async () => {
const endpoint = `/social/follow/${type}/${id}`
const response = await api.post(endpoint)
return response.data
},
onSuccess: (data) => {
setIsFollowing(true)
setFollowerCount(data.follower_count)
onFollowChange?.(true, data.follower_count)
// Invalidate relevant queries
queryClient.invalidateQueries({ queryKey: ['social', 'stats'] })
queryClient.invalidateQueries({ queryKey: ['following', type] })
}
})
const unfollowMutation = useMutation({
mutationFn: async () => {
const endpoint = `/social/follow/${type}/${id}`
const response = await api.delete(endpoint)
return response.data
},
onSuccess: (data) => {
setIsFollowing(false)
setFollowerCount(data.follower_count)
onFollowChange?.(false, data.follower_count)
// Invalidate relevant queries
queryClient.invalidateQueries({ queryKey: ['social', 'stats'] })
queryClient.invalidateQueries({ queryKey: ['following', type] })
}
})
const handleClick = () => {
if (isFollowing) {
unfollowMutation.mutate()
} else {
followMutation.mutate()
}
}
const isLoading = followMutation.isPending || unfollowMutation.isPending
// LinkedIn/Facebook style button
if (compact) {
return (
<button
onClick={handleClick}
disabled={isLoading}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
className={`
inline-flex items-center gap-1.5 px-4 py-1.5 rounded-full text-sm font-medium
transition-all duration-200 border
${isFollowing
? isHovered
? 'bg-red-50 border-red-300 text-red-700 hover:bg-red-100'
: 'bg-white border-gray-300 text-gray-700 hover:bg-gray-50'
: 'bg-blue-600 border-blue-600 text-white hover:bg-blue-700'
}
disabled:opacity-50 disabled:cursor-not-allowed
`}
>
{isLoading ? (
<span className="h-4 w-4 border-2 border-t-transparent border-current rounded-full animate-spin" />
) : isFollowing ? (
<>
{isHovered ? (
<UserMinusIcon className="h-4 w-4" />
) : (
<CheckIcon className="h-4 w-4" />
)}
<span>{isHovered ? 'Unfollow' : 'Following'}</span>
</>
) : (
<>
<UserPlusIcon className="h-4 w-4" />
<span>Follow</span>
</>
)}
</button>
)
}
// Full button with count
return (
<div className="inline-flex items-center gap-3">
<button
onClick={handleClick}
disabled={isLoading}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
className={`
inline-flex items-center gap-2 px-5 py-2 rounded-lg text-sm font-semibold
transition-all duration-200 border-2
${isFollowing
? isHovered
? 'bg-red-50 border-red-400 text-red-700 hover:bg-red-100'
: 'bg-white border-gray-300 text-gray-700 hover:border-gray-400'
: 'bg-blue-600 border-blue-600 text-white hover:bg-blue-700'
}
disabled:opacity-50 disabled:cursor-not-allowed shadow-sm hover:shadow-md
`}
>
{isLoading ? (
<span className="h-5 w-5 border-2 border-t-transparent border-current rounded-full animate-spin" />
) : isFollowing ? (
<>
{isHovered ? (
<UserMinusIcon className="h-5 w-5" />
) : (
<CheckIcon className="h-5 w-5" />
)}
<span>{isHovered ? 'Unfollow' : 'Following'}</span>
</>
) : (
<>
<UserPlusIcon className="h-5 w-5" />
<span>Follow</span>
</>
)}
</button>
{showCount && (
<span className="text-sm text-gray-600">
{followerCount.toLocaleString()} {followerCount === 1 ? 'follower' : 'followers'}
</span>
)}
</div>
)
}
|