stacker.news/components/upvote.js

171 lines
5.0 KiB
JavaScript
Raw Normal View History

2021-04-22 22:14:32 +00:00
import { LightningConsumer } from './lightning'
import UpArrow from '../svgs/lightning-arrow.svg'
import styles from './upvote.module.css'
import { gql, useMutation } from '@apollo/client'
import { signIn } from 'next-auth/client'
2021-05-20 21:32:59 +00:00
import { useFundError } from './fund-error'
2021-07-08 18:42:57 +00:00
import ActionTooltip from './action-tooltip'
2021-09-10 18:55:36 +00:00
import { useItemAct } from './item-act'
2021-09-10 21:13:52 +00:00
import Window from '../svgs/window-2-fill.svg'
2021-09-12 16:55:38 +00:00
import { useMe } from './me'
import { useState } from 'react'
2021-10-30 16:52:24 +00:00
import LongPressable from 'react-longpressable'
2021-09-10 21:13:52 +00:00
export default function UpVote ({ item, className }) {
2021-05-20 21:32:59 +00:00
const { setError } = useFundError()
2021-09-10 18:55:36 +00:00
const { setItem } = useItemAct()
const [voteLock, setVoteLock] = useState()
2021-09-12 16:55:38 +00:00
const me = useMe()
2021-09-08 21:51:23 +00:00
const [act] = useMutation(
gql`
2021-09-12 16:55:38 +00:00
mutation act($id: ID!, $act: ItemAct! $sats: Int!, $tipDefault: Boolean) {
act(id: $id, act: $act, sats: $sats, tipDefault: $tipDefault) {
2021-09-10 21:13:52 +00:00
act,
sats
}
}`, {
2021-09-10 21:13:52 +00:00
update (cache, { data: { act: { act, sats } } }) {
// read in the cached object so we don't use meSats prop
// which can be stale
cache.modify({
2021-09-10 21:13:52 +00:00
id: `Item:${item.id}`,
fields: {
2021-09-10 21:13:52 +00:00
meVote (existingMeVote = 0) {
if (act === 'VOTE') {
return existingMeVote + sats
}
return existingMeVote
},
meBoost (existingMeBoost = 0) {
if (act === 'BOOST') {
return existingMeBoost + sats
}
return existingMeBoost
},
meTip (existingMeTip = 0) {
if (act === 'TIP') {
return existingMeTip + sats
}
return existingMeTip
2021-06-27 03:09:39 +00:00
},
sats (existingSats = 0) {
2021-09-10 21:13:52 +00:00
if (act === 'VOTE') {
return existingSats + sats
}
return existingSats
2021-04-27 21:30:58 +00:00
},
boost (existingBoost = 0) {
2021-09-10 21:13:52 +00:00
if (act === 'BOOST') {
return existingBoost + sats
}
return existingBoost
},
tips (existingTips = 0) {
if (act === 'TIP') {
return existingTips + sats
}
return existingTips
}
}
})
}
}
)
2021-04-22 22:14:32 +00:00
2021-09-12 16:55:38 +00:00
const overlayText = () => {
if (item?.meVote) {
if (me?.tipDefault) {
return `tip ${me.tipDefault}`
}
return <Window style={{ fill: '#fff' }} width={18} height={18} />
}
return '1 sat'
}
const noSelfTips = item?.meVote && item?.user?.id === me?.id
2021-04-22 22:14:32 +00:00
return (
<LightningConsumer>
{({ strike }) =>
2021-10-30 17:04:32 +00:00
<LongPressable
onLongPress={
2021-10-30 16:52:24 +00:00
async (e) => {
e.stopPropagation()
if (!item || voteLock) return
// we can't tip ourselves
if (noSelfTips) {
return
}
2021-10-30 17:11:12 +00:00
if (window.getSelection) {
window.getSelection().removeAllRanges()
} else if (document.selection) {
document.selection.empty()
}
2021-10-30 16:52:24 +00:00
setItem({ itemId: item.id, act, strike })
}
}
2021-10-30 17:04:32 +00:00
onShortPress={
me
2021-06-24 23:56:01 +00:00
? async (e) => {
e.stopPropagation()
if (!item || voteLock) return
2021-09-12 16:55:38 +00:00
// we can't tip ourselves
if (noSelfTips) {
return
}
2021-09-10 21:13:52 +00:00
if (item?.meVote) {
2021-09-12 16:55:38 +00:00
if (me?.tipDefault) {
try {
strike()
await act({ variables: { id: item.id, act: 'TIP', sats: me.tipDefault } })
2021-09-12 16:55:38 +00:00
} catch (e) {
console.log(e)
}
return
}
2021-09-10 21:13:52 +00:00
setItem({ itemId: item.id, act, strike })
2021-09-10 18:55:36 +00:00
return
}
2021-06-24 23:56:01 +00:00
strike()
2021-09-10 18:55:36 +00:00
2021-05-20 21:32:59 +00:00
try {
setVoteLock(true)
2021-09-10 21:13:52 +00:00
await act({ variables: { id: item.id, act: 'VOTE', sats: 1 } })
2021-05-20 21:32:59 +00:00
} catch (error) {
if (error.toString().includes('insufficient funds')) {
setError(true)
return
}
throw new Error({ message: error.toString() })
} finally {
setVoteLock(false)
}
}
: signIn
}
2021-10-30 17:04:32 +00:00
>
<ActionTooltip notForm disable={noSelfTips} overlayText={overlayText()}>
2021-10-30 16:52:24 +00:00
<UpArrow
width={24}
height={24}
className={
`${styles.upvote}
${className || ''}
${noSelfTips ? styles.noSelfTips : ''}
${item?.meVote ? styles.voted : ''}`
}
/>
2021-10-30 17:04:32 +00:00
</ActionTooltip>
</LongPressable>}
2021-04-22 22:14:32 +00:00
</LightningConsumer>
)
}