stacker.news/components/upvote.js

289 lines
8.7 KiB
JavaScript
Raw Normal View History

2021-12-05 17:37:55 +00:00
import UpBolt from '../svgs/bolt.svg'
2021-04-22 22:14:32 +00:00
import styles from './upvote.module.css'
import { gql, useMutation } from '@apollo/client'
import FundError from './fund-error'
2021-07-08 18:42:57 +00:00
import ActionTooltip from './action-tooltip'
import ItemAct from './item-act'
2021-09-12 16:55:38 +00:00
import { useMe } from './me'
import Rainbow from '../lib/rainbow'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
2021-10-30 16:52:24 +00:00
import LongPressable from 'react-longpressable'
2023-07-24 18:35:05 +00:00
import Overlay from 'react-bootstrap/Overlay'
import Popover from 'react-bootstrap/Popover'
import { useShowModal } from './modal'
import { useRouter } from 'next/router'
2023-07-05 14:47:44 +00:00
import { LightningConsumer } from './lightning'
2021-12-09 20:40:40 +00:00
const getColor = (meSats) => {
if (!meSats || meSats <= 10) {
2023-07-24 18:35:05 +00:00
return 'var(--bs-secondary)'
2021-12-09 20:40:40 +00:00
}
const idx = Math.min(
Math.floor((Math.log(meSats) / Math.log(10000)) * (Rainbow.length - 1)),
2021-12-09 20:40:40 +00:00
Rainbow.length - 1)
return Rainbow[idx]
}
2022-04-12 21:09:12 +00:00
const UpvotePopover = ({ target, show, handleClose }) => {
const me = useMe()
return (
<Overlay
show={show}
target={target}
placement='right'
>
<Popover id='popover-basic'>
2023-07-24 18:35:05 +00:00
<Popover.Body className='d-flex justify-content-between alert-dismissible' as='h3'>Zapping
<button type='button' className='close' onClick={handleClose}><span aria-hidden='true'>×</span><span className='visually-hidden-focusable'>Close alert</span></button>
2023-07-24 18:35:05 +00:00
</Popover.Body>
<Popover.Body>
2023-06-19 18:21:55 +00:00
<div className='mb-2'>Press the bolt again to zap {me?.tipDefault || 1} more sat{me?.tipDefault > 1 ? 's' : ''}.</div>
<div>Repeatedly press the bolt to zap more sats.</div>
2023-07-24 18:35:05 +00:00
</Popover.Body>
2022-04-12 21:09:12 +00:00
</Popover>
</Overlay>
)
}
2021-12-09 20:40:40 +00:00
const TipPopover = ({ target, show, handleClose }) => (
<Overlay
show={show}
target={target}
placement='right'
>
<Popover id='popover-basic'>
2023-07-24 18:35:05 +00:00
<Popover.Body className='d-flex justify-content-between alert-dismissible' as='h3'>Press and hold
<button type='button' className='close' onClick={handleClose}><span aria-hidden='true'>×</span><span className='visually-hidden-focusable'>Close alert</span></button>
2023-07-24 18:35:05 +00:00
</Popover.Body>
<Popover.Body>
2023-06-19 18:21:55 +00:00
<div className='mb-2'>Press and hold bolt to zap a custom amount.</div>
<div>As you zap more, the bolt color follows the rainbow.</div>
2023-07-24 18:35:05 +00:00
</Popover.Body>
2021-12-09 20:40:40 +00:00
</Popover>
</Overlay>
)
export default function UpVote ({ item, className, pendingSats, setPendingSats }) {
const showModal = useShowModal()
const router = useRouter()
2021-12-09 20:40:40 +00:00
const [voteShow, _setVoteShow] = useState(false)
const [tipShow, _setTipShow] = useState(false)
const ref = useRef()
const timerRef = useRef(null)
2021-09-12 16:55:38 +00:00
const me = useMe()
2021-12-09 20:40:40 +00:00
const [setWalkthrough] = useMutation(
gql`
mutation setWalkthrough($upvotePopover: Boolean, $tipPopover: Boolean) {
setWalkthrough(upvotePopover: $upvotePopover, tipPopover: $tipPopover)
}`
)
const setVoteShow = useCallback((yes) => {
2021-12-09 20:40:40 +00:00
if (!me) return
// if they haven't seen the walkthrough and they have sats
if (yes && !me.upvotePopover && me.sats) {
_setVoteShow(true)
2021-12-09 20:40:40 +00:00
}
if (voteShow && !yes) {
_setVoteShow(false)
2021-12-09 20:40:40 +00:00
setWalkthrough({ variables: { upvotePopover: true } })
}
}, [me, voteShow, setWalkthrough])
2021-12-09 20:40:40 +00:00
const setTipShow = useCallback((yes) => {
2021-12-09 20:40:40 +00:00
if (!me) return
// if we want to show it, yet we still haven't shown
if (yes && !me.tipPopover && me.sats) {
_setTipShow(true)
2021-12-09 20:40:40 +00:00
}
// if it's currently showing and we want to hide it
if (tipShow && !yes) {
_setTipShow(false)
2021-12-09 20:40:40 +00:00
setWalkthrough({ variables: { tipPopover: true } })
}
}, [me, tipShow, setWalkthrough])
2021-12-09 20:40:40 +00:00
2021-09-08 21:51:23 +00:00
const [act] = useMutation(
gql`
mutation act($id: ID!, $sats: Int!) {
act(id: $id, sats: $sats) {
2021-09-10 21:13:52 +00:00
sats
}
}`, {
update (cache, { data: { act: { sats } } }) {
cache.modify({
2021-09-10 21:13:52 +00:00
id: `Item:${item.id}`,
fields: {
sats (existingSats = 0) {
return existingSats + sats
2021-04-27 21:30:58 +00:00
},
2021-12-05 17:37:55 +00:00
meSats (existingSats = 0) {
if (sats <= me.sats) {
if (existingSats === 0) {
setVoteShow(true)
} else {
setTipShow(true)
}
2021-09-10 21:13:52 +00:00
}
return existingSats + sats
}
}
})
// update all ancestors
item.path.split('.').forEach(id => {
if (Number(id) === Number(item.id)) return
cache.modify({
id: `Item:${id}`,
fields: {
commentSats (existingCommentSats = 0) {
return existingCommentSats + sats
}
}
})
})
}
}
)
2021-04-22 22:14:32 +00:00
// if we want to use optimistic response, we need to buffer the votes
// because if someone votes in quick succession, responses come back out of order
// so we wait a bit to see if there are more votes coming in
// this effectively performs our own debounced optimistic response
useEffect(() => {
if (timerRef.current) {
clearTimeout(timerRef.current)
}
if (pendingSats > 0) {
timerRef.current = setTimeout(async (sats) => {
try {
timerRef.current && setPendingSats(0)
await act({
variables: { id: item.id, sats },
optimisticResponse: {
act: {
sats
}
}
})
} catch (error) {
if (!timerRef.current) return
if (error.toString().includes('insufficient funds')) {
showModal(onClose => {
return <FundError onClose={onClose} />
})
return
}
throw new Error({ message: error.toString() })
}
}, 500, pendingSats)
}
return async () => {
clearTimeout(timerRef.current)
timerRef.current = null
}
}, [pendingSats, act, item, showModal, setPendingSats])
const disabled = useMemo(() => {
return item?.mine || (me && Number(me.id) === item?.fwdUserId) || item?.deletedAt
}, [me?.id, item?.fwdUserId, item?.mine, item?.deletedAt])
const [meSats, sats, overlayText, color] = useMemo(() => {
const meSats = (item?.meSats || 0) + pendingSats
2022-12-09 19:25:38 +00:00
// what should our next tip be?
let sats = me?.tipDefault || 1
if (me?.turboTipping && me) {
let raiseTip = sats
while (meSats >= raiseTip) {
raiseTip *= 10
}
2022-12-09 19:25:38 +00:00
sats = raiseTip - meSats
}
2021-09-12 16:55:38 +00:00
return [meSats, sats, `${sats} sat${sats > 1 ? 's' : ''}`, getColor(meSats)]
}, [item?.meSats, pendingSats, me?.tipDefault, me?.turboDefault])
2023-01-12 23:53:09 +00:00
2021-04-22 22:14:32 +00:00
return (
2023-07-05 14:47:44 +00:00
<LightningConsumer>
{(strike) =>
<div ref={ref} className='upvoteParent'>
2021-12-09 20:40:40 +00:00
<LongPressable
onLongPress={
2021-10-30 16:52:24 +00:00
async (e) => {
2022-01-20 23:23:21 +00:00
if (!item) return
2021-10-30 16:52:24 +00:00
// we can't tip ourselves
2023-01-12 23:53:09 +00:00
if (disabled) {
2021-10-30 16:52:24 +00:00
return
}
2021-12-09 20:40:40 +00:00
setTipShow(false)
showModal(onClose =>
<ItemAct onClose={onClose} itemId={item.id} act={act} strike={strike} />)
2021-10-30 16:52:24 +00:00
}
}
2021-12-09 20:40:40 +00:00
onShortPress={
me
2021-06-24 23:56:01 +00:00
? async (e) => {
2023-07-25 14:14:45 +00:00
if (!item) return
2021-09-12 16:55:38 +00:00
2023-07-25 14:14:45 +00:00
// we can't tip ourselves
if (disabled) {
return
}
2021-09-12 16:55:38 +00:00
2023-07-25 14:14:45 +00:00
if (meSats) {
setVoteShow(false)
}
2021-09-10 18:55:36 +00:00
2023-07-25 14:14:45 +00:00
strike()
2021-09-10 18:55:36 +00:00
2023-07-25 14:14:45 +00:00
setPendingSats(pendingSats + sats)
}
: async () => await router.push({
pathname: '/signup',
query: { callbackUrl: window.location.origin + router.asPath }
})
}
2021-12-09 20:40:40 +00:00
>
<ActionTooltip notForm disable={disabled} overlayText={overlayText}>
2021-12-09 20:40:40 +00:00
<div
className={`${disabled ? styles.noSelfTips : ''} ${styles.upvoteWrapper}`}
2021-12-09 20:40:40 +00:00
>
2021-12-06 15:32:33 +00:00
<UpBolt
2023-07-25 18:32:48 +00:00
width={26}
height={26}
2021-12-06 15:32:33 +00:00
className={
2021-12-09 20:40:40 +00:00
`${styles.upvote}
${className || ''}
2023-01-12 23:53:09 +00:00
${disabled ? styles.noSelfTips : ''}
${meSats ? styles.voted : ''}`
2021-12-09 20:40:40 +00:00
}
style={meSats
2021-12-06 15:32:33 +00:00
? {
fill: color,
filter: `drop-shadow(0 0 6px ${color}90)`
}
: undefined}
/>
2021-12-09 20:40:40 +00:00
</div>
</ActionTooltip>
</LongPressable>
<TipPopover target={ref.current} show={tipShow} handleClose={() => setTipShow(false)} />
<UpvotePopover target={ref.current} show={voteShow} handleClose={() => setVoteShow(false)} />
</div>}
2023-07-05 14:47:44 +00:00
</LightningConsumer>
2021-04-22 22:14:32 +00:00
)
}