2022-06-03 04:52:14 +00:00
|
|
|
import clsx from 'clsx'
|
2022-06-08 20:27:52 +00:00
|
|
|
import { useFollows } from 'web/hooks/use-follows'
|
2022-06-03 04:52:14 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
2022-06-08 20:27:52 +00:00
|
|
|
import { follow, unfollow } from 'web/lib/firebase/users'
|
2022-06-03 04:52:14 +00:00
|
|
|
|
|
|
|
export function FollowButton(props: {
|
|
|
|
isFollowing: boolean | undefined
|
|
|
|
onFollow: () => void
|
|
|
|
onUnfollow: () => void
|
2022-06-08 20:27:52 +00:00
|
|
|
small?: boolean
|
2022-06-03 04:52:14 +00:00
|
|
|
className?: string
|
|
|
|
}) {
|
2022-06-08 20:27:52 +00:00
|
|
|
const { isFollowing, onFollow, onUnfollow, small, className } = props
|
2022-06-03 04:52:14 +00:00
|
|
|
|
|
|
|
const user = useUser()
|
|
|
|
|
2022-06-08 20:27:52 +00:00
|
|
|
const smallStyle =
|
2022-06-09 04:06:25 +00:00
|
|
|
'btn !btn-xs border-2 border-gray-500 bg-white normal-case text-gray-500 hover:border-gray-500 hover:bg-white hover:text-gray-500'
|
2022-06-08 20:27:52 +00:00
|
|
|
|
2022-06-03 04:52:14 +00:00
|
|
|
if (!user || isFollowing === undefined)
|
|
|
|
return (
|
2022-06-08 20:27:52 +00:00
|
|
|
<button
|
|
|
|
className={clsx('btn btn-sm invisible', small && smallStyle, className)}
|
|
|
|
>
|
2022-06-03 04:52:14 +00:00
|
|
|
Follow
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
|
|
|
|
if (isFollowing) {
|
|
|
|
return (
|
|
|
|
<button
|
2022-06-08 20:27:52 +00:00
|
|
|
className={clsx(
|
|
|
|
'btn btn-outline btn-sm',
|
|
|
|
small && smallStyle,
|
|
|
|
className
|
|
|
|
)}
|
2022-06-03 04:52:14 +00:00
|
|
|
onClick={onUnfollow}
|
|
|
|
>
|
|
|
|
Following
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-06-08 20:27:52 +00:00
|
|
|
<button
|
|
|
|
className={clsx('btn btn-sm', small && smallStyle, className)}
|
|
|
|
onClick={onFollow}
|
|
|
|
>
|
2022-06-03 04:52:14 +00:00
|
|
|
Follow
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
2022-06-08 20:27:52 +00:00
|
|
|
|
|
|
|
export function UserFollowButton(props: { userId: string; small?: boolean }) {
|
|
|
|
const { userId, small } = props
|
|
|
|
const currentUser = useUser()
|
|
|
|
const following = useFollows(currentUser?.id)
|
|
|
|
const isFollowing = following?.includes(userId)
|
|
|
|
|
|
|
|
if (!currentUser) return null
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FollowButton
|
|
|
|
isFollowing={isFollowing}
|
|
|
|
onFollow={() => follow(currentUser.id, userId)}
|
|
|
|
onUnfollow={() => unfollow(currentUser.id, userId)}
|
|
|
|
small={small}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|