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'
|
2021-04-26 21:55:15 +00:00
|
|
|
import { gql, useMutation } from '@apollo/client'
|
2021-09-24 23:04:59 +00:00
|
|
|
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'
|
2021-10-28 21:59:45 +00:00
|
|
|
import { useState } from 'react'
|
2021-10-30 16:52:24 +00:00
|
|
|
import LongPressable from 'react-longpressable'
|
2021-04-26 21:55:15 +00:00
|
|
|
|
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()
|
2021-10-28 21:59:45 +00:00
|
|
|
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(
|
2021-04-26 21:55:15 +00:00
|
|
|
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-04-26 21:55:15 +00:00
|
|
|
}`, {
|
2021-09-10 21:13:52 +00:00
|
|
|
update (cache, { data: { act: { act, sats } } }) {
|
2021-09-07 12:59:27 +00:00
|
|
|
// read in the cached object so we don't use meSats prop
|
|
|
|
// which can be stale
|
2021-04-26 21:55:15 +00:00
|
|
|
cache.modify({
|
2021-09-10 21:13:52 +00:00
|
|
|
id: `Item:${item.id}`,
|
2021-04-26 21:55:15 +00:00
|
|
|
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
|
|
|
},
|
2021-04-26 21:55:15 +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-26 21:55:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
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) => {
|
2021-10-30 19:19:49 +00:00
|
|
|
e.preventDefault()
|
2021-10-30 16:52:24 +00:00
|
|
|
e.stopPropagation()
|
|
|
|
if (!item || voteLock) return
|
2021-10-30 19:19:49 +00:00
|
|
|
console.log('hi1')
|
2021-10-30 16:52:24 +00:00
|
|
|
|
|
|
|
// we can't tip ourselves
|
|
|
|
if (noSelfTips) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setItem({ itemId: item.id, act, strike })
|
|
|
|
}
|
|
|
|
}
|
2021-10-30 17:04:32 +00:00
|
|
|
onShortPress={
|
2021-09-24 23:04:59 +00:00
|
|
|
me
|
2021-06-24 23:56:01 +00:00
|
|
|
? async (e) => {
|
2021-10-30 19:19:49 +00:00
|
|
|
e.preventDefault()
|
2021-06-24 23:56:01 +00:00
|
|
|
e.stopPropagation()
|
2021-10-28 21:59:45 +00:00
|
|
|
if (!item || voteLock) return
|
2021-10-30 19:19:49 +00:00
|
|
|
console.log('hi2')
|
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()
|
2021-10-28 21:59:45 +00:00
|
|
|
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 {
|
2021-10-28 21:59:45 +00:00
|
|
|
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
|
|
|
|
}
|
2021-05-20 19:41:21 +00:00
|
|
|
throw new Error({ message: error.toString() })
|
2021-10-28 21:59:45 +00:00
|
|
|
} finally {
|
|
|
|
setVoteLock(false)
|
2021-05-20 19:41:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
: 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={
|
2021-10-30 19:19:49 +00:00
|
|
|
`${styles.upvote}
|
|
|
|
${className || ''}
|
|
|
|
${noSelfTips ? styles.noSelfTips : ''}
|
|
|
|
${item?.meVote ? styles.voted : ''}`
|
|
|
|
}
|
|
|
|
onClick={e => {
|
|
|
|
e.stopPropagation()
|
|
|
|
}}
|
2021-10-30 16:52:24 +00:00
|
|
|
/>
|
2021-10-30 17:04:32 +00:00
|
|
|
</ActionTooltip>
|
|
|
|
</LongPressable>}
|
2021-04-22 22:14:32 +00:00
|
|
|
</LightningConsumer>
|
|
|
|
)
|
|
|
|
}
|