manifold/web/hooks/use-has-created-contract-today.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

import { listContracts } from 'web/lib/firebase/contracts'
import { useEffect, useState } from 'react'
import { User } from 'common/user'
let sessionCreatedContractToday = true
2022-05-17 15:32:10 +00:00
export function getUtcFreeMarketResetTimeToday() {
2022-05-23 17:26:17 +00:00
// 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)
const utcFreeMarketMS = utcFreeMarketResetTime.setUTCHours(16, 0, 0, 0)
return utcFreeMarketMS
}
export const useHasCreatedContractToday = (user: User | null | undefined) => {
const [hasCreatedContractToday, setHasCreatedContractToday] = useState<
boolean | 'loading'
>('loading')
useEffect(() => {
setHasCreatedContractToday('loading')
2022-05-23 17:26:17 +00:00
const todayAtMidnight = getUtcFreeMarketResetTimeYesterday()
async function listUserContractsForToday() {
if (!user) return
const contracts = await listContracts(user.id)
const todayContracts = contracts.filter(
(contract) => contract.createdTime > todayAtMidnight
)
2022-05-17 15:32:10 +00:00
sessionCreatedContractToday = todayContracts.length > 0
setHasCreatedContractToday(sessionCreatedContractToday)
}
listUserContractsForToday()
}, [user])
return hasCreatedContractToday
}