Skip to content

Commit

Permalink
[Tech] update staleTime for get quest and get user playstreak (#35)
Browse files Browse the repository at this point in the history
* update staleTime

* set stale time for get playstreak to 60s, rm dateTimeCurrentSessionStartedInMsSinceEpoch

* rm unused syncPlaySession

* rm refetchOnMount: false
  • Loading branch information
BrettCleary authored Nov 7, 2024
1 parent e38f0e7 commit b48e0a4
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hyperplay/quests-ui",
"version": "0.0.36",
"version": "0.0.37",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ Rise among Craft World's top ranks. 🚀 Join now and make your mark before the
getExternalTaskCredits: async () => {
return '100'
},
syncPlaySession: async () => {},
logInfo: () => {},
openDiscordLink: () => {},
getDepositContracts: async () => {
Expand Down
9 changes: 5 additions & 4 deletions src/components/QuestDetailsWrapper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
Reward,
RewardClaimSignature,
ConfirmClaimParams,
Runner,
DepositContract
} from '@hyperplay/utils'
import { mintReward } from '../../helpers/mintReward'
Expand Down Expand Up @@ -76,7 +75,6 @@ export interface QuestDetailsWrapperProps {
syncPlayStreakWithExternalSource: (questId: number) => Promise<unknown>
resyncExternalTask: (rewardId: string) => Promise<void>
getExternalTaskCredits: (rewardId: string) => Promise<string>
syncPlaySession: (appName: string, runner: Runner) => Promise<void>
logInfo: (message: string) => void
openDiscordLink: () => void
getDepositContracts: (questId: number) => Promise<DepositContract[]>
Expand Down Expand Up @@ -622,6 +620,9 @@ export function QuestDetailsWrapper({
['ERC1155', 'ERC721', 'ERC20'].includes(reward.reward_type)
)

const dateTimeCurrentSessionStartedInMsSinceEpoch =
questPlayStreakResult?.data.dataUpdatedAt ?? Date.now()

const questDetailsProps: QuestDetailsProps = {
className,
alertProps,
Expand All @@ -642,8 +643,8 @@ export function QuestDetailsWrapper({
playStreak: getPlaystreakArgsFromQuestData({
questMeta,
questPlayStreakData,
useModuleInitTimeForSessionStartTime: isSignedIn,
rightSection: streakRightSection
rightSection: streakRightSection,
dateTimeCurrentSessionStartedInMsSinceEpoch
})
},
rewards: questRewards ?? [],
Expand Down
19 changes: 4 additions & 15 deletions src/helpers/getPlaystreakArgsFromQuestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@ import { PlayStreakEligibility } from '@hyperplay/ui'
import { Quest, UserPlayStreak } from '@hyperplay/utils'
import { ReactNode } from 'react'

// this is initialized when the overlay is started for this game
let dateTimeCurrentSessionStartedInMsSinceEpoch = Date.now()

export function getPlaystreakArgsFromQuestData({
questMeta,
questPlayStreakData,
useModuleInitTimeForSessionStartTime,
rightSection
rightSection,
dateTimeCurrentSessionStartedInMsSinceEpoch
}: {
questMeta: Quest
questPlayStreakData: UserPlayStreak | undefined | null
useModuleInitTimeForSessionStartTime?: boolean
rightSection?: ReactNode
dateTimeCurrentSessionStartedInMsSinceEpoch: number
}): PlayStreakEligibility {
let sessionStartedTime = undefined
if (useModuleInitTimeForSessionStartTime) {
sessionStartedTime = dateTimeCurrentSessionStartedInMsSinceEpoch
}
return {
rightSection,
requiredStreakInDays:
Expand All @@ -32,10 +25,6 @@ export function getPlaystreakArgsFromQuestData({
new Date().toISOString(),
accumulatedPlaytimeTodayInSeconds:
questPlayStreakData?.accumulated_playtime_today_in_seconds ?? 0,
dateTimeCurrentSessionStartedInMsSinceEpoch: sessionStartedTime
dateTimeCurrentSessionStartedInMsSinceEpoch
}
}

export function resetSessionStartedTime() {
dateTimeCurrentSessionStartedInMsSinceEpoch = Date.now()
}
4 changes: 3 additions & 1 deletion src/hooks/useGetQuest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export function getQuestQueryOptions(
return response
},
refetchOnWindowFocus: false,
enabled: questId !== null
enabled: questId !== null,
// choosing 60s over Infinity here to keep the num of claims left value fresh
staleTime: 60 * 1000
})
}

Expand Down
1 change: 1 addition & 0 deletions src/hooks/useGetUserPlayStreak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function getUserPlaystreakQueryOptions(
const queryKey = getGetUserPlayStreakQueryKey(questId)
return queryOptions({
queryKey,
staleTime: 60 * 1000,
queryFn: async () => {
if (questId === null) {
return null
Expand Down
15 changes: 3 additions & 12 deletions src/state/QuestPlayStreakSyncState.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { Quest, Runner, UserPlayStreak } from '@hyperplay/utils'
import { makeAutoObservable } from 'mobx'
import { QueryClient } from '@tanstack/query-core'
import { resetSessionStartedTime } from '@/helpers/getPlaystreakArgsFromQuestData'
import {
getGetUserPlayStreakQueryKey,
getSyncPlaysessionQueryKey
} from '@/helpers/getQueryKeys'
import { getSyncPlaysessionQueryKey } from '@/helpers/getQueryKeys'
import { getQuestQueryOptions } from '@/hooks/useGetQuest'
import { getUserPlaystreakQueryOptions } from '@/hooks/useGetUserPlayStreak'

Expand Down Expand Up @@ -101,14 +97,9 @@ class QuestPlayStreakSyncState {
projectId,
questMeta?.quest_external_game?.runner ?? 'hyperplay'
)
resetSessionStartedTime()
// all quest user playstreak data needs to be refetched after playsession sync
for (const questToInvalidate of quests) {
const queryKey = getGetUserPlayStreakQueryKey(
questToInvalidate.id
)
this.appQueryClient?.invalidateQueries({ queryKey })
}
const queryKey = ['getUserPlayStreak']
this.appQueryClient?.invalidateQueries({ queryKey })
return null
}
})
Expand Down

0 comments on commit b48e0a4

Please sign in to comment.