Skip to content

Commit

Permalink
Use superlike weight for superlike toast
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Jun 14, 2024
1 parent 0724a2e commit 887b780
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import Toast from '@/components/Toast'
import { useMyMainAddress } from '@/stores/my-account'
import { useSubscriptionState } from '@/stores/subscription'
import { cx } from '@/utils/class-names'
import { DataHubSubscriptionEventEnum } from '@subsocial/data-hub-sdk'
import { QueryClient, useQueryClient } from '@tanstack/react-query'
import { gql } from 'graphql-request'
import { ReactNode, useEffect, useRef } from 'react'
import { toast } from 'sonner'
import { datahubSubscription, isDatahubAvailable } from '../utils'
import { useEffect, useRef } from 'react'
import {
getAddressLikeCountToPostQuery,
getSuperLikeCountQuery,
getTodaySuperLikeCountQuery,
getTokenomicsMetadataQuery,
} from './query'
DataHubSubscriptionEventEnum,
SubscribeSuperLikeSubscription,
} from '../generated-query'
import { datahubSubscription, isDatahubAvailable } from '../utils'
import { toastSuperLikeNotification } from './utils'

export function useDatahubContentStakingSubscriber() {
const queryClient = useQueryClient()
Expand Down Expand Up @@ -58,6 +53,9 @@ const SUBSCRIBE_SUPER_LIKE = gql`
subscription SubscribeSuperLike {
activeStakingSuperLike {
event
meta {
stakerDistributedRewardPoints
}
entity {
staker {
id
Expand All @@ -79,7 +77,7 @@ export function subscription(
isSubscribed = true

const client = datahubSubscription()
let unsub = client.subscribe(
let unsub = client.subscribe<SubscribeSuperLikeSubscription>(
{
query: SUBSCRIBE_SUPER_LIKE,
},
Expand All @@ -105,69 +103,16 @@ export function subscription(

async function processSubscriptionEvent(
queryClient: QueryClient,
eventData: {
event: DataHubSubscriptionEventEnum
entity: { staker: { id: string }; post: { persistentId: string } }
},
eventData: SubscribeSuperLikeSubscription['activeStakingSuperLike'],
myAddress: string | undefined
) {
if (
eventData.event !==
DataHubSubscriptionEventEnum.ACTIVE_STAKING_SUPER_LIKE_CREATED &&
DataHubSubscriptionEventEnum.ActiveStakingSuperLikeCreated &&
eventData.event !==
DataHubSubscriptionEventEnum.ACTIVE_STAKING_SUPER_LIKE_STATE_UPDATED
DataHubSubscriptionEventEnum.ActiveStakingSuperLikeStateUpdated
)
return

const { post, staker } = eventData.entity

getSuperLikeCountQuery.invalidate(queryClient, post.persistentId)
if (staker.id === myAddress) {
getAddressLikeCountToPostQuery.invalidate(queryClient, {
address: myAddress,
postId: post.persistentId,
})
const todayLike = getTodaySuperLikeCountQuery.getQueryData(
queryClient,
myAddress
)
const remaining = 10 - todayLike.count
let desc: ReactNode =
'Great progress today! You used all the available likes. Come back tomorrow 😉'
let icon: ((className: string) => ReactNode) | undefined = (className) => (
<span
className={cx(className, 'relative top-px mr-1.5 self-start text-base')}
>
</span>
)
if (remaining > 0) {
const tokenomics = await getTokenomicsMetadataQuery.fetchQuery(
queryClient,
null
)
desc = (
<div>
<p>
You earned 💎 {tokenomics.superLikeWeightPoints} Points for liking
the meme.
</p>
<p className='text-text-muted'>
{remaining} more likes left for today
</p>
</div>
)
icon = (className) => (
<span
className={cx(
className,
'relative top-px mr-1.5 self-start text-base'
)}
>
ℹ️
</span>
)
}
toast.custom((t) => <Toast t={t} title={desc} icon={icon} />)
}
toastSuperLikeNotification(queryClient, eventData, myAddress)
}
64 changes: 64 additions & 0 deletions src/services/datahub/content-staking/utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Toast from '@/components/Toast'
import { cx } from '@/utils/class-names'
import { QueryClient } from '@tanstack/react-query'
import { ReactNode } from 'react'
import { toast } from 'sonner'
import { SubscribeSuperLikeSubscription } from '../generated-query'
import {
getAddressLikeCountToPostQuery,
getSuperLikeCountQuery,
getTodaySuperLikeCountQuery,
} from './query'

export function toastSuperLikeNotification(
queryClient: QueryClient,
eventData: SubscribeSuperLikeSubscription['activeStakingSuperLike'],
myAddress: string | undefined
) {
const { post, staker } = eventData.entity
getSuperLikeCountQuery.invalidate(queryClient, post.persistentId)
if (staker.id === myAddress && post.persistentId) {
getAddressLikeCountToPostQuery.invalidate(queryClient, {
address: myAddress,
postId: post.persistentId,
})
const todayLike = getTodaySuperLikeCountQuery.getQueryData(
queryClient,
myAddress
)
const remaining = 10 - todayLike.count
let desc: ReactNode =
'Great progress today! You used all the available likes. Come back tomorrow 😉'
let icon: ((className: string) => ReactNode) | undefined = (className) => (
<span
className={cx(className, 'relative top-px mr-1.5 self-start text-base')}
>
</span>
)
if (remaining > 0) {
desc = (
<div>
<p>
You earned 💎 {eventData.meta.stakerDistributedRewardPoints} Points
for liking the meme.
</p>
<p className='text-text-muted'>
{remaining} more likes left for today
</p>
</div>
)
icon = (className) => (
<span
className={cx(
className,
'relative top-px mr-1.5 self-start text-base'
)}
>
ℹ️
</span>
)
}
toast.custom((t) => <Toast t={t} title={desc} icon={icon} />)
}
}
7 changes: 7 additions & 0 deletions src/services/datahub/generated-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2113,6 +2113,10 @@ export type SubscribeSuperLikeSubscription = {
activeStakingSuperLike: {
__typename?: 'SuperLikeSubscriptionPayload'
event: DataHubSubscriptionEventEnum
meta: {
__typename?: 'SubscriptionPayloadMeta'
stakerDistributedRewardPoints: string
}
entity: {
__typename?: 'ActiveStakingSuperLike'
staker: { __typename?: 'Account'; id: string }
Expand Down Expand Up @@ -3350,6 +3354,9 @@ export const SubscribeSuperLike = gql`
subscription SubscribeSuperLike {
activeStakingSuperLike {
event
meta {
stakerDistributedRewardPoints
}
entity {
staker {
id
Expand Down

0 comments on commit 887b780

Please sign in to comment.