2022-01-26 20:03:32 +00:00
|
|
|
import clsx from 'clsx'
|
2022-03-17 07:56:25 +00:00
|
|
|
import { Fold } from '../../../common/fold'
|
2022-03-31 05:35:20 +00:00
|
|
|
import { useFollowedFoldIds } from '../../hooks/use-fold'
|
2022-03-17 07:56:25 +00:00
|
|
|
import { useUser } from '../../hooks/use-user'
|
|
|
|
import { followFold, unfollowFold } from '../../lib/firebase/folds'
|
2022-01-26 20:03:32 +00:00
|
|
|
|
|
|
|
export function FollowFoldButton(props: { fold: Fold; className?: string }) {
|
|
|
|
const { fold, className } = props
|
|
|
|
|
|
|
|
const user = useUser()
|
2022-02-21 05:12:35 +00:00
|
|
|
|
2022-03-31 05:35:20 +00:00
|
|
|
const followedFoldIds = useFollowedFoldIds(user)
|
2022-02-21 05:12:35 +00:00
|
|
|
const following = followedFoldIds
|
|
|
|
? followedFoldIds.includes(fold.id)
|
|
|
|
: undefined
|
2022-01-26 20:03:32 +00:00
|
|
|
|
|
|
|
const onFollow = () => {
|
2022-02-19 23:17:36 +00:00
|
|
|
if (user) followFold(fold.id, user.id)
|
2022-01-26 20:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const onUnfollow = () => {
|
|
|
|
if (user) unfollowFold(fold, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!user || following === undefined)
|
|
|
|
return (
|
|
|
|
<button className={clsx('btn btn-sm invisible', className)}>
|
|
|
|
Follow
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
|
|
|
|
if (following) {
|
|
|
|
return (
|
|
|
|
<button
|
2022-01-27 06:38:22 +00:00
|
|
|
className={clsx('btn btn-outline btn-sm', className)}
|
2022-01-26 20:03:32 +00:00
|
|
|
onClick={onUnfollow}
|
|
|
|
>
|
|
|
|
Following
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-02-03 09:49:53 +00:00
|
|
|
<button className={clsx('btn btn-sm', className)} onClick={onFollow}>
|
2022-01-26 20:03:32 +00:00
|
|
|
Follow
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|