Keyan 146b60278c
cowboy credits (aka nov-5 (aka jan-3)) (#1678)
* wip adding cowboy credits

* invite gift paid action

* remove balance limit

* remove p2p zap withdrawal notifications

* credits typedefs

* squash migrations

* remove wallet limit stuff

* CCs in item detail

* comments with meCredits

* begin including CCs in item stats/notifications

* buy credits ui/mutation

* fix old /settings/wallets paths

* bios don't get sats

* fix settings

* make invites work with credits

* restore migration from master

* inform backend of send wallets on zap

* satistics header

* default receive options to true and squash migrations

* fix paidAction query

* add nav for credits

* fix forever stacked count

* ek suggested fixes

* fix lint

* fix freebies wrt CCs

* add back disable freebies

* trigger cowboy hat job on CC depletion

* fix meMsats+meMcredits

* Update api/paidAction/README.md

Co-authored-by: ekzyis <ek@stacker.news>

* remove expireBoost migration that doesn't work

---------

Co-authored-by: ekzyis <ek@stacker.news>
2025-01-03 10:33:07 -06:00

92 lines
2.4 KiB
JavaScript

import Login from '@/components/login'
import { getProviders } from 'next-auth/react'
import { getServerSession } from 'next-auth/next'
import models from '@/api/models'
import { gql } from '@apollo/client'
import { INVITE_FIELDS } from '@/fragments/invites'
import getSSRApolloClient from '@/api/ssrApollo'
import Link from 'next/link'
import { CenterLayout } from '@/components/layout'
import { getAuthOptions } from '@/pages/api/auth/[...nextauth]'
import performPaidAction from '@/api/paidAction'
export async function getServerSideProps ({ req, res, query: { id, error = null } }) {
const session = await getServerSession(req, res, getAuthOptions(req))
const client = await getSSRApolloClient({ req, res })
const { data } = await client.query({
query: gql`
${INVITE_FIELDS}
{
invite(id: "${id}") {
...InviteFields
}
}`
})
if (!data?.invite) {
res.writeHead(302, {
Location: '/404'
}).end()
return { props: {} }
}
if (session && res) {
try {
// attempt to send gift
// catch any errors and just ignore them for now
await performPaidAction('INVITE_GIFT', {
id,
userId: session.user.id
}, { models, me: { id: data.invite.user.id } })
} catch (e) {
console.log(e)
}
res.writeHead(302, {
Location: '/'
}).end()
return { props: {} }
}
return {
props: {
providers: await getProviders(),
callbackUrl: process.env.NEXT_PUBLIC_URL + req.url,
invite: data.invite,
error
}
}
}
function InviteHeader ({ invite }) {
let Inner
if (invite.revoked) {
Inner = () => <div className='text-danger'>this invite link expired</div>
} else if ((invite.limit && invite.limit <= invite.invitees.length) || invite.poor) {
Inner = () => <div className='text-danger'>this invite link has no more cowboy credits</div>
} else {
Inner = () => (
<div>
Get <span className='text-success'>{invite.gift} cowboy credits</span> from{' '}
<Link href={`/${invite.user.name}`}>@{invite.user.name}</Link>{' '}
when you sign up
</div>
)
}
return (
<h3 className='text-center pb-3'>
<Inner />
</h3>
)
}
export default function Invite ({ invite, ...props }) {
return (
<CenterLayout>
<Login Header={() => <InviteHeader invite={invite} />} text='Sign up' {...props} />
</CenterLayout>
)
}