2023-07-23 15:08:43 +00:00
|
|
|
import { gql } from 'graphql-tag'
|
2023-07-24 18:35:05 +00:00
|
|
|
import Button from 'react-bootstrap/Button'
|
|
|
|
import InputGroup from 'react-bootstrap/InputGroup'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { getGetServerSideProps } from '@/api/ssrApollo'
|
|
|
|
import { Form, Input, SubmitButton } from '@/components/form'
|
|
|
|
import Layout from '@/components/layout'
|
2024-07-01 17:02:29 +00:00
|
|
|
import { useQuery } from '@apollo/client'
|
2022-12-08 00:04:02 +00:00
|
|
|
import Link from 'next/link'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { amountSchema } from '@/lib/validate'
|
|
|
|
import { numWithUnits } from '@/lib/format'
|
|
|
|
import PageLoading from '@/components/page-loading'
|
|
|
|
import { useShowModal } from '@/components/modal'
|
2023-07-24 22:50:12 +00:00
|
|
|
import dynamic from 'next/dynamic'
|
2024-04-08 14:13:12 +00:00
|
|
|
import { FAST_POLL_INTERVAL, SSR } from '@/lib/constants'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { useToast } from '@/components/toast'
|
|
|
|
import { useLightning } from '@/components/lightning'
|
|
|
|
import { ListUsers } from '@/components/user-list'
|
2024-03-01 16:28:55 +00:00
|
|
|
import { Col, Row } from 'react-bootstrap'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { proportions } from '@/lib/madness'
|
|
|
|
import { useData } from '@/components/use-data'
|
2024-03-23 03:47:21 +00:00
|
|
|
import { GrowthPieChartSkeleton } from '@/components/charts-skeletons'
|
2024-04-05 00:31:38 +00:00
|
|
|
import { useMemo } from 'react'
|
2024-04-16 22:58:26 +00:00
|
|
|
import { CompactLongCountdown } from '@/components/countdown'
|
2024-07-01 17:02:29 +00:00
|
|
|
import { usePaidMutation } from '@/components/use-paid-mutation'
|
|
|
|
import { DONATE } from '@/fragments/paidAction'
|
2023-07-24 22:50:12 +00:00
|
|
|
|
2024-03-20 00:37:31 +00:00
|
|
|
const GrowthPieChart = dynamic(() => import('@/components/charts').then(mod => mod.GrowthPieChart), {
|
2024-03-23 03:47:21 +00:00
|
|
|
loading: () => <GrowthPieChartSkeleton />
|
2023-07-24 22:50:12 +00:00
|
|
|
})
|
2022-12-08 00:04:02 +00:00
|
|
|
|
2024-03-01 16:28:55 +00:00
|
|
|
const REWARDS_FULL = gql`
|
2022-12-08 00:04:02 +00:00
|
|
|
{
|
2023-08-15 17:41:51 +00:00
|
|
|
rewards {
|
2022-12-08 00:04:02 +00:00
|
|
|
total
|
2024-03-01 16:28:55 +00:00
|
|
|
time
|
2022-12-08 00:04:02 +00:00
|
|
|
sources {
|
|
|
|
name
|
|
|
|
value
|
|
|
|
}
|
2024-03-01 16:28:55 +00:00
|
|
|
leaderboard {
|
|
|
|
users {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
photoId
|
2024-03-02 17:48:11 +00:00
|
|
|
ncomments
|
|
|
|
nposts
|
2024-03-01 16:28:55 +00:00
|
|
|
|
|
|
|
optional {
|
|
|
|
streak
|
2024-03-02 17:48:11 +00:00
|
|
|
stacked
|
|
|
|
spent
|
|
|
|
referrals
|
2024-03-01 16:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 00:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2024-03-01 16:28:55 +00:00
|
|
|
const REWARDS = gql`
|
|
|
|
{
|
|
|
|
rewards {
|
|
|
|
total
|
|
|
|
time
|
|
|
|
sources {
|
|
|
|
name
|
|
|
|
value
|
|
|
|
}
|
2023-07-07 19:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-01 16:28:55 +00:00
|
|
|
`
|
2023-07-07 19:43:53 +00:00
|
|
|
|
2024-03-01 16:28:55 +00:00
|
|
|
export const getServerSideProps = getGetServerSideProps({ query: REWARDS_FULL })
|
2023-07-06 17:43:51 +00:00
|
|
|
|
2024-03-01 16:28:55 +00:00
|
|
|
export function RewardLine ({ total, time }) {
|
2023-07-06 17:43:51 +00:00
|
|
|
return (
|
|
|
|
<>
|
2024-03-01 16:28:55 +00:00
|
|
|
<span tyle={{ whiteSpace: 'nowrap' }}>
|
|
|
|
{numWithUnits(total)} in rewards
|
|
|
|
</span>
|
|
|
|
{time &&
|
2024-04-16 22:58:26 +00:00
|
|
|
<small style={{ whiteSpace: 'nowrap' }}>
|
|
|
|
<CompactLongCountdown
|
|
|
|
className='text-monospace'
|
|
|
|
date={time}
|
|
|
|
/>
|
|
|
|
</small>}
|
2023-07-06 17:43:51 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-23 15:08:43 +00:00
|
|
|
export default function Rewards ({ ssrData }) {
|
2024-03-01 16:28:55 +00:00
|
|
|
// only poll for updates to rewards and not leaderboard
|
2024-03-02 00:21:52 +00:00
|
|
|
const { data: rewardsData } = useQuery(
|
2024-03-01 16:28:55 +00:00
|
|
|
REWARDS,
|
2024-04-08 14:13:12 +00:00
|
|
|
SSR ? {} : { pollInterval: FAST_POLL_INTERVAL, nextFetchPolicy: 'cache-and-network' })
|
2024-03-02 00:21:52 +00:00
|
|
|
const { data } = useQuery(REWARDS_FULL)
|
2024-04-05 00:31:38 +00:00
|
|
|
const dat = useData(data, ssrData)
|
|
|
|
|
|
|
|
let { rewards: [{ total, sources, time, leaderboard }] } = useMemo(() => {
|
|
|
|
return dat || { rewards: [{}] }
|
|
|
|
}, [dat])
|
2022-12-08 00:04:02 +00:00
|
|
|
|
2024-03-02 00:21:52 +00:00
|
|
|
if (rewardsData?.rewards?.length > 0) {
|
2024-04-01 14:05:39 +00:00
|
|
|
total = rewardsData.rewards[0].total
|
2024-03-02 00:21:52 +00:00
|
|
|
sources = rewardsData.rewards[0].sources
|
|
|
|
time = rewardsData.rewards[0].time
|
2024-03-01 16:28:55 +00:00
|
|
|
}
|
2022-12-08 00:04:02 +00:00
|
|
|
|
2024-04-05 00:31:38 +00:00
|
|
|
if (!dat) return <PageLoading />
|
|
|
|
|
2024-03-01 16:28:55 +00:00
|
|
|
function EstimatedReward ({ rank }) {
|
2024-07-11 00:23:05 +00:00
|
|
|
const referrerReward = Math.floor(total * proportions[rank - 1] * 0.2)
|
|
|
|
const reward = Math.floor(total * proportions[rank - 1]) - referrerReward
|
|
|
|
|
2024-03-01 16:28:55 +00:00
|
|
|
return (
|
|
|
|
<div className='text-muted fst-italic'>
|
|
|
|
<small>
|
2024-07-11 00:23:05 +00:00
|
|
|
<span>estimated reward: {numWithUnits(reward)} <small className='fw-light'>(+ {numWithUnits(referrerReward)} to referrers)</small></span>
|
2024-03-01 16:28:55 +00:00
|
|
|
</small>
|
2022-12-08 00:04:02 +00:00
|
|
|
</div>
|
2024-03-01 16:28:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Layout footerLinks>
|
2024-07-11 00:59:05 +00:00
|
|
|
<h4 className='pt-3 align-self-center text-reset'>
|
|
|
|
<small className='text-muted'>rewards are sponsored by ...</small>
|
|
|
|
<Link className='text-reset ms-2' href='/items/141924' style={{ lineHeight: 1.5, textDecoration: 'underline' }}>
|
|
|
|
SN is hiring
|
|
|
|
</Link>
|
|
|
|
</h4>
|
2024-03-31 21:53:57 +00:00
|
|
|
<Row className='pb-3'>
|
2024-04-14 21:28:09 +00:00
|
|
|
<Col lg={leaderboard?.users && 5}>
|
2024-03-01 16:28:55 +00:00
|
|
|
<div
|
|
|
|
className='d-flex flex-column sticky-lg-top py-5'
|
|
|
|
>
|
2024-03-31 21:53:57 +00:00
|
|
|
<h3 className='text-center text-muted'>
|
2024-03-01 16:28:55 +00:00
|
|
|
<div>
|
|
|
|
<RewardLine total={total} time={time} />
|
|
|
|
</div>
|
|
|
|
<Link href='/faq#how-do-i-earn-sats-on-stacker-news' className='text-info fw-normal'>
|
|
|
|
<small><small><small>learn about rewards</small></small></small>
|
|
|
|
</Link>
|
|
|
|
</h3>
|
|
|
|
<div className='my-3 w-100'>
|
|
|
|
<GrowthPieChart data={sources} />
|
|
|
|
</div>
|
|
|
|
<DonateButton />
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
{leaderboard?.users &&
|
|
|
|
<Col lg={7}>
|
2024-03-31 21:53:57 +00:00
|
|
|
<h2 className='pt-5 text-center text-muted'>leaderboard</h2>
|
2024-03-01 16:28:55 +00:00
|
|
|
<div className='d-flex justify-content-center pt-4'>
|
|
|
|
<ListUsers users={leaderboard.users} rank Embellish={EstimatedReward} />
|
|
|
|
</div>
|
|
|
|
</Col>}
|
|
|
|
</Row>
|
|
|
|
</Layout>
|
2022-12-08 00:04:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function DonateButton () {
|
2023-07-23 15:08:43 +00:00
|
|
|
const showModal = useShowModal()
|
2023-09-24 01:26:12 +00:00
|
|
|
const toaster = useToast()
|
2023-12-26 00:05:45 +00:00
|
|
|
const strike = useLightning()
|
2024-07-01 17:02:29 +00:00
|
|
|
const [donateToRewards] = usePaidMutation(DONATE)
|
2022-12-08 00:04:02 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2024-03-01 16:28:55 +00:00
|
|
|
<Button
|
|
|
|
onClick={() => showModal(onClose => (
|
|
|
|
<Form
|
|
|
|
initial={{
|
|
|
|
amount: 10000
|
|
|
|
}}
|
|
|
|
schema={amountSchema}
|
2024-07-04 17:30:42 +00:00
|
|
|
onSubmit={async ({ amount }) => {
|
2024-03-01 16:28:55 +00:00
|
|
|
const { error } = await donateToRewards({
|
|
|
|
variables: {
|
2024-07-04 17:30:42 +00:00
|
|
|
sats: Number(amount)
|
2024-07-01 17:02:29 +00:00
|
|
|
},
|
|
|
|
onCompleted: () => {
|
|
|
|
strike()
|
2024-03-01 16:28:55 +00:00
|
|
|
toaster.success('donated')
|
|
|
|
}
|
2024-07-01 17:02:29 +00:00
|
|
|
})
|
2024-03-01 16:28:55 +00:00
|
|
|
onClose()
|
2024-07-01 17:02:29 +00:00
|
|
|
if (error) throw error
|
2024-03-01 16:28:55 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Input
|
|
|
|
label='amount'
|
|
|
|
name='amount'
|
|
|
|
type='number'
|
|
|
|
required
|
|
|
|
autoFocus
|
|
|
|
append={<InputGroup.Text className='text-monospace'>sats</InputGroup.Text>}
|
|
|
|
/>
|
|
|
|
<div className='d-flex'>
|
|
|
|
<SubmitButton variant='success' className='ms-auto mt-1 px-4' value='TIP'>donate</SubmitButton>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
))}
|
|
|
|
className='align-self-center'
|
2023-07-23 15:08:43 +00:00
|
|
|
>DONATE TO REWARDS
|
|
|
|
</Button>
|
2022-12-08 00:04:02 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|