Skip to content

Commit

Permalink
Merge pull request #684 from dappforce/deploy/cache
Browse files Browse the repository at this point in the history
Remove can be super liked query
  • Loading branch information
teodorus-nathaniel authored Jul 4, 2024
2 parents eccebf4 + e49bfbf commit 6f23da1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 37 deletions.
42 changes: 11 additions & 31 deletions src/components/content-staking/SuperLike.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Thumbsup from '@/assets/emojis/thumbsup.png'
import { useIsAddressBlockedInApp } from '@/hooks/useIsAddressBlockedInApp'
import { getPostQuery } from '@/services/api/query'
import { getPostQuery, getServerTimeQuery } from '@/services/api/query'
import { useCreateSuperLike } from '@/services/datahub/content-staking/mutation'
import {
PostRewards,
getAddressLikeCountToPostQuery,
getCanPostSuperLikedQuery,
getConfirmationMsgQuery,
getSuperLikeCountQuery,
getTodaySuperLikeCountQuery,
Expand All @@ -21,13 +20,7 @@ import { LocalStorage } from '@/utils/storage'
import dayjs from 'dayjs'
import { verifyMessage } from 'ethers'
import Image from 'next/image'
import {
ComponentProps,
ReactNode,
SyntheticEvent,
useEffect,
useState,
} from 'react'
import { ComponentProps, ReactNode, SyntheticEvent } from 'react'
import { toast } from 'sonner'
import PopOver from '../floating/PopOver'
import { sendEventWithRef } from '../referral/analytics'
Expand Down Expand Up @@ -70,13 +63,8 @@ export function SuperLikeWrapper({
const { mutate: createSuperLike } = useCreateSuperLike()
const { data: superLikeCount } = getSuperLikeCountQuery.useQuery(postId)

const clientCanPostSuperLiked = useClientValidationOfPostSuperLike(
post?.struct.createdAtTime ?? 0
)
const { data: canPostSuperLike, isLoading: loadingCanPostSuperLiked } =
getCanPostSuperLikedQuery.useQuery(postId)
const { canPostSuperLiked, isExist, validByCreatorMinStake } =
canPostSuperLike || {}
const { canBeLiked: canBeSuperliked, isLoading: loadingCanBeLiked } =
useClientValidationOfPostSuperLike(post?.struct.createdAtTime ?? 0)

const myAddress = useMyMainAddress()
const myGrillAddress = useMyAccount.use.address()
Expand All @@ -95,7 +83,6 @@ export function SuperLikeWrapper({
const hasILiked = (myLike?.count ?? 0) > 0
const isMyPost = post?.struct.ownerId === myAddress

const canBeSuperliked = clientCanPostSuperLiked && canPostSuperLiked
// const entity = post?.struct.isComment ? 'message' : 'post'
const entity = 'memes'

Expand All @@ -105,22 +92,18 @@ export function SuperLikeWrapper({
loadingMyLike ||
loadingBlocked ||
loadingTodayCount ||
loadingCanPostSuperLiked ||
loadingCanBeLiked ||
hasLikedMoreThanLimit ||
!message) &&
!hasILiked

let disabledCause = ''
if (loadingBlocked || loadingTodayCount || loadingCanPostSuperLiked)
if (loadingBlocked || loadingTodayCount || loadingCanBeLiked)
disabledCause = 'Loading...'
else if (isMyPost) {
disabledCause = `You cannot like your own ${entity}`
} else if (hasLikedMoreThanLimit)
disabledCause = `You've liked 10 ${entity} today. Come back tomorrow for more fun!`
else if (!isExist)
disabledCause = `This ${entity} is still being minted, please wait a few seconds`
else if (!validByCreatorMinStake)
disabledCause = `This ${entity} cannot be liked because its author has not yet locked at least 2,000 SUB`
else if (!canBeSuperliked)
disabledCause = `You cannot like ${entity}s that are older than 7 days`

Expand Down Expand Up @@ -264,14 +247,11 @@ const currentWeekSigStorage = new LocalStorage(() => 'df.current-week-sig')

const CLIENT_CHECK_INTERVAL = 5 * 1000 * 60 // 5 minutes
function useClientValidationOfPostSuperLike(createdAtTime: number) {
const [, setState] = useState({})

useEffect(() => {
const interval = setInterval(() => setState({}), CLIENT_CHECK_INTERVAL) // refresh every interval
return () => clearInterval(interval)
}, [])
const { data: serverTime, isLoading } = getServerTimeQuery.useQuery(null, {
refetchInterval: CLIENT_CHECK_INTERVAL,
})

const isPostMadeMoreThan1WeekAgo =
dayjs().diff(dayjs(createdAtTime), 'day') > 7
return !isPostMadeMoreThan1WeekAgo
dayjs(serverTime).diff(dayjs(createdAtTime), 'day') > 7
return { canBeLiked: !isPostMadeMoreThan1WeekAgo, isLoading }
}
4 changes: 4 additions & 0 deletions src/services/api/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ export async function getServerTime() {
throw new Error('Failed to get server time')
}
}
export const getServerTimeQuery = createQuery({
key: 'server-time',
fetcher: getServerTime,
})

async function getServerDay() {
try {
Expand Down
7 changes: 1 addition & 6 deletions src/services/datahub/posts/subscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { QueryClient, useQueryClient } from '@tanstack/react-query'
import { gql } from 'graphql-request'
import { useEffect, useRef } from 'react'
import { toast } from 'sonner'
import {
getCanPostSuperLikedQuery,
getTokenomicsMetadataQuery,
} from '../content-staking/query'
import { getTokenomicsMetadataQuery } from '../content-staking/query'
import {
DataHubSubscriptionEventEnum,
SubscribePostSubscription,
Expand Down Expand Up @@ -159,8 +156,6 @@ async function processMessage(
}

if (isCreationEvent) {
getCanPostSuperLikedQuery.fetchQuery(queryClient, newestId, true)

const newPost = getPostQuery.getQueryData(queryClient, newestId)
const tokenomics = await getTokenomicsMetadataQuery.fetchQuery(
queryClient,
Expand Down

0 comments on commit 6f23da1

Please sign in to comment.