Clean up free market countdown bugfix

This commit is contained in:
Ian Philips 2022-05-23 15:14:44 -06:00
parent 2b13b1adb7
commit fb33f829cc
2 changed files with 8 additions and 14 deletions

View File

@ -21,7 +21,7 @@ import { ManifoldLogo } from './manifold-logo'
import { MenuButton } from './menu' import { MenuButton } from './menu'
import { getNavigationOptions, ProfileSummary } from './profile-menu' import { getNavigationOptions, ProfileSummary } from './profile-menu'
import { import {
getUtcFreeMarketResetTimeToday, getUtcFreeMarketResetTime,
useHasCreatedContractToday, useHasCreatedContractToday,
} from 'web/hooks/use-has-created-contract-today' } from 'web/hooks/use-has-created-contract-today'
import { Row } from '../layout/row' import { Row } from '../layout/row'
@ -127,7 +127,7 @@ export default function Sidebar(props: { className?: string }) {
const currentPage = router.pathname const currentPage = router.pathname
const [countdown, setCountdown] = useState('...') const [countdown, setCountdown] = useState('...')
useEffect(() => { useEffect(() => {
const utcMidnightToLocalDate = new Date(getUtcFreeMarketResetTimeToday()) const utcMidnightToLocalDate = new Date(getUtcFreeMarketResetTime(false))
const interval = setInterval(() => { const interval = setInterval(() => {
const timeUntil = utcMidnightToLocalDate.getTime() - new Date().getTime() const timeUntil = utcMidnightToLocalDate.getTime() - new Date().getTime()
const hoursUntil = 24 + timeUntil / 1000 / 60 / 60 const hoursUntil = 24 + timeUntil / 1000 / 60 / 60

View File

@ -4,18 +4,12 @@ import { User } from 'common/user'
let sessionCreatedContractToday = true let sessionCreatedContractToday = true
export function getUtcFreeMarketResetTimeToday() { export function getUtcFreeMarketResetTime(yesterday: boolean) {
// Uses utc time like the server. // Uses utc time like the server.
const utcFreeMarketResetTime = new Date() const utcFreeMarketResetTime = new Date()
utcFreeMarketResetTime.setUTCDate(utcFreeMarketResetTime.getUTCDate()) utcFreeMarketResetTime.setUTCDate(
const utcFreeMarketMS = utcFreeMarketResetTime.setUTCHours(16, 0, 0, 0) utcFreeMarketResetTime.getUTCDate() - (yesterday ? 1 : 0)
return utcFreeMarketMS )
}
function getUtcFreeMarketResetTimeYesterday() {
// Uses utc time like the server.
const utcFreeMarketResetTime = new Date()
utcFreeMarketResetTime.setUTCDate(utcFreeMarketResetTime.getUTCDate() - 1)
const utcFreeMarketMS = utcFreeMarketResetTime.setUTCHours(16, 0, 0, 0) const utcFreeMarketMS = utcFreeMarketResetTime.setUTCHours(16, 0, 0, 0)
return utcFreeMarketMS return utcFreeMarketMS
} }
@ -27,13 +21,13 @@ export const useHasCreatedContractToday = (user: User | null | undefined) => {
useEffect(() => { useEffect(() => {
setHasCreatedContractToday('loading') setHasCreatedContractToday('loading')
const todayAtMidnight = getUtcFreeMarketResetTimeYesterday() const previousResetTime = getUtcFreeMarketResetTime(true)
async function listUserContractsForToday() { async function listUserContractsForToday() {
if (!user) return if (!user) return
const contracts = await listContracts(user.id) const contracts = await listContracts(user.id)
const todayContracts = contracts.filter( const todayContracts = contracts.filter(
(contract) => contract.createdTime > todayAtMidnight (contract) => contract.createdTime > previousResetTime
) )
sessionCreatedContractToday = todayContracts.length > 0 sessionCreatedContractToday = todayContracts.length > 0