Skip to content

Commit

Permalink
🥇 Notification dispatch block (#6427)
Browse files Browse the repository at this point in the history
* Add new variable and regen graphql

* Add dispatch block to the notification handler
  • Loading branch information
ikprk authored Jul 22, 2024
1 parent 73b2211 commit 3fe44fd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
9 changes: 8 additions & 1 deletion packages/atlas/src/api/hooks/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { QueryHookOptions } from '@apollo/client'
import { useRef } from 'react'

import {
GetNftActivitiesCountQuery,
Expand All @@ -10,15 +11,21 @@ import {
useGetNotificationsConnectionQuery,
useMarkNotificationsAsReadMutation,
} from '@/api/queries/__generated__/notifications.generated'
import { useJoystreamStore } from '@/providers/joystream/joystream.store'

import { NftActivityOrderByInput, RecipientTypeWhereInput } from '../queries/__generated__/baseTypes.generated'

export const useRawNotifications = (
recipient: RecipientTypeWhereInput | undefined,
opts?: Pick<QueryHookOptions, 'notifyOnNetworkStatusChange'>
) => {
const currentBlockRef = useRef(useJoystreamStore((store) => store.currentBlock))
const { data, ...rest } = useGetNotificationsConnectionQuery({
variables: { first: 10, recipient: recipient as RecipientTypeWhereInput },
variables: {
first: 10,
recipient: recipient as RecipientTypeWhereInput,
dispatchBlock: currentBlockRef.current ?? 0,
},
...opts,
skip: !recipient,
})
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions packages/atlas/src/api/queries/notifications.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ query GetNotificationsCount($where: NotificationWhereInput!) {
}
}

query GetNotificationsConnection($recipient: RecipientTypeWhereInput!, $first: Int!, $after: String) {
query GetNotificationsConnection(
$recipient: RecipientTypeWhereInput!
$first: Int!
$after: String
$dispatchBlock: Int!
) {
notificationsConnection(
first: $first
after: $after
orderBy: createdAt_DESC
where: { inApp_eq: true, recipient: $recipient }
orderBy: [dispatchBlock_DESC, id_DESC]
where: { inApp_eq: true, recipient: $recipient, dispatchBlock_lte: $dispatchBlock }
) {
pageInfo {
hasNextPage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { FC, useEffect } from 'react'
import { FC, useEffect, useRef } from 'react'

import { atlasConfig } from '@/config'

import { useNotifications } from './notifications.hooks'

import { useJoystreamStore } from '../joystream/joystream.store'

export const NotificationsManager: FC = () => {
const { fetchMore, unseenNotificationsCounts, recipient } = useNotifications()
const { currentBlock } = useJoystreamStore()
const currentBlockRef = useRef(currentBlock)

useEffect(() => {
if (currentBlock) {
currentBlockRef.current = currentBlock
}
}, [currentBlock])

useEffect(() => {
const id = setInterval(() => {
Expand All @@ -15,6 +25,9 @@ export const NotificationsManager: FC = () => {

unseenNotificationsCounts.fetchMore()
fetchMore({
variables: {
dispatchBlock: currentBlockRef.current,
},
updateQuery: (prev, { fetchMoreResult }) => {
if (!Object.keys(prev).length) {
return fetchMoreResult
Expand Down

0 comments on commit 3fe44fd

Please sign in to comment.