From fb33f829cc21db12bf3cafa2fe73bf3104cf181e Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Mon, 23 May 2022 15:14:44 -0600 Subject: [PATCH] Clean up free market countdown bugfix --- web/components/nav/sidebar.tsx | 4 ++-- web/hooks/use-has-created-contract-today.ts | 18 ++++++------------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/web/components/nav/sidebar.tsx b/web/components/nav/sidebar.tsx index 59ff8a26..3b030f2f 100644 --- a/web/components/nav/sidebar.tsx +++ b/web/components/nav/sidebar.tsx @@ -21,7 +21,7 @@ import { ManifoldLogo } from './manifold-logo' import { MenuButton } from './menu' import { getNavigationOptions, ProfileSummary } from './profile-menu' import { - getUtcFreeMarketResetTimeToday, + getUtcFreeMarketResetTime, useHasCreatedContractToday, } from 'web/hooks/use-has-created-contract-today' import { Row } from '../layout/row' @@ -127,7 +127,7 @@ export default function Sidebar(props: { className?: string }) { const currentPage = router.pathname const [countdown, setCountdown] = useState('...') useEffect(() => { - const utcMidnightToLocalDate = new Date(getUtcFreeMarketResetTimeToday()) + const utcMidnightToLocalDate = new Date(getUtcFreeMarketResetTime(false)) const interval = setInterval(() => { const timeUntil = utcMidnightToLocalDate.getTime() - new Date().getTime() const hoursUntil = 24 + timeUntil / 1000 / 60 / 60 diff --git a/web/hooks/use-has-created-contract-today.ts b/web/hooks/use-has-created-contract-today.ts index 44ea4287..f63f5ac1 100644 --- a/web/hooks/use-has-created-contract-today.ts +++ b/web/hooks/use-has-created-contract-today.ts @@ -4,18 +4,12 @@ import { User } from 'common/user' let sessionCreatedContractToday = true -export function getUtcFreeMarketResetTimeToday() { +export function getUtcFreeMarketResetTime(yesterday: boolean) { // Uses utc time like the server. const utcFreeMarketResetTime = new Date() - utcFreeMarketResetTime.setUTCDate(utcFreeMarketResetTime.getUTCDate()) - const utcFreeMarketMS = utcFreeMarketResetTime.setUTCHours(16, 0, 0, 0) - return utcFreeMarketMS -} - -function getUtcFreeMarketResetTimeYesterday() { - // Uses utc time like the server. - const utcFreeMarketResetTime = new Date() - utcFreeMarketResetTime.setUTCDate(utcFreeMarketResetTime.getUTCDate() - 1) + utcFreeMarketResetTime.setUTCDate( + utcFreeMarketResetTime.getUTCDate() - (yesterday ? 1 : 0) + ) const utcFreeMarketMS = utcFreeMarketResetTime.setUTCHours(16, 0, 0, 0) return utcFreeMarketMS } @@ -27,13 +21,13 @@ export const useHasCreatedContractToday = (user: User | null | undefined) => { useEffect(() => { setHasCreatedContractToday('loading') - const todayAtMidnight = getUtcFreeMarketResetTimeYesterday() + const previousResetTime = getUtcFreeMarketResetTime(true) async function listUserContractsForToday() { if (!user) return const contracts = await listContracts(user.id) const todayContracts = contracts.filter( - (contract) => contract.createdTime > todayAtMidnight + (contract) => contract.createdTime > previousResetTime ) sessionCreatedContractToday = todayContracts.length > 0