2023-08-09 22:02:43 +00:00
|
|
|
import { useEffect } from 'react'
|
2023-07-24 18:35:05 +00:00
|
|
|
import Table from 'react-bootstrap/Table'
|
2022-08-10 15:06:31 +00:00
|
|
|
import ActionTooltip from './action-tooltip'
|
|
|
|
import Info from './info'
|
|
|
|
import styles from './fee-button.module.css'
|
|
|
|
import { gql, useQuery } from '@apollo/client'
|
|
|
|
import { useFormikContext } from 'formik'
|
2023-08-07 20:10:20 +00:00
|
|
|
import { SSR, ANON_COMMENT_FEE, ANON_POST_FEE } from '../lib/constants'
|
2023-08-08 21:04:06 +00:00
|
|
|
import { numWithUnits } from '../lib/format'
|
2023-08-09 22:02:43 +00:00
|
|
|
import { useMe } from './me'
|
2023-08-10 19:41:56 +00:00
|
|
|
import AnonIcon from '../svgs/spy-fill.svg'
|
|
|
|
import { useShowModal } from './modal'
|
|
|
|
import Link from 'next/link'
|
2022-08-10 15:06:31 +00:00
|
|
|
|
|
|
|
function Receipt ({ cost, repetition, hasImgLink, baseFee, parentId, boost }) {
|
|
|
|
return (
|
|
|
|
<Table className={styles.receipt} borderless size='sm'>
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
2023-08-08 21:04:06 +00:00
|
|
|
<td>{numWithUnits(baseFee, { abbreviate: false })}</td>
|
2022-08-10 15:06:31 +00:00
|
|
|
<td align='right' className='font-weight-light'>{parentId ? 'reply' : 'post'} fee</td>
|
|
|
|
</tr>
|
|
|
|
{hasImgLink &&
|
|
|
|
<tr>
|
|
|
|
<td>x 10</td>
|
|
|
|
<td align='right' className='font-weight-light'>image/link fee</td>
|
|
|
|
</tr>}
|
|
|
|
{repetition > 0 &&
|
|
|
|
<tr>
|
|
|
|
<td>x 10<sup>{repetition}</sup></td>
|
|
|
|
<td className='font-weight-light' align='right'>{repetition} {parentId ? 'repeat or self replies' : 'posts'} in 10m</td>
|
|
|
|
</tr>}
|
|
|
|
{boost > 0 &&
|
|
|
|
<tr>
|
2023-08-08 21:04:06 +00:00
|
|
|
<td>+ {numWithUnits(boost, { abbreviate: false })}</td>
|
2022-08-10 15:06:31 +00:00
|
|
|
<td className='font-weight-light' align='right'>boost</td>
|
|
|
|
</tr>}
|
|
|
|
</tbody>
|
|
|
|
<tfoot>
|
|
|
|
<tr>
|
2023-08-08 21:04:06 +00:00
|
|
|
<td className='fw-bold'>{numWithUnits(cost, { abbreviate: false })}</td>
|
2022-08-10 15:06:31 +00:00
|
|
|
<td align='right' className='font-weight-light'>total fee</td>
|
|
|
|
</tr>
|
|
|
|
</tfoot>
|
|
|
|
</Table>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-10 19:41:56 +00:00
|
|
|
function AnonInfo () {
|
|
|
|
const showModal = useShowModal()
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AnonIcon
|
|
|
|
className='fill-muted ms-2 theme' height={22} width={22}
|
|
|
|
onClick={
|
|
|
|
(e) =>
|
|
|
|
showModal(onClose =>
|
2023-08-11 19:12:18 +00:00
|
|
|
<div><div className='fw-bold text-center'>You are posting without an account</div>
|
|
|
|
<ol className='my-3'>
|
2023-08-10 19:41:56 +00:00
|
|
|
<li>You'll pay by invoice</li>
|
|
|
|
<li>Your content will be content-joined (get it?!) under the <Link href='/anon' target='_blank'>@anon</Link> account</li>
|
|
|
|
<li>Any sats your content earns will go toward <Link href='/rewards' target='_blank'>rewards</Link></li>
|
2023-08-11 19:12:18 +00:00
|
|
|
<li>We won't be able to notify you when you receive replies</li>
|
2023-08-10 19:41:56 +00:00
|
|
|
</ol>
|
2023-08-11 23:19:35 +00:00
|
|
|
<small className='text-center fst-italic text-muted'>btw if you don't need to be anonymous, posting is cheaper with an account</small>
|
2023-08-10 19:41:56 +00:00
|
|
|
</div>)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-01 03:01:21 +00:00
|
|
|
export default function FeeButton ({ parentId, hasImgLink, baseFee, ChildButton, variant, text, alwaysShow, disabled }) {
|
2023-07-19 17:06:52 +00:00
|
|
|
const me = useMe()
|
|
|
|
baseFee = me ? baseFee : (parentId ? ANON_COMMENT_FEE : ANON_POST_FEE)
|
2022-08-10 15:06:31 +00:00
|
|
|
const query = parentId
|
|
|
|
? gql`{ itemRepetition(parentId: "${parentId}") }`
|
|
|
|
: gql`{ itemRepetition }`
|
2023-07-31 19:54:30 +00:00
|
|
|
const { data } = useQuery(query, SSR ? {} : { pollInterval: 1000, nextFetchPolicy: 'cache-and-network' })
|
2023-07-19 17:06:52 +00:00
|
|
|
const repetition = me ? data?.itemRepetition || 0 : 0
|
2022-08-10 15:06:31 +00:00
|
|
|
const formik = useFormikContext()
|
2023-07-24 22:50:12 +00:00
|
|
|
const boost = Number(formik?.values?.boost) || 0
|
2022-08-10 15:06:31 +00:00
|
|
|
const cost = baseFee * (hasImgLink ? 10 : 1) * Math.pow(10, repetition) + Number(boost)
|
|
|
|
|
2023-07-20 22:34:39 +00:00
|
|
|
useEffect(() => {
|
2023-08-12 00:40:37 +00:00
|
|
|
formik?.setFieldValue('cost', cost)
|
|
|
|
}, [formik?.getFieldProps('cost').value, cost])
|
2023-07-20 22:34:39 +00:00
|
|
|
|
2022-08-10 15:06:31 +00:00
|
|
|
const show = alwaysShow || !formik?.isSubmitting
|
|
|
|
return (
|
2023-08-10 19:41:56 +00:00
|
|
|
<div className={styles.feeButton}>
|
2023-08-08 21:04:06 +00:00
|
|
|
<ActionTooltip overlayText={numWithUnits(cost, { abbreviate: false })}>
|
2023-08-10 19:41:56 +00:00
|
|
|
<ChildButton variant={variant} disabled={disabled}>{text}{cost > 1 && show && <small> {numWithUnits(cost, { abbreviate: false })}</small>}</ChildButton>
|
2022-08-10 15:06:31 +00:00
|
|
|
</ActionTooltip>
|
2023-08-10 19:41:56 +00:00
|
|
|
{!me && <AnonInfo />}
|
2022-08-10 15:06:31 +00:00
|
|
|
{cost > baseFee && show &&
|
|
|
|
<Info>
|
|
|
|
<Receipt baseFee={baseFee} hasImgLink={hasImgLink} repetition={repetition} cost={cost} parentId={parentId} boost={boost} />
|
|
|
|
</Info>}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2022-08-18 18:15:24 +00:00
|
|
|
|
|
|
|
function EditReceipt ({ cost, paidSats, addImgLink, boost, parentId }) {
|
|
|
|
return (
|
|
|
|
<Table className={styles.receipt} borderless size='sm'>
|
|
|
|
<tbody>
|
|
|
|
{addImgLink &&
|
|
|
|
<>
|
|
|
|
<tr>
|
2023-08-08 21:04:06 +00:00
|
|
|
<td>{numWithUnits(paidSats, { abbreviate: false })}</td>
|
2022-08-18 18:15:24 +00:00
|
|
|
<td align='right' className='font-weight-light'>{parentId ? 'reply' : 'post'} fee</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>x 10</td>
|
|
|
|
<td align='right' className='font-weight-light'>image/link fee</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
2023-08-08 21:04:06 +00:00
|
|
|
<td>- {numWithUnits(paidSats, { abbreviate: false })}</td>
|
2022-08-18 18:15:24 +00:00
|
|
|
<td align='right' className='font-weight-light'>already paid</td>
|
|
|
|
</tr>
|
|
|
|
</>}
|
|
|
|
{boost > 0 &&
|
|
|
|
<tr>
|
2023-08-08 21:04:06 +00:00
|
|
|
<td>+ {numWithUnits(boost, { abbreviate: false })}</td>
|
2022-08-18 18:15:24 +00:00
|
|
|
<td className='font-weight-light' align='right'>boost</td>
|
|
|
|
</tr>}
|
|
|
|
</tbody>
|
|
|
|
<tfoot>
|
|
|
|
<tr>
|
2023-08-08 21:04:06 +00:00
|
|
|
<td className='fw-bold'>{numWithUnits(cost)}</td>
|
2022-08-18 18:15:24 +00:00
|
|
|
<td align='right' className='font-weight-light'>total fee</td>
|
|
|
|
</tr>
|
|
|
|
</tfoot>
|
|
|
|
</Table>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function EditFeeButton ({ paidSats, hadImgLink, hasImgLink, ChildButton, variant, text, alwaysShow, parentId }) {
|
|
|
|
const formik = useFormikContext()
|
|
|
|
const boost = formik?.values?.boost || 0
|
|
|
|
const addImgLink = hasImgLink && !hadImgLink
|
|
|
|
const cost = (addImgLink ? paidSats * 9 : 0) + Number(boost)
|
|
|
|
|
2023-08-11 03:11:41 +00:00
|
|
|
useEffect(() => {
|
2023-08-12 00:40:37 +00:00
|
|
|
formik?.setFieldValue('cost', cost)
|
|
|
|
}, [formik?.getFieldProps('cost').value, cost])
|
2023-08-11 03:11:41 +00:00
|
|
|
|
2022-08-18 18:15:24 +00:00
|
|
|
const show = alwaysShow || !formik?.isSubmitting
|
|
|
|
return (
|
|
|
|
<div className='d-flex align-items-center'>
|
2023-08-08 21:04:06 +00:00
|
|
|
<ActionTooltip overlayText={numWithUnits(cost, { abbreviate: false })}>
|
|
|
|
<ChildButton variant={variant}>{text}{cost > 0 && show && <small> {numWithUnits(cost, { abbreviate: false })}</small>}</ChildButton>
|
2022-08-18 18:15:24 +00:00
|
|
|
</ActionTooltip>
|
|
|
|
{cost > 0 && show &&
|
|
|
|
<Info>
|
|
|
|
<EditReceipt paidSats={paidSats} addImgLink={addImgLink} cost={cost} parentId={parentId} boost={boost} />
|
|
|
|
</Info>}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|