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'
|
2022-07-10 22:03:15 +00:00
|
|
|
import { transact } from 'web/lib/firebase/api'
|
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(() =>
|
2022-08-10 06:05:56 +00:00
|
|
|
debounce(async (user: User, comment: Comment, change: number) => {
|
2022-06-18 03:28:16 +00:00
|
|
|
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,
|
2022-07-07 23:23:13 +00:00
|
|
|
groupId: comment.groupId,
|
2022-06-18 03:28:16 +00:00
|
|
|
},
|
|
|
|
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,
|
2022-07-07 23:23:13 +00:00
|
|
|
groupId: comment.groupId,
|
2022-06-21 15:36:44 +00:00
|
|
|
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])
|
|
|
|
|
2022-08-10 06:05:56 +00:00
|
|
|
const addTip = (delta: number) => {
|
|
|
|
setLocalTip(localTip + delta)
|
|
|
|
me && saveTip(me, comment, localTip - savedTip + delta)
|
2022-06-18 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 06:05:56 +00:00
|
|
|
const canDown = me && localTip > savedTip
|
|
|
|
const canUp = me && me.id !== comment.userId && me.balance >= localTip + 5
|
2022-06-18 03:28:16 +00:00
|
|
|
return (
|
|
|
|
<Row className="items-center gap-0.5">
|
2022-08-10 06:05:56 +00:00
|
|
|
<DownTip onClick={canDown ? () => addTip(-5) : undefined} />
|
2022-06-27 16:18:15 +00:00
|
|
|
<span className="font-bold">{Math.floor(total)}</span>
|
2022-08-10 06:05:56 +00:00
|
|
|
<UpTip onClick={canUp ? () => addTip(+5) : undefined} value={localTip} />
|
2022-06-18 03:28:16 +00:00
|
|
|
{localTip === 0 ? (
|
|
|
|
''
|
|
|
|
) : (
|
|
|
|
<span
|
|
|
|
className={clsx(
|
2022-08-10 06:05:56 +00:00
|
|
|
'ml-1 font-semibold',
|
2022-06-18 03:28:16 +00:00
|
|
|
localTip > 0 ? 'text-primary' : 'text-red-400'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
({formatMoney(localTip)} tip)
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-10 06:05:56 +00:00
|
|
|
function DownTip(props: { onClick?: () => void }) {
|
|
|
|
const { onClick } = props
|
2022-06-18 03:28:16 +00:00
|
|
|
return (
|
|
|
|
<Tooltip
|
2022-08-10 06:05:56 +00:00
|
|
|
className="tooltip-bottom h-6 w-6"
|
|
|
|
text={onClick && `-${formatMoney(5)}`}
|
2022-06-18 03:28:16 +00:00
|
|
|
>
|
|
|
|
<button
|
2022-08-10 06:05:56 +00:00
|
|
|
className="hover:text-red-600 disabled:text-gray-300"
|
|
|
|
disabled={!onClick}
|
|
|
|
onClick={onClick}
|
2022-06-18 03:28:16 +00:00
|
|
|
>
|
|
|
|
<ChevronLeftIcon className="h-6 w-6" />
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-10 06:05:56 +00:00
|
|
|
function UpTip(props: { onClick?: () => void; value: number }) {
|
|
|
|
const { onClick, value } = props
|
|
|
|
const IconKind = value >= 10 ? ChevronDoubleRightIcon : ChevronRightIcon
|
2022-06-18 03:28:16 +00:00
|
|
|
return (
|
|
|
|
<Tooltip
|
2022-08-10 06:05:56 +00:00
|
|
|
className="tooltip-bottom h-6 w-6"
|
|
|
|
text={onClick && `Tip ${formatMoney(5)}`}
|
2022-06-18 03:28:16 +00:00
|
|
|
>
|
|
|
|
<button
|
2022-08-10 06:05:56 +00:00
|
|
|
className="hover:text-primary disabled:text-gray-300"
|
|
|
|
disabled={!onClick}
|
|
|
|
onClick={onClick}
|
2022-06-18 03:28:16 +00:00
|
|
|
>
|
2022-08-10 06:05:56 +00:00
|
|
|
<IconKind className={clsx('h-6 w-6', value ? 'text-primary' : '')} />
|
2022-06-18 03:28:16 +00:00
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
)
|
|
|
|
}
|