Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove can be super liked query #684

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading