getting rid of debounce
This commit is contained in:
parent
7822fabf0f
commit
f083826885
|
@ -6,3 +6,4 @@ export type Like = {
|
||||||
tipTxnId?: string // only holds most recent tip txn id
|
tipTxnId?: string // only holds most recent tip txn id
|
||||||
}
|
}
|
||||||
export const LIKE_TIP_AMOUNT = 10
|
export const LIKE_TIP_AMOUNT = 10
|
||||||
|
export const TIP_UNDO_DURATION = 2000
|
||||||
|
|
|
@ -5,11 +5,12 @@ import { useUserLikes } from 'web/hooks/use-likes'
|
||||||
import toast from 'react-hot-toast'
|
import toast from 'react-hot-toast'
|
||||||
import { formatMoney } from 'common/util/format'
|
import { formatMoney } from 'common/util/format'
|
||||||
import { likeContract } from 'web/lib/firebase/likes'
|
import { likeContract } from 'web/lib/firebase/likes'
|
||||||
import { LIKE_TIP_AMOUNT } from 'common/like'
|
import { LIKE_TIP_AMOUNT, TIP_UNDO_DURATION } from 'common/like'
|
||||||
import { firebaseLogin } from 'web/lib/firebase/users'
|
import { firebaseLogin } from 'web/lib/firebase/users'
|
||||||
import { useMarketTipTxns } from 'web/hooks/use-tip-txns'
|
import { useMarketTipTxns } from 'web/hooks/use-tip-txns'
|
||||||
import { sum } from 'lodash'
|
import { sum } from 'lodash'
|
||||||
import { TipButton } from './tip-button'
|
import { TipButton } from './tip-button'
|
||||||
|
import { TipToast } from '../tipper'
|
||||||
|
|
||||||
export function LikeMarketButton(props: {
|
export function LikeMarketButton(props: {
|
||||||
contract: Contract
|
contract: Contract
|
||||||
|
@ -35,8 +36,20 @@ export function LikeMarketButton(props: {
|
||||||
if (!user) return firebaseLogin()
|
if (!user) return firebaseLogin()
|
||||||
|
|
||||||
setIsLiking(true)
|
setIsLiking(true)
|
||||||
likeContract(user, contract).catch(() => setIsLiking(false))
|
const timeoutId = setTimeout(() => {
|
||||||
toast(`You tipped ${contract.creatorName} ${formatMoney(LIKE_TIP_AMOUNT)}!`)
|
likeContract(user, contract).catch(() => setIsLiking(false))
|
||||||
|
}, 3000)
|
||||||
|
toast.custom(
|
||||||
|
(t) => (
|
||||||
|
<TipToast
|
||||||
|
userName={contract.creatorUsername}
|
||||||
|
onUndoClick={() => {
|
||||||
|
clearTimeout(timeoutId)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
{ duration: TIP_UNDO_DURATION }
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -9,7 +9,7 @@ import { transact } from 'web/lib/firebase/api'
|
||||||
import { track } from 'web/lib/service/analytics'
|
import { track } from 'web/lib/service/analytics'
|
||||||
import { TipButton } from './contract/tip-button'
|
import { TipButton } from './contract/tip-button'
|
||||||
import { Row } from './layout/row'
|
import { Row } from './layout/row'
|
||||||
import { LIKE_TIP_AMOUNT } from 'common/like'
|
import { LIKE_TIP_AMOUNT, TIP_UNDO_DURATION } from 'common/like'
|
||||||
import { formatMoney } from 'common/util/format'
|
import { formatMoney } from 'common/util/format'
|
||||||
import { Button } from './button'
|
import { Button } from './button'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
|
@ -37,8 +37,8 @@ export function Tipper(prop: {
|
||||||
const total = totalTip - myTip + localTip
|
const total = totalTip - myTip + localTip
|
||||||
|
|
||||||
// declare debounced function only on first render
|
// declare debounced function only on first render
|
||||||
const [saveTip] = useState(() =>
|
const [saveTip] = useState(
|
||||||
debounce(async (user: User, comment: Comment, change: number) => {
|
() => async (user: User, comment: Comment, change: number) => {
|
||||||
if (change === 0) {
|
if (change === 0) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -69,40 +69,25 @@ export function Tipper(prop: {
|
||||||
fromId: user.id,
|
fromId: user.id,
|
||||||
toId: comment.userId,
|
toId: comment.userId,
|
||||||
})
|
})
|
||||||
}, 1500)
|
}
|
||||||
)
|
)
|
||||||
// instant save on unrender
|
|
||||||
useEffect(() => () => void saveTip.flush(), [saveTip])
|
|
||||||
|
|
||||||
const addTip = (delta: number) => {
|
const addTip = (delta: number) => {
|
||||||
let cancelled = false
|
|
||||||
setLocalTip(localTip + delta)
|
|
||||||
const timeoutId = setTimeout(() => {
|
const timeoutId = setTimeout(() => {
|
||||||
|
setLocalTip(localTip + delta)
|
||||||
me && saveTip(me, comment, localTip - myTip + delta)
|
me && saveTip(me, comment, localTip - myTip + delta)
|
||||||
}, 5000)
|
}, 3000)
|
||||||
toast.custom((t) => (
|
toast.custom(
|
||||||
<Row className="text-greyscale-6 items-center gap-4 rounded-lg bg-white px-4 py-2 text-sm drop-shadow-md">
|
(t) => (
|
||||||
<div className={clsx(cancelled ? 'hidden' : 'inline')}>
|
<TipToast
|
||||||
You tipped {comment.userName} {formatMoney(LIKE_TIP_AMOUNT)}!
|
userName={comment.userName}
|
||||||
</div>
|
onUndoClick={() => {
|
||||||
<div className={clsx('py-1', cancelled ? 'inline' : 'hidden')}>
|
|
||||||
Cancelled tipping
|
|
||||||
</div>
|
|
||||||
<Button
|
|
||||||
className={clsx(cancelled ? 'hidden' : 'inline')}
|
|
||||||
size="xs"
|
|
||||||
color="gray-outline"
|
|
||||||
onClick={() => {
|
|
||||||
clearTimeout(timeoutId)
|
clearTimeout(timeoutId)
|
||||||
setLocalTip(localTip)
|
|
||||||
cancelled = true
|
|
||||||
}}
|
}}
|
||||||
disabled={cancelled}
|
/>
|
||||||
>
|
),
|
||||||
Undo
|
{ duration: TIP_UNDO_DURATION }
|
||||||
</Button>
|
)
|
||||||
</Row>
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const canUp =
|
const canUp =
|
||||||
|
@ -121,3 +106,38 @@ export function Tipper(prop: {
|
||||||
</Row>
|
</Row>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function TipToast(props: { userName: string; onUndoClick: () => void }) {
|
||||||
|
const { userName, onUndoClick } = props
|
||||||
|
const [cancelled, setCancelled] = useState(false)
|
||||||
|
return (
|
||||||
|
<div className="relative overflow-hidden rounded-lg bg-white drop-shadow-md">
|
||||||
|
<div
|
||||||
|
className={clsx(
|
||||||
|
'animate-progress-loading absolute bottom-0 z-10 h-1 w-full bg-indigo-600',
|
||||||
|
cancelled ? 'hidden' : ''
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<Row className="text-greyscale-6 items-center gap-4 px-4 py-2 text-sm">
|
||||||
|
<div className={clsx(cancelled ? 'hidden' : 'inline')}>
|
||||||
|
Tipping {userName} {formatMoney(LIKE_TIP_AMOUNT)}...
|
||||||
|
</div>
|
||||||
|
<div className={clsx('py-1', cancelled ? 'inline' : 'hidden')}>
|
||||||
|
Cancelled tipping
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
className={clsx(cancelled ? 'hidden' : 'inline')}
|
||||||
|
size="xs"
|
||||||
|
color="gray-outline"
|
||||||
|
onClick={() => {
|
||||||
|
onUndoClick()
|
||||||
|
setCancelled(true)
|
||||||
|
}}
|
||||||
|
disabled={cancelled}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</Row>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
|
@ -15,6 +15,15 @@ module.exports = {
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
extend: {
|
extend: {
|
||||||
|
keyframes: {
|
||||||
|
progress: {
|
||||||
|
'0%': { width: '0%' },
|
||||||
|
'100%': { width: '100%' },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
animation: {
|
||||||
|
'progress-loading': 'progress 2s linear',
|
||||||
|
},
|
||||||
colors: {
|
colors: {
|
||||||
primary: '#11b981',
|
primary: '#11b981',
|
||||||
'primary-focus': '#069668',
|
'primary-focus': '#069668',
|
||||||
|
|
Loading…
Reference in New Issue
Block a user