diff --git a/src/components/content-staking/SuperLike.tsx b/src/components/content-staking/SuperLike.tsx index 6978e967b..0c033f0d8 100644 --- a/src/components/content-staking/SuperLike.tsx +++ b/src/components/content-staking/SuperLike.tsx @@ -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, @@ -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' @@ -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() @@ -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' @@ -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` @@ -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 } } diff --git a/src/services/api/query.ts b/src/services/api/query.ts index 3cd0d6d25..1d5c44bc6 100644 --- a/src/services/api/query.ts +++ b/src/services/api/query.ts @@ -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 { diff --git a/src/services/datahub/posts/subscription.tsx b/src/services/datahub/posts/subscription.tsx index 1176a8c78..577f148e2 100644 --- a/src/services/datahub/posts/subscription.tsx +++ b/src/services/datahub/posts/subscription.tsx @@ -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, @@ -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,