From b9ae919fdacfe66892afe4f1a5500d2277aa389a Mon Sep 17 00:00:00 2001 From: James Grugett Date: Thu, 8 Sep 2022 16:59:05 -0500 Subject: [PATCH] Add staleTime option for prefetching --- web/hooks/use-contracts.ts | 7 +++++-- web/hooks/use-portfolio-history.ts | 12 ++++++++---- web/hooks/use-user-bets.ts | 7 ++++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/web/hooks/use-contracts.ts b/web/hooks/use-contracts.ts index 4d7d2f79..2f3bea7b 100644 --- a/web/hooks/use-contracts.ts +++ b/web/hooks/use-contracts.ts @@ -13,6 +13,7 @@ import { getUserBetContractsQuery, } from 'web/lib/firebase/contracts' import { useQueryClient } from 'react-query' +import { MINUTE_MS } from 'common/util/time' export const useContracts = () => { const [contracts, setContracts] = useState() @@ -96,8 +97,10 @@ export const useUpdatedContracts = (contracts: Contract[] | undefined) => { export const usePrefetchUserBetContracts = (userId: string) => { const queryClient = useQueryClient() - return queryClient.prefetchQuery(['contracts', 'bets', userId], () => - getUserBetContracts(userId) + return queryClient.prefetchQuery( + ['contracts', 'bets', userId], + () => getUserBetContracts(userId), + { staleTime: 5 * MINUTE_MS } ) } diff --git a/web/hooks/use-portfolio-history.ts b/web/hooks/use-portfolio-history.ts index 1945eb7a..6cc1a84e 100644 --- a/web/hooks/use-portfolio-history.ts +++ b/web/hooks/use-portfolio-history.ts @@ -1,6 +1,6 @@ import { useQueryClient } from 'react-query' import { useFirestoreQueryData } from '@react-query-firebase/firestore' -import { DAY_MS, HOUR_MS } from 'common/util/time' +import { DAY_MS, HOUR_MS, MINUTE_MS } from 'common/util/time' import { getPortfolioHistory, getPortfolioHistoryQuery, @@ -15,8 +15,10 @@ const getCutoff = (period: Period) => { export const usePrefetchPortfolioHistory = (userId: string, period: Period) => { const queryClient = useQueryClient() const cutoff = getCutoff(period) - return queryClient.prefetchQuery(['portfolio-history', userId, cutoff], () => - getPortfolioHistory(userId, cutoff) + return queryClient.prefetchQuery( + ['portfolio-history', userId, cutoff], + () => getPortfolioHistory(userId, cutoff), + { staleTime: 15 * MINUTE_MS } ) } @@ -24,7 +26,9 @@ export const usePortfolioHistory = (userId: string, period: Period) => { const cutoff = getCutoff(period) const result = useFirestoreQueryData( ['portfolio-history', userId, cutoff], - getPortfolioHistoryQuery(userId, cutoff) + getPortfolioHistoryQuery(userId, cutoff), + {}, + { staleTime: 15 * MINUTE_MS } ) return result.data } diff --git a/web/hooks/use-user-bets.ts b/web/hooks/use-user-bets.ts index 8f0bd9f7..3731fb07 100644 --- a/web/hooks/use-user-bets.ts +++ b/web/hooks/use-user-bets.ts @@ -7,10 +7,15 @@ import { getUserBetsQuery, listenForUserContractBets, } from 'web/lib/firebase/bets' +import { MINUTE_MS } from 'common/util/time' export const usePrefetchUserBets = (userId: string) => { const queryClient = useQueryClient() - return queryClient.prefetchQuery(['bets', userId], () => getUserBets(userId)) + return queryClient.prefetchQuery( + ['bets', userId], + () => getUserBets(userId), + { staleTime: MINUTE_MS } + ) } export const useUserBets = (userId: string) => {