2021-09-06 22:36:08 +00:00
|
|
|
import { useQuery } from '@apollo/client'
|
2021-08-17 18:15:24 +00:00
|
|
|
import Comment, { CommentSkeleton } from './comment'
|
2021-08-17 23:07:52 +00:00
|
|
|
import Item from './item'
|
2021-08-17 18:15:24 +00:00
|
|
|
import { NOTIFICATIONS } from '../fragments/notifications'
|
2021-08-17 23:59:22 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2021-09-30 15:46:58 +00:00
|
|
|
import MoreFooter from './more-footer'
|
2022-01-19 21:02:38 +00:00
|
|
|
import Invite from './invite'
|
2022-01-20 19:03:48 +00:00
|
|
|
import { ignoreClick } from '../lib/clicks'
|
2021-08-17 18:15:24 +00:00
|
|
|
|
2021-09-06 22:36:08 +00:00
|
|
|
function Notification ({ n }) {
|
2021-08-17 23:59:22 +00:00
|
|
|
const router = useRouter()
|
2021-08-20 00:13:32 +00:00
|
|
|
return (
|
|
|
|
<div
|
2021-11-04 18:22:03 +00:00
|
|
|
className='clickToContext'
|
2022-01-20 19:03:48 +00:00
|
|
|
onClick={e => {
|
|
|
|
if (ignoreClick(e)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-19 21:02:38 +00:00
|
|
|
if (n.__typename === 'Invitification') {
|
|
|
|
router.push('/invites')
|
|
|
|
} else if (!n.item.title) {
|
2021-08-20 00:13:32 +00:00
|
|
|
router.push({
|
|
|
|
pathname: '/items/[id]',
|
2021-11-27 18:01:02 +00:00
|
|
|
query: { id: n.item.root.id, commentId: n.item.id }
|
|
|
|
}, `/items/${n.item.root.id}`)
|
2021-08-20 00:13:32 +00:00
|
|
|
} else {
|
2021-09-06 22:36:08 +00:00
|
|
|
router.push({
|
|
|
|
pathname: '/items/[id]',
|
|
|
|
query: { id: n.item.id }
|
|
|
|
}, `/items/${n.item.id}`)
|
2021-08-20 00:13:32 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2022-01-19 21:02:38 +00:00
|
|
|
{n.__typename === 'Invitification'
|
|
|
|
? (
|
|
|
|
<>
|
|
|
|
<small className='font-weight-bold text-secondary ml-2'>
|
|
|
|
your invite has been redeemed by {n.invite.invitees.length} users
|
|
|
|
</small>
|
|
|
|
<div className='ml-4 mr-2 mt-1'>
|
|
|
|
<Invite
|
|
|
|
invite={n.invite} active={
|
|
|
|
!n.invite.revoked &&
|
|
|
|
!(n.invite.limit && n.invite.invitees.length >= n.invite.limit)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
: (
|
|
|
|
<>
|
|
|
|
{n.__typename === 'Votification' &&
|
|
|
|
<small className='font-weight-bold text-success ml-2'>
|
|
|
|
your {n.item.title ? 'post' : 'reply'} stacked {n.earnedSats} sats
|
|
|
|
</small>}
|
|
|
|
{n.__typename === 'Mention' &&
|
|
|
|
<small className='font-weight-bold text-info ml-2'>
|
|
|
|
you were mentioned in
|
|
|
|
</small>}
|
|
|
|
<div className={n.__typename === 'Votification' || n.__typename === 'Mention' ? '' : 'py-2'}>
|
|
|
|
{n.item.title
|
|
|
|
? <Item item={n.item} />
|
|
|
|
: (
|
|
|
|
<div className='pb-2'>
|
2022-01-30 15:35:57 +00:00
|
|
|
<Comment item={n.item} noReply includeParent rootText={n.__typename === 'Reply' ? 'replying on:' : undefined} clickToContext />
|
2022-01-19 21:02:38 +00:00
|
|
|
</div>)}
|
|
|
|
</div>
|
|
|
|
</>)}
|
2021-08-20 00:13:32 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-30 15:46:58 +00:00
|
|
|
export default function Notifications ({ notifications, cursor, lastChecked, variables }) {
|
2021-10-26 20:49:37 +00:00
|
|
|
const { data, fetchMore } = useQuery(NOTIFICATIONS, { variables })
|
2021-08-20 00:13:32 +00:00
|
|
|
|
2021-09-30 15:46:58 +00:00
|
|
|
if (data) {
|
|
|
|
({ notifications: { notifications, cursor } } = data)
|
|
|
|
}
|
2021-08-20 00:13:32 +00:00
|
|
|
|
|
|
|
const [fresh, old] =
|
|
|
|
notifications.reduce((result, n) => {
|
|
|
|
result[new Date(n.sortTime).getTime() > lastChecked ? 0 : 1].push(n)
|
|
|
|
return result
|
|
|
|
},
|
|
|
|
[[], []])
|
2021-08-17 23:59:22 +00:00
|
|
|
|
2021-08-17 18:15:24 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-08-17 23:07:52 +00:00
|
|
|
{/* XXX we shouldn't use the index but we don't have a unique id in this union yet */}
|
2021-11-04 18:22:03 +00:00
|
|
|
<div className='fresh'>
|
2021-08-20 00:13:32 +00:00
|
|
|
{fresh.map((n, i) => (
|
|
|
|
<Notification n={n} key={i} />
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
{old.map((n, i) => (
|
|
|
|
<Notification n={n} key={i} />
|
2021-08-17 18:15:24 +00:00
|
|
|
))}
|
2021-09-30 15:46:58 +00:00
|
|
|
<MoreFooter cursor={cursor} fetchMore={fetchMore} Skeleton={CommentsFlatSkeleton} />
|
2021-08-17 18:15:24 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function CommentsFlatSkeleton () {
|
|
|
|
const comments = new Array(21).fill(null)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>{comments.map((_, i) => (
|
|
|
|
<CommentSkeleton key={i} skeletonChildren={0} />
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|