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
This commit is contained in:
Ian Philips 2022-06-01 07:54:48 -06:00 committed by GitHub
parent 1c980ba678
commit 59830579a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 9 deletions

View File

@ -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

View File

@ -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