2024-03-20 00:37:31 +00:00
|
|
|
import Layout from '@/components/layout'
|
|
|
|
import { Form, Input, SubmitButton } from '@/components/form'
|
2023-07-24 18:35:05 +00:00
|
|
|
import InputGroup from 'react-bootstrap/InputGroup'
|
2021-10-12 23:49:04 +00:00
|
|
|
import { gql, useMutation, useQuery } from '@apollo/client'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { INVITE_FIELDS } from '@/fragments/invites'
|
|
|
|
import AccordianItem from '@/components/accordian-item'
|
|
|
|
import styles from '@/styles/invites.module.css'
|
|
|
|
import Invite from '@/components/invite'
|
|
|
|
import { inviteSchema } from '@/lib/validate'
|
|
|
|
import { SSR } from '@/lib/constants'
|
|
|
|
import { getGetServerSideProps } from '@/api/ssrApollo'
|
2024-12-01 22:31:47 +00:00
|
|
|
import Info from '@/components/info'
|
|
|
|
import Text from '@/components/text'
|
2024-02-13 22:40:26 +00:00
|
|
|
|
|
|
|
// force SSR to include CSP nonces
|
|
|
|
export const getServerSideProps = getGetServerSideProps({ query: null })
|
2021-10-12 23:49:04 +00:00
|
|
|
|
|
|
|
function InviteForm () {
|
|
|
|
const [createInvite] = useMutation(
|
|
|
|
gql`
|
|
|
|
${INVITE_FIELDS}
|
2024-12-01 22:31:47 +00:00
|
|
|
mutation createInvite($id: String, $gift: Int!, $limit: Int, $description: String) {
|
|
|
|
createInvite(id: $id, gift: $gift, limit: $limit, description: $description) {
|
2021-10-12 23:49:04 +00:00
|
|
|
...InviteFields
|
|
|
|
}
|
|
|
|
}`, {
|
|
|
|
update (cache, { data: { createInvite } }) {
|
|
|
|
cache.modify({
|
|
|
|
fields: {
|
|
|
|
invites (existingInviteRefs = []) {
|
|
|
|
const newInviteRef = cache.writeFragment({
|
2024-10-25 12:10:41 +00:00
|
|
|
fragmentName: 'InviteFields',
|
2021-10-12 23:49:04 +00:00
|
|
|
data: createInvite,
|
|
|
|
fragment: INVITE_FIELDS
|
|
|
|
})
|
|
|
|
return [newInviteRef, ...existingInviteRefs]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-12-01 22:31:47 +00:00
|
|
|
const initialValues = {
|
|
|
|
id: '',
|
|
|
|
gift: 1000,
|
|
|
|
limit: 1,
|
|
|
|
description: ''
|
|
|
|
}
|
|
|
|
|
2021-10-12 23:49:04 +00:00
|
|
|
return (
|
|
|
|
<Form
|
2024-12-01 22:31:47 +00:00
|
|
|
initial={initialValues}
|
2023-02-08 19:38:04 +00:00
|
|
|
schema={inviteSchema}
|
2024-12-01 22:31:47 +00:00
|
|
|
onSubmit={async ({ id, gift, limit, description }, { resetForm }) => {
|
2021-10-12 23:49:04 +00:00
|
|
|
const { error } = await createInvite({
|
|
|
|
variables: {
|
2024-12-01 22:31:47 +00:00
|
|
|
id: id || undefined,
|
|
|
|
gift: Number(gift),
|
|
|
|
limit: limit ? Number(limit) : limit,
|
|
|
|
description: description || undefined
|
2021-10-12 23:49:04 +00:00
|
|
|
}
|
|
|
|
})
|
2024-07-01 17:02:29 +00:00
|
|
|
if (error) throw error
|
2024-12-01 22:31:47 +00:00
|
|
|
resetForm({ values: initialValues })
|
2021-10-12 23:49:04 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Input
|
|
|
|
label='gift'
|
|
|
|
name='gift'
|
|
|
|
append={<InputGroup.Text className='text-monospace'>sats</InputGroup.Text>}
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
<Input
|
2023-07-24 18:35:05 +00:00
|
|
|
label={<>invitee limit <small className='text-muted ms-2'>optional</small></>}
|
2021-10-12 23:49:04 +00:00
|
|
|
name='limit'
|
|
|
|
/>
|
2024-12-01 22:31:47 +00:00
|
|
|
<AccordianItem
|
|
|
|
headerColor='#6c757d' header='advanced' body={
|
|
|
|
<>
|
|
|
|
<Input
|
|
|
|
prepend={<InputGroup.Text className='text-muted'>{`${process.env.NEXT_PUBLIC_URL}/invites/`}</InputGroup.Text>}
|
|
|
|
label={<>invite code <small className='text-muted ms-2'>optional</small></>}
|
2024-12-04 18:10:30 +00:00
|
|
|
hint='leave blank for a random code that is hard to guess'
|
2024-12-01 22:31:47 +00:00
|
|
|
name='id'
|
|
|
|
autoComplete='off'
|
|
|
|
/>
|
|
|
|
<Input
|
|
|
|
label={
|
|
|
|
<>
|
|
|
|
<div className='d-flex align-items-center'>
|
|
|
|
description <small className='text-muted ms-2'>optional</small>
|
|
|
|
<Info>
|
|
|
|
<Text>
|
|
|
|
A brief description to keep track of the invite purpose, such as "Shared in group chat".
|
|
|
|
This description is private and visible only to you.
|
|
|
|
</Text>
|
|
|
|
</Info>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
name='description'
|
|
|
|
autoComplete='off'
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<SubmitButton
|
|
|
|
className='mt-4'
|
|
|
|
variant='secondary'
|
|
|
|
>create
|
|
|
|
</SubmitButton>
|
2021-10-12 23:49:04 +00:00
|
|
|
</Form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-14 21:05:37 +00:00
|
|
|
function InviteList ({ name, invites }) {
|
|
|
|
return (
|
|
|
|
<div className='mt-4'>
|
|
|
|
<AccordianItem
|
|
|
|
show
|
2021-11-04 18:22:03 +00:00
|
|
|
headerColor='#6c757d'
|
2021-10-14 21:05:37 +00:00
|
|
|
header={<div style={{ fontWeight: 'bold', fontSize: '92%' }}>{name}</div>} body={
|
|
|
|
<div className={styles.invites}>{invites.map(invite => {
|
|
|
|
return <Invite invite={invite} key={invite.id} active={name === 'active'} />
|
|
|
|
})}
|
|
|
|
</div>
|
2021-10-15 17:56:54 +00:00
|
|
|
}
|
2021-10-14 21:05:37 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-12 23:49:04 +00:00
|
|
|
export default function Invites () {
|
|
|
|
const { data } = useQuery(
|
|
|
|
gql`
|
|
|
|
${INVITE_FIELDS}
|
|
|
|
{
|
|
|
|
invites {
|
|
|
|
...InviteFields
|
|
|
|
}
|
|
|
|
}
|
2023-07-31 19:54:30 +00:00
|
|
|
`, SSR ? {} : { fetchPolicy: 'cache-and-network' })
|
2021-10-14 21:05:37 +00:00
|
|
|
|
|
|
|
const [active, inactive] = data && data.invites
|
|
|
|
? data.invites.reduce((result, invite) => {
|
2023-07-25 14:14:45 +00:00
|
|
|
result[
|
|
|
|
invite.revoked || (invite.limit &&
|
2021-10-15 17:56:54 +00:00
|
|
|
invite.invitees.length >= invite.limit)
|
2023-07-25 14:14:45 +00:00
|
|
|
? 1
|
|
|
|
: 0].push(invite)
|
|
|
|
return result
|
|
|
|
},
|
|
|
|
[[], []])
|
2021-10-14 21:05:37 +00:00
|
|
|
: [[], []]
|
|
|
|
|
2021-10-12 23:49:04 +00:00
|
|
|
return (
|
|
|
|
<Layout>
|
2022-04-26 15:48:30 +00:00
|
|
|
<div className='text-center'>
|
|
|
|
<h2 className='mt-3 mb-0'>
|
|
|
|
invite links
|
|
|
|
</h2>
|
2023-07-24 18:35:05 +00:00
|
|
|
<small className='d-block text-muted fw-bold mx-5'>send these to people you trust, e.g. group chats or DMs</small>
|
2022-04-26 15:48:30 +00:00
|
|
|
</div>
|
2021-10-12 23:49:04 +00:00
|
|
|
<InviteForm />
|
2021-10-14 21:05:37 +00:00
|
|
|
{active.length > 0 && <InviteList name='active' invites={active} />}
|
|
|
|
{inactive.length > 0 && <InviteList name='inactive' invites={inactive} />}
|
2021-10-12 23:49:04 +00:00
|
|
|
</Layout>
|
|
|
|
)
|
|
|
|
}
|