2022-10-01 20:06:09 +00:00
|
|
|
import React, { useMemo, useState } from 'react'
|
2022-08-30 15:38:59 +00:00
|
|
|
import { User } from 'common/user'
|
|
|
|
import { useUserLikes } from 'web/hooks/use-likes'
|
|
|
|
import toast from 'react-hot-toast'
|
2022-10-14 12:05:07 +00:00
|
|
|
import { likeItem } from 'web/lib/firebase/likes'
|
2022-10-14 06:47:51 +00:00
|
|
|
import { LIKE_TIP_AMOUNT, TIP_UNDO_DURATION } from 'common/like'
|
2022-08-30 23:13:25 +00:00
|
|
|
import { firebaseLogin } from 'web/lib/firebase/users'
|
2022-10-14 12:05:07 +00:00
|
|
|
import { useItemTipTxns } from 'web/hooks/use-tip-txns'
|
2022-08-31 15:27:37 +00:00
|
|
|
import { sum } from 'lodash'
|
2022-10-01 20:51:08 +00:00
|
|
|
import { TipButton } from './tip-button'
|
2022-10-14 12:05:07 +00:00
|
|
|
import { Contract } from 'common/contract'
|
|
|
|
import { Post } from 'common/post'
|
2022-10-14 06:47:51 +00:00
|
|
|
import { TipToast } from '../tipper'
|
2022-08-30 15:38:59 +00:00
|
|
|
|
2022-10-14 12:05:07 +00:00
|
|
|
export function LikeItemButton(props: {
|
|
|
|
item: Contract | Post
|
2022-08-30 15:38:59 +00:00
|
|
|
user: User | null | undefined
|
2022-10-14 12:05:07 +00:00
|
|
|
itemType: string
|
2022-08-30 15:38:59 +00:00
|
|
|
}) {
|
2022-10-14 12:05:07 +00:00
|
|
|
const { item, user, itemType } = props
|
2022-10-01 20:06:09 +00:00
|
|
|
|
2022-10-14 12:05:07 +00:00
|
|
|
const tips = useItemTipTxns(item.id)
|
2022-10-01 21:16:34 +00:00
|
|
|
|
2022-08-31 15:27:37 +00:00
|
|
|
const totalTipped = useMemo(() => {
|
|
|
|
return sum(tips.map((tip) => tip.amount))
|
|
|
|
}, [tips])
|
2022-10-01 20:06:09 +00:00
|
|
|
|
2022-08-30 15:38:59 +00:00
|
|
|
const likes = useUserLikes(user?.id)
|
2022-10-01 20:06:09 +00:00
|
|
|
|
|
|
|
const [isLiking, setIsLiking] = useState(false)
|
|
|
|
|
2022-10-14 12:05:07 +00:00
|
|
|
const userLikedItemIds = likes
|
|
|
|
?.filter((l) => l.type === 'contract' || l.type === 'post')
|
2022-08-30 15:38:59 +00:00
|
|
|
.map((l) => l.id)
|
|
|
|
|
|
|
|
const onLike = async () => {
|
2022-08-30 23:13:25 +00:00
|
|
|
if (!user) return firebaseLogin()
|
2022-10-01 20:06:09 +00:00
|
|
|
|
|
|
|
setIsLiking(true)
|
2022-10-14 12:05:07 +00:00
|
|
|
|
2022-10-14 06:47:51 +00:00
|
|
|
const timeoutId = setTimeout(() => {
|
2022-10-14 12:05:07 +00:00
|
|
|
likeItem(user, item, itemType).catch(() => setIsLiking(false))
|
2022-10-14 06:47:51 +00:00
|
|
|
}, 3000)
|
|
|
|
toast.custom(
|
|
|
|
() => (
|
|
|
|
<TipToast
|
2022-10-14 12:05:07 +00:00
|
|
|
userName={item.creatorUsername}
|
2022-10-14 06:47:51 +00:00
|
|
|
onUndoClick={() => {
|
|
|
|
clearTimeout(timeoutId)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
{ duration: TIP_UNDO_DURATION }
|
|
|
|
)
|
2022-08-30 15:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-10-01 20:51:08 +00:00
|
|
|
<TipButton
|
|
|
|
onClick={onLike}
|
|
|
|
tipAmount={LIKE_TIP_AMOUNT}
|
|
|
|
totalTipped={totalTipped}
|
|
|
|
userTipped={
|
|
|
|
!!user &&
|
|
|
|
(isLiking ||
|
2022-10-14 12:05:07 +00:00
|
|
|
userLikedItemIds?.includes(item.id) ||
|
|
|
|
(!likes && !!item.likedByUserIds?.includes(user.id)))
|
2022-10-01 20:51:08 +00:00
|
|
|
}
|
2022-10-14 12:05:07 +00:00
|
|
|
disabled={item.creatorId === user?.id}
|
2022-10-01 20:51:08 +00:00
|
|
|
/>
|
2022-08-30 15:38:59 +00:00
|
|
|
)
|
|
|
|
}
|