highlight new notifications

This commit is contained in:
keyan 2021-08-19 19:13:32 -05:00
parent 79cb2d5c27
commit 09b358397a
6 changed files with 77 additions and 47 deletions

View File

@ -43,14 +43,14 @@ export default {
*/ */
let notifications = await models.$queryRaw(` let notifications = await models.$queryRaw(`
SELECT ${ITEM_FIELDS}, "Item".created_at as sort_time, NULL as "earnedSats", SELECT ${ITEM_FIELDS}, "Item".created_at as "sortTime", NULL as "earnedSats",
false as mention false as mention
From "Item" From "Item"
JOIN "Item" p ON "Item"."parentId" = p.id JOIN "Item" p ON "Item"."parentId" = p.id
WHERE p."userId" = $1 WHERE p."userId" = $1
AND "Item"."userId" <> $1 AND "Item".created_at <= $2 AND "Item"."userId" <> $1 AND "Item".created_at <= $2
UNION ALL UNION ALL
(SELECT ${ITEM_SUBQUERY_FIELDS}, max(subquery.voted_at) as sort_time, (SELECT ${ITEM_SUBQUERY_FIELDS}, max(subquery.voted_at) as "sortTime",
sum(subquery.sats) as "earnedSats", false as mention sum(subquery.sats) as "earnedSats", false as mention
FROM FROM
(SELECT ${ITEM_FIELDS}, "Vote".created_at as voted_at, "Vote".sats, (SELECT ${ITEM_FIELDS}, "Vote".created_at as voted_at, "Vote".sats,
@ -64,7 +64,7 @@ export default {
AND "Item"."userId" = $1) subquery AND "Item"."userId" = $1) subquery
GROUP BY ${ITEM_SUBQUERY_FIELDS}, subquery.island ORDER BY max(subquery.voted_at) desc) GROUP BY ${ITEM_SUBQUERY_FIELDS}, subquery.island ORDER BY max(subquery.voted_at) desc)
UNION ALL UNION ALL
(SELECT ${ITEM_FIELDS}, "Mention".created_at as sort_time, NULL as "earnedSats", (SELECT ${ITEM_FIELDS}, "Mention".created_at as "sortTime", NULL as "earnedSats",
true as mention true as mention
FROM "Mention" FROM "Mention"
JOIN "Item" on "Mention"."itemId" = "Item".id JOIN "Item" on "Mention"."itemId" = "Item".id
@ -73,7 +73,7 @@ export default {
AND "Mention".created_at <= $2 AND "Mention".created_at <= $2
AND "Item"."userId" <> $1 AND "Item"."userId" <> $1
AND p."userId" <> $1) AND p."userId" <> $1)
ORDER BY sort_time DESC ORDER BY "sortTime" DESC
OFFSET $3 OFFSET $3
LIMIT ${LIMIT}`, me.id, decodedCursor.time, decodedCursor.offset) LIMIT ${LIMIT}`, me.id, decodedCursor.time, decodedCursor.offset)
@ -82,9 +82,11 @@ export default {
return n return n
}) })
const { checkedNotesAt } = await models.user.findUnique({ where: { id: me.id } })
await models.user.update({ where: { id: me.id }, data: { checkedNotesAt: new Date() } }) await models.user.update({ where: { id: me.id }, data: { checkedNotesAt: new Date() } })
return { return {
lastChecked: checkedNotesAt,
cursor: notifications.length === LIMIT ? nextCursorEncoded(decodedCursor) : null, cursor: notifications.length === LIMIT ? nextCursorEncoded(decodedCursor) : null,
notifications notifications
} }

View File

@ -8,20 +8,24 @@ export default gql`
type Votification { type Votification {
earnedSats: Int! earnedSats: Int!
item: Item! item: Item!
sortTime: String!
} }
type Reply { type Reply {
item: Item! item: Item!
sortTime: String!
} }
type Mention { type Mention {
mention: Boolean! mention: Boolean!
item: Item! item: Item!
sortTime: String!
} }
union Notification = Reply | Votification | Mention union Notification = Reply | Votification | Mention
type Notifications { type Notifications {
lastChecked: String
cursor: String cursor: String
notifications: [Notification!]! notifications: [Notification!]!
} }

View File

@ -7,9 +7,47 @@ import { NOTIFICATIONS } from '../fragments/notifications'
import styles from './notifications.module.css' import styles from './notifications.module.css'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
export default function Notifications ({ variables, ...props }) { function Notification ({ key, n }) {
const router = useRouter() const router = useRouter()
const client = useApolloClient() const client = useApolloClient()
return (
<div
key={key}
className={styles.clickToContext}
onClick={() => {
if (n.__typename === 'Reply' || !n.item.title) {
// evict item from cache so that it has current state
// e.g. if they previously visited before a recent comment
client.cache.evict({ id: `Item:${n.item.parentId}` })
router.push({
pathname: '/items/[id]',
query: { id: n.item.parentId, commentId: n.item.id }
}, `/items/${n.item.parentId}`)
} else {
client.cache.evict({ id: `Item:${n.item.id}` })
router.push(`items/${n.item.id}`)
}
}}
>
{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'
? `ml-sm-4 ml-3 ${n.item.title ? 'pb-2' : ''}`
: ''
}
>
{n.item.title
? <Item item={n.item} />
: <Comment item={n.item} noReply includeParent rootText={n.__typename === 'Reply' ? 'replying to you on:' : undefined} clickToContext />}
</div>
</div>
)
}
export default function Notifications ({ variables }) {
const { loading, error, data, fetchMore } = useQuery(NOTIFICATIONS, { const { loading, error, data, fetchMore } = useQuery(NOTIFICATIONS, {
variables variables
}) })
@ -17,45 +55,26 @@ export default function Notifications ({ variables, ...props }) {
if (loading) { if (loading) {
return <CommentsFlatSkeleton /> return <CommentsFlatSkeleton />
} }
const { notifications: { notifications, cursor } } = data
const { notifications: { notifications, cursor, lastChecked } } = data
const [fresh, old] =
notifications.reduce((result, n) => {
result[new Date(n.sortTime).getTime() > lastChecked ? 0 : 1].push(n)
return result
},
[[], []])
return ( return (
<> <>
{/* XXX we shouldn't use the index but we don't have a unique id in this union yet */} {/* XXX we shouldn't use the index but we don't have a unique id in this union yet */}
{notifications.map((n, i) => ( <div className={styles.fresh}>
<div {fresh.map((n, i) => (
key={i} <Notification n={n} key={i} />
className={styles.clickToContext} ))}
onClick={() => { </div>
if (n.__typename === 'Reply' || !n.item.title) { {old.map((n, i) => (
// evict item from cache so that it has current state <Notification n={n} key={i} />
// e.g. if they previously visited before a recent comment
client.cache.evict({ id: `Item:${n.item.parentId}` })
router.push({
pathname: '/items/[id]',
query: { id: n.item.parentId, commentId: n.item.id }
}, `/items/${n.item.parentId}`)
} else {
client.cache.evict({ id: `Item:${n.item.id}` })
router.push(`items/${n.item.id}`)
}
}}
>
{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'
? `ml-sm-4 ml-3 ${n.item.title ? 'pb-2' : ''}`
: ''
}
>
{n.item.title
? <Item item={n.item} />
: <Comment item={n.item} noReply includeParent rootText={n.__typename === 'Reply' ? 'replying to you on:' : undefined} clickToContext {...props} />}
</div>
</div>
))} ))}
<MoreFooter cursor={cursor} fetchMore={fetchMore} /> <MoreFooter cursor={cursor} fetchMore={fetchMore} />
</> </>

View File

@ -6,4 +6,9 @@
.clickToContext:hover { .clickToContext:hover {
background-color: rgba(0, 0, 0, 0.03); background-color: rgba(0, 0, 0, 0.03);
}
.fresh {
background-color: rgba(0, 0, 0, 0.03);
border-radius: .4rem;
} }

View File

@ -7,9 +7,11 @@ export const NOTIFICATIONS = gql`
query Notifications($cursor: String) { query Notifications($cursor: String) {
notifications(cursor: $cursor) { notifications(cursor: $cursor) {
cursor cursor
lastChecked
notifications { notifications {
__typename __typename
... on Mention { ... on Mention {
sortTime
mention mention
item { item {
...ItemFields ...ItemFields
@ -17,6 +19,7 @@ export const NOTIFICATIONS = gql`
} }
} }
... on Votification { ... on Votification {
sortTime
earnedSats earnedSats
item { item {
...ItemFields ...ItemFields
@ -24,6 +27,7 @@ export const NOTIFICATIONS = gql`
} }
} }
... on Reply { ... on Reply {
sortTime
item { item {
...ItemFields ...ItemFields
text text

View File

@ -55,17 +55,13 @@ const client = new ApolloClient({
const notifications = existing ? existing.notifications : [] const notifications = existing ? existing.notifications : []
return { return {
cursor: incoming.cursor, cursor: incoming.cursor,
notifications: [...notifications, ...incoming.notifications] notifications: [...notifications, ...incoming.notifications],
lastChecked: incoming.lastChecked
} }
}, },
read (existing) { read (existing) {
if (existing) { return existing
return {
cursor: existing.cursor,
notifications: existing.notifications
}
}
} }
} }
} }