2022-06-18 03:28:16 +00:00
|
|
|
import {
|
|
|
|
ChevronDoubleRightIcon,
|
|
|
|
ChevronLeftIcon,
|
|
|
|
ChevronRightIcon,
|
|
|
|
} from '@heroicons/react/solid'
|
|
|
|
import clsx from 'clsx'
|
|
|
|
import { Comment } from 'common/comment'
|
|
|
|
import { User } from 'common/user'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
2022-06-27 16:18:15 +00:00
|
|
|
import { debounce, sum } from 'lodash'
|
|
|
|
import { useEffect, useRef, useState } from 'react'
|
2022-06-18 03:28:16 +00:00
|
|
|
import { CommentTips } from 'web/hooks/use-tip-txns'
|
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { transact } from 'web/lib/firebase/fn-call'
|
2022-06-21 15:36:44 +00:00
|
|
|
import { track } from 'web/lib/service/analytics'
|
2022-06-18 03:28:16 +00:00
|
|
|
import { Row } from './layout/row'
|
|
|
|
import { Tooltip } from './tooltip'
|
|
|
|
|
|
|
|
export function Tipper(prop: { comment: Comment; tips: CommentTips }) {
|
|
|
|
const { comment, tips } = prop
|
|
|
|
|
|
|
|
const me = useUser()
|
|
|
|
const myId = me?.id ?? ''
|
2022-06-27 16:18:15 +00:00
|
|
|
const savedTip = tips[myId] ?? 0
|
2022-06-18 03:28:16 +00:00
|
|
|
|
2022-06-27 16:18:15 +00:00
|
|
|
const [localTip, setLocalTip] = useState(savedTip)
|
|
|
|
// listen for user being set
|
2022-06-18 03:28:16 +00:00
|
|
|
const initialized = useRef(false)
|
|
|
|
useEffect(() => {
|
2022-06-27 16:18:15 +00:00
|
|
|
if (tips[myId] && !initialized.current) {
|
|
|
|
setLocalTip(tips[myId])
|
2022-06-18 03:28:16 +00:00
|
|
|
initialized.current = true
|
|
|
|
}
|
2022-06-27 16:18:15 +00:00
|
|
|
}, [tips, myId])
|
2022-06-18 03:28:16 +00:00
|
|
|
|
2022-06-27 16:18:15 +00:00
|
|
|
const total = sum(Object.values(tips)) - savedTip + localTip
|
2022-06-18 03:28:16 +00:00
|
|
|
|
|
|
|
// declare debounced function only on first render
|
|
|
|
const [saveTip] = useState(() =>
|
|
|
|
debounce(async (user: User, change: number) => {
|
|
|
|
if (change === 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
await transact({
|
|
|
|
amount: change,
|
|
|
|
fromId: user.id,
|
|
|
|
fromType: 'USER',
|
|
|
|
toId: comment.userId,
|
|
|
|
toType: 'USER',
|
|
|
|
token: 'M$',
|
|
|
|
category: 'TIP',
|
|
|
|
data: {
|
|
|
|
contractId: comment.contractId,
|
|
|
|
commentId: comment.id,
|
|
|
|
},
|
|
|
|
description: `${user.name} tipped M$ ${change} to ${comment.userName} for a comment`,
|
|
|
|
})
|
2022-06-21 15:36:44 +00:00
|
|
|
|
|
|
|
track('send comment tip', {
|
|
|
|
contractId: comment.contractId,
|
|
|
|
commentId: comment.id,
|
|
|
|
amount: change,
|
|
|
|
fromId: user.id,
|
|
|
|
toId: comment.userId,
|
|
|
|
})
|
2022-06-18 03:28:16 +00:00
|
|
|
}, 1500)
|
|
|
|
)
|
|
|
|
// instant save on unrender
|
|
|
|
useEffect(() => () => void saveTip.flush(), [saveTip])
|
|
|
|
|
|
|
|
const changeTip = (tip: number) => {
|
|
|
|
setLocalTip(tip)
|
2022-06-27 16:18:15 +00:00
|
|
|
me && saveTip(me, tip - savedTip)
|
2022-06-18 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Row className="items-center gap-0.5">
|
|
|
|
<DownTip
|
|
|
|
value={localTip}
|
|
|
|
onChange={changeTip}
|
2022-06-27 16:18:15 +00:00
|
|
|
disabled={!me || localTip <= savedTip}
|
2022-06-18 03:28:16 +00:00
|
|
|
/>
|
2022-06-27 16:18:15 +00:00
|
|
|
<span className="font-bold">{Math.floor(total)}</span>
|
2022-06-18 03:28:16 +00:00
|
|
|
<UpTip
|
|
|
|
value={localTip}
|
|
|
|
onChange={changeTip}
|
2022-06-27 16:18:15 +00:00
|
|
|
disabled={!me || me.id === comment.userId || me.balance < localTip + 5}
|
2022-06-18 03:28:16 +00:00
|
|
|
/>
|
|
|
|
{localTip === 0 ? (
|
|
|
|
''
|
|
|
|
) : (
|
|
|
|
<span
|
|
|
|
className={clsx(
|
|
|
|
'font-semibold',
|
|
|
|
localTip > 0 ? 'text-primary' : 'text-red-400'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
({formatMoney(localTip)} tip)
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function DownTip(prop: {
|
|
|
|
value: number
|
|
|
|
onChange: (tip: number) => void
|
|
|
|
disabled?: boolean
|
|
|
|
}) {
|
|
|
|
const { onChange, value, disabled } = prop
|
|
|
|
return (
|
|
|
|
<Tooltip
|
|
|
|
className="tooltip-bottom"
|
2022-06-27 16:18:15 +00:00
|
|
|
text={!disabled && `-${formatMoney(5)}`}
|
2022-06-18 03:28:16 +00:00
|
|
|
>
|
|
|
|
<button
|
|
|
|
className="flex h-max items-center hover:text-red-600 disabled:text-gray-300"
|
|
|
|
disabled={disabled}
|
2022-06-27 16:18:15 +00:00
|
|
|
onClick={() => onChange(value - 5)}
|
2022-06-18 03:28:16 +00:00
|
|
|
>
|
|
|
|
<ChevronLeftIcon className="h-6 w-6" />
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function UpTip(prop: {
|
|
|
|
value: number
|
|
|
|
onChange: (tip: number) => void
|
|
|
|
disabled?: boolean
|
|
|
|
}) {
|
|
|
|
const { onChange, value, disabled } = prop
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Tooltip
|
|
|
|
className="tooltip-bottom"
|
2022-06-27 16:18:15 +00:00
|
|
|
text={!disabled && `Tip ${formatMoney(5)}`}
|
2022-06-18 03:28:16 +00:00
|
|
|
>
|
|
|
|
<button
|
|
|
|
className="hover:text-primary flex h-max items-center disabled:text-gray-300"
|
|
|
|
disabled={disabled}
|
2022-06-27 16:18:15 +00:00
|
|
|
onClick={() => onChange(value + 5)}
|
2022-06-18 03:28:16 +00:00
|
|
|
>
|
2022-06-27 16:18:15 +00:00
|
|
|
{value >= 10 ? (
|
2022-06-18 03:28:16 +00:00
|
|
|
<ChevronDoubleRightIcon className="text-primary mx-1 h-6 w-6" />
|
|
|
|
) : value > 0 ? (
|
|
|
|
<ChevronRightIcon className="text-primary h-6 w-6" />
|
|
|
|
) : (
|
|
|
|
<ChevronRightIcon className="h-6 w-6" />
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
)
|
|
|
|
}
|