2022-05-09 13:04:36 +00:00
|
|
|
import { listContracts } from 'web/lib/firebase/contracts'
|
2022-04-28 23:01:50 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { User } from 'common/user'
|
2022-05-25 21:55:50 +00:00
|
|
|
import dayjs from 'dayjs'
|
|
|
|
import utc from 'dayjs/plugin/utc'
|
|
|
|
dayjs.extend(utc)
|
2022-04-28 23:01:50 +00:00
|
|
|
|
2022-05-17 16:05:51 +00:00
|
|
|
let sessionCreatedContractToday = true
|
2022-05-17 15:32:10 +00:00
|
|
|
|
2022-05-25 21:55:50 +00:00
|
|
|
export function getUtcFreeMarketResetTime(previous: boolean) {
|
|
|
|
const localTimeNow = new Date()
|
|
|
|
const utc4pmToday = dayjs()
|
|
|
|
.utc()
|
|
|
|
.set('hour', 16)
|
|
|
|
.set('minute', 0)
|
|
|
|
.set('second', 0)
|
|
|
|
.set('millisecond', 0)
|
|
|
|
|
|
|
|
// if it's after 4pm UTC today
|
|
|
|
if (localTimeNow.getTime() > utc4pmToday.valueOf()) {
|
|
|
|
return previous
|
|
|
|
? // Return it as it is
|
|
|
|
utc4pmToday.valueOf()
|
|
|
|
: // Or add 24 hours to get the next 4pm UTC time:
|
|
|
|
utc4pmToday.valueOf() + 24 * 60 * 60 * 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4pm UTC today is coming up
|
|
|
|
return previous
|
|
|
|
? // Subtract 24 hours to get the previous 4pm UTC time:
|
|
|
|
utc4pmToday.valueOf() - 24 * 60 * 60 * 1000
|
|
|
|
: // Return it as it is
|
|
|
|
utc4pmToday.valueOf()
|
2022-05-23 14:43:11 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 23:01:50 +00:00
|
|
|
export const useHasCreatedContractToday = (user: User | null | undefined) => {
|
2022-05-23 14:43:11 +00:00
|
|
|
const [hasCreatedContractToday, setHasCreatedContractToday] = useState<
|
|
|
|
boolean | 'loading'
|
|
|
|
>('loading')
|
2022-04-28 23:01:50 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-05-23 14:43:11 +00:00
|
|
|
setHasCreatedContractToday('loading')
|
2022-05-23 21:14:44 +00:00
|
|
|
const previousResetTime = getUtcFreeMarketResetTime(true)
|
2022-04-28 23:01:50 +00:00
|
|
|
async function listUserContractsForToday() {
|
|
|
|
if (!user) return
|
|
|
|
|
|
|
|
const contracts = await listContracts(user.id)
|
|
|
|
const todayContracts = contracts.filter(
|
2022-05-23 21:14:44 +00:00
|
|
|
(contract) => contract.createdTime > previousResetTime
|
2022-04-28 23:01:50 +00:00
|
|
|
)
|
2022-05-17 15:32:10 +00:00
|
|
|
|
|
|
|
sessionCreatedContractToday = todayContracts.length > 0
|
|
|
|
setHasCreatedContractToday(sessionCreatedContractToday)
|
2022-04-28 23:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listUserContractsForToday()
|
|
|
|
}, [user])
|
|
|
|
|
|
|
|
return hasCreatedContractToday
|
|
|
|
}
|