2023-07-23 15:08:43 +00:00
|
|
|
import { gql } from 'graphql-tag'
|
|
|
|
import { useEffect, useState } from 'react'
|
2023-07-24 18:35:05 +00:00
|
|
|
import Button from 'react-bootstrap/Button'
|
|
|
|
import InputGroup from 'react-bootstrap/InputGroup'
|
2022-12-08 00:04:02 +00:00
|
|
|
import { Cell, Pie, PieChart, ResponsiveContainer, Tooltip } from 'recharts'
|
|
|
|
import { getGetServerSideProps } from '../api/ssrApollo'
|
|
|
|
import { Form, Input, SubmitButton } from '../components/form'
|
2023-07-23 15:08:43 +00:00
|
|
|
import { CenterLayout } from '../components/layout'
|
2022-12-08 00:04:02 +00:00
|
|
|
import { useMutation, useQuery } from '@apollo/client'
|
|
|
|
import Link from 'next/link'
|
2023-02-08 19:38:04 +00:00
|
|
|
import { amountSchema } from '../lib/validate'
|
2023-07-06 17:43:51 +00:00
|
|
|
import Countdown from 'react-countdown'
|
|
|
|
import { abbrNum } from '../lib/format'
|
2023-07-23 15:08:43 +00:00
|
|
|
import PageLoading from '../components/page-loading'
|
|
|
|
import { useShowModal } from '../components/modal'
|
2022-12-08 00:04:02 +00:00
|
|
|
|
|
|
|
const REWARDS = gql`
|
|
|
|
{
|
|
|
|
expectedRewards {
|
|
|
|
total
|
|
|
|
sources {
|
|
|
|
name
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2023-07-07 19:43:53 +00:00
|
|
|
function midnight (tz) {
|
|
|
|
function tzOffset (tz) {
|
|
|
|
const date = new Date()
|
|
|
|
date.setMilliseconds(0)
|
|
|
|
const targetDate = new Date(date.toLocaleString('en-US', { timeZone: tz }))
|
|
|
|
const targetOffsetHours = (date.getTime() - targetDate.getTime()) / 1000 / 60 / 60
|
|
|
|
return targetOffsetHours
|
|
|
|
}
|
|
|
|
|
|
|
|
const date = new Date()
|
|
|
|
date.setHours(24, 0, 0, 0)
|
|
|
|
return date.getTime() + tzOffset(tz) * 60 * 60 * 1000
|
|
|
|
}
|
|
|
|
|
2022-12-08 00:04:02 +00:00
|
|
|
export const getServerSideProps = getGetServerSideProps(REWARDS)
|
|
|
|
|
2023-07-06 17:43:51 +00:00
|
|
|
export function RewardLine ({ total }) {
|
|
|
|
const [threshold, setThreshold] = useState(0)
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-07-07 19:43:53 +00:00
|
|
|
setThreshold(midnight('America/Chicago'))
|
2023-07-06 17:43:51 +00:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{abbrNum(total)} sats in rewards
|
|
|
|
{threshold &&
|
|
|
|
<Countdown
|
|
|
|
date={threshold}
|
|
|
|
renderer={props => <small className='text-monospace'> {props.formatted.hours}:{props.formatted.minutes}:{props.formatted.seconds}</small>}
|
|
|
|
/>}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-23 15:08:43 +00:00
|
|
|
export default function Rewards ({ ssrData }) {
|
|
|
|
const { data } = useQuery(REWARDS, { pollInterval: 1000, nextFetchPolicy: 'cache-and-network' })
|
|
|
|
if (!data && !ssrData) return <PageLoading />
|
2022-12-08 00:04:02 +00:00
|
|
|
|
2023-07-23 15:08:43 +00:00
|
|
|
const { expectedRewards: { total, sources } } = data || ssrData
|
2022-12-08 00:04:02 +00:00
|
|
|
|
|
|
|
return (
|
2023-07-23 15:08:43 +00:00
|
|
|
<CenterLayout footerLinks>
|
2023-07-24 18:35:05 +00:00
|
|
|
<h4 className='fw-bold text-muted text-center'>
|
2023-07-06 17:43:51 +00:00
|
|
|
<div>
|
|
|
|
<RewardLine total={total} />
|
|
|
|
</div>
|
2023-07-23 15:08:43 +00:00
|
|
|
<Link href='/faq#how-do-i-earn-sats-on-stacker-news' className='text-reset'>
|
|
|
|
<small><small><small>learn about rewards</small></small></small>
|
2022-12-08 00:04:02 +00:00
|
|
|
</Link>
|
|
|
|
</h4>
|
|
|
|
<div className='my-3 w-100'>
|
|
|
|
<GrowthPieChart data={sources} />
|
|
|
|
</div>
|
|
|
|
<DonateButton />
|
2023-07-23 15:08:43 +00:00
|
|
|
</CenterLayout>
|
2022-12-08 00:04:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const COLORS = [
|
2023-07-24 18:35:05 +00:00
|
|
|
'var(--bs-secondary)',
|
|
|
|
'var(--bs-info)',
|
|
|
|
'var(--bs-success)',
|
|
|
|
'var(--bs-boost)',
|
|
|
|
'var(--bs-grey)'
|
2022-12-08 00:04:02 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
function GrowthPieChart ({ data }) {
|
|
|
|
return (
|
|
|
|
<ResponsiveContainer width='100%' height={250} minWidth={200}>
|
|
|
|
<PieChart margin={{ top: 5, right: 5, bottom: 5, left: 5 }}>
|
|
|
|
<Pie
|
|
|
|
dataKey='value'
|
|
|
|
isAnimationActive={false}
|
|
|
|
data={data}
|
|
|
|
cx='50%'
|
|
|
|
cy='50%'
|
|
|
|
outerRadius={80}
|
2023-07-24 18:35:05 +00:00
|
|
|
fill='var(--bs-secondary)'
|
2022-12-08 00:04:02 +00:00
|
|
|
label
|
|
|
|
>
|
|
|
|
{
|
|
|
|
data.map((entry, index) => (
|
|
|
|
<Cell key={`cell-${index}`} fill={COLORS[index]} />
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</Pie>
|
|
|
|
<Tooltip />
|
|
|
|
</PieChart>
|
|
|
|
</ResponsiveContainer>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function DonateButton () {
|
2023-07-23 15:08:43 +00:00
|
|
|
const showModal = useShowModal()
|
2022-12-08 00:04:02 +00:00
|
|
|
const [donateToRewards] = useMutation(
|
|
|
|
gql`
|
|
|
|
mutation donateToRewards($sats: Int!) {
|
|
|
|
donateToRewards(sats: $sats)
|
|
|
|
}`)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-07-23 15:08:43 +00:00
|
|
|
<Button onClick={() => showModal(onClose => (
|
|
|
|
<Form
|
|
|
|
initial={{
|
|
|
|
amount: 1000
|
|
|
|
}}
|
|
|
|
schema={amountSchema}
|
|
|
|
onSubmit={async ({ amount }) => {
|
|
|
|
await donateToRewards({
|
|
|
|
variables: {
|
|
|
|
sats: Number(amount)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
onClose()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Input
|
|
|
|
label='amount'
|
|
|
|
name='amount'
|
|
|
|
required
|
|
|
|
autoFocus
|
|
|
|
append={<InputGroup.Text className='text-monospace'>sats</InputGroup.Text>}
|
|
|
|
/>
|
|
|
|
<div className='d-flex'>
|
2023-07-24 18:35:05 +00:00
|
|
|
<SubmitButton variant='success' className='ms-auto mt-1 px-4' value='TIP'>donate</SubmitButton>
|
2023-07-23 15:08:43 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
))}
|
|
|
|
>DONATE TO REWARDS
|
|
|
|
</Button>
|
2022-12-08 00:04:02 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|