From 59830579a9ae15f5a8aeccc2e8b05055a007ff9e Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Wed, 1 Jun 2022 07:54:48 -0600 Subject: [PATCH] Set timeout to reset the free market banner (#367) * wip - trigger recheck when past time * wip - trigger recheck when past time * Reset logic * Interval=>timeout --- web/components/nav/sidebar.tsx | 2 +- web/hooks/use-has-created-contract-today.ts | 22 +++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/web/components/nav/sidebar.tsx b/web/components/nav/sidebar.tsx index 1fd5743f..70396676 100644 --- a/web/components/nav/sidebar.tsx +++ b/web/components/nav/sidebar.tsx @@ -160,7 +160,7 @@ export default function Sidebar(props: { className?: string }) { const currentPage = router.pathname const [countdown, setCountdown] = useState('...') useEffect(() => { - const nextUtcResetTime = getUtcFreeMarketResetTime(false) + const nextUtcResetTime = getUtcFreeMarketResetTime({ previousTime: false }) const interval = setInterval(() => { const now = new Date().getTime() const timeUntil = nextUtcResetTime - now diff --git a/web/hooks/use-has-created-contract-today.ts b/web/hooks/use-has-created-contract-today.ts index 8f05287c..eac2c78e 100644 --- a/web/hooks/use-has-created-contract-today.ts +++ b/web/hooks/use-has-created-contract-today.ts @@ -5,9 +5,8 @@ import dayjs from 'dayjs' import utc from 'dayjs/plugin/utc' dayjs.extend(utc) -let sessionCreatedContractToday = true - -export function getUtcFreeMarketResetTime(previous: boolean) { +export function getUtcFreeMarketResetTime(options: { previousTime: boolean }) { + const { previousTime } = options const localTimeNow = new Date() const utc4pmToday = dayjs() .utc() @@ -18,7 +17,7 @@ export function getUtcFreeMarketResetTime(previous: boolean) { // if it's after 4pm UTC today if (localTimeNow.getTime() > utc4pmToday.valueOf()) { - return previous + return previousTime ? // Return it as it is utc4pmToday.valueOf() : // Or add 24 hours to get the next 4pm UTC time: @@ -26,7 +25,7 @@ export function getUtcFreeMarketResetTime(previous: boolean) { } // 4pm UTC today is coming up - return previous + return previousTime ? // Subtract 24 hours to get the previous 4pm UTC time: utc4pmToday.valueOf() - 24 * 60 * 60 * 1000 : // Return it as it is @@ -39,9 +38,12 @@ export const useHasCreatedContractToday = (user: User | null | undefined) => { >('loading') useEffect(() => { + const nextUtcResetTime = getUtcFreeMarketResetTime({ previousTime: false }) setHasCreatedContractToday('loading') - const previousResetTime = getUtcFreeMarketResetTime(true) async function listUserContractsForToday() { + const previousResetTime = getUtcFreeMarketResetTime({ + previousTime: true, + }) if (!user) return const contracts = await listContracts(user.id) @@ -49,11 +51,15 @@ export const useHasCreatedContractToday = (user: User | null | undefined) => { (contract) => contract.createdTime > previousResetTime ) - sessionCreatedContractToday = todayContracts.length > 0 - setHasCreatedContractToday(sessionCreatedContractToday) + setHasCreatedContractToday(todayContracts.length > 0) } + const timeoutUntilNextFreeMarket = setTimeout(() => { + setHasCreatedContractToday(false) + }, nextUtcResetTime - Date.now()) + listUserContractsForToday() + return () => clearTimeout(timeoutUntilNextFreeMarket) }, [user]) return hasCreatedContractToday