2022-08-30 15:38:59 +00:00
|
|
|
import { HeartIcon } from '@heroicons/react/outline'
|
|
|
|
import { Button } from 'web/components/button'
|
2022-08-31 15:27:37 +00:00
|
|
|
import React, { useMemo } from 'react'
|
2022-08-30 15:38:59 +00:00
|
|
|
import { Contract } from 'common/contract'
|
|
|
|
import { User } from 'common/user'
|
|
|
|
import { useUserLikes } from 'web/hooks/use-likes'
|
|
|
|
import toast from 'react-hot-toast'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
2022-08-30 23:13:25 +00:00
|
|
|
import { likeContract } from 'web/lib/firebase/likes'
|
2022-08-30 15:38:59 +00:00
|
|
|
import { LIKE_TIP_AMOUNT } from 'common/like'
|
|
|
|
import clsx from 'clsx'
|
2022-08-30 23:13:25 +00:00
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import { firebaseLogin } from 'web/lib/firebase/users'
|
2022-08-31 15:27:37 +00:00
|
|
|
import { useMarketTipTxns } from 'web/hooks/use-tip-txns'
|
|
|
|
import { sum } from 'lodash'
|
2022-09-22 15:53:55 +00:00
|
|
|
import { Tooltip } from '../tooltip'
|
2022-08-30 15:38:59 +00:00
|
|
|
|
|
|
|
export function LikeMarketButton(props: {
|
|
|
|
contract: Contract
|
|
|
|
user: User | null | undefined
|
|
|
|
}) {
|
|
|
|
const { contract, user } = props
|
2022-08-31 15:27:37 +00:00
|
|
|
const tips = useMarketTipTxns(contract.id).filter(
|
|
|
|
(txn) => txn.fromId === user?.id
|
|
|
|
)
|
|
|
|
const totalTipped = useMemo(() => {
|
|
|
|
return sum(tips.map((tip) => tip.amount))
|
|
|
|
}, [tips])
|
2022-08-30 15:38:59 +00:00
|
|
|
const likes = useUserLikes(user?.id)
|
2022-08-30 23:13:25 +00:00
|
|
|
const userLikedContractIds = likes
|
2022-08-30 15:38:59 +00:00
|
|
|
?.filter((l) => l.type === 'contract')
|
|
|
|
.map((l) => l.id)
|
|
|
|
|
|
|
|
const onLike = async () => {
|
2022-08-30 23:13:25 +00:00
|
|
|
if (!user) return firebaseLogin()
|
2022-08-30 15:38:59 +00:00
|
|
|
await likeContract(user, contract)
|
|
|
|
toast(`You tipped ${contract.creatorName} ${formatMoney(LIKE_TIP_AMOUNT)}!`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-09-22 15:53:55 +00:00
|
|
|
<Tooltip
|
|
|
|
text={`Tip ${formatMoney(LIKE_TIP_AMOUNT)}`}
|
|
|
|
placement="bottom"
|
|
|
|
noTap
|
|
|
|
noFade
|
2022-08-30 15:38:59 +00:00
|
|
|
>
|
2022-09-22 15:53:55 +00:00
|
|
|
<Button
|
|
|
|
size={'sm'}
|
|
|
|
className={'max-w-xs self-center'}
|
|
|
|
color={'gray-white'}
|
|
|
|
onClick={onLike}
|
|
|
|
>
|
|
|
|
<Col className={'relative items-center sm:flex-row'}>
|
|
|
|
<HeartIcon
|
2022-09-16 00:41:25 +00:00
|
|
|
className={clsx(
|
2022-09-22 15:53:55 +00:00
|
|
|
'h-5 w-5 sm:h-6 sm:w-6',
|
|
|
|
totalTipped > 0 ? 'mr-2' : '',
|
|
|
|
user &&
|
|
|
|
(userLikedContractIds?.includes(contract.id) ||
|
|
|
|
(!likes && contract.likedByUserIds?.includes(user.id)))
|
|
|
|
? 'fill-red-500 text-red-500'
|
|
|
|
: ''
|
2022-09-16 00:41:25 +00:00
|
|
|
)}
|
2022-09-22 15:53:55 +00:00
|
|
|
/>
|
|
|
|
{totalTipped > 0 && (
|
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
'bg-greyscale-6 absolute ml-3.5 mt-2 h-4 w-4 rounded-full align-middle text-white sm:mt-3 sm:h-5 sm:w-5 sm:px-1',
|
|
|
|
totalTipped > 99
|
|
|
|
? 'text-[0.4rem] sm:text-[0.5rem]'
|
|
|
|
: 'sm:text-2xs text-[0.5rem]'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{totalTipped}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
</Button>
|
|
|
|
</Tooltip>
|
2022-08-30 15:38:59 +00:00
|
|
|
)
|
|
|
|
}
|