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:
parent
1c980ba678
commit
59830579a9
|
@ -160,7 +160,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 nextUtcResetTime = getUtcFreeMarketResetTime(false)
|
const nextUtcResetTime = getUtcFreeMarketResetTime({ previousTime: false })
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
const now = new Date().getTime()
|
const now = new Date().getTime()
|
||||||
const timeUntil = nextUtcResetTime - now
|
const timeUntil = nextUtcResetTime - now
|
||||||
|
|
|
@ -5,9 +5,8 @@ import dayjs from 'dayjs'
|
||||||
import utc from 'dayjs/plugin/utc'
|
import utc from 'dayjs/plugin/utc'
|
||||||
dayjs.extend(utc)
|
dayjs.extend(utc)
|
||||||
|
|
||||||
let sessionCreatedContractToday = true
|
export function getUtcFreeMarketResetTime(options: { previousTime: boolean }) {
|
||||||
|
const { previousTime } = options
|
||||||
export function getUtcFreeMarketResetTime(previous: boolean) {
|
|
||||||
const localTimeNow = new Date()
|
const localTimeNow = new Date()
|
||||||
const utc4pmToday = dayjs()
|
const utc4pmToday = dayjs()
|
||||||
.utc()
|
.utc()
|
||||||
|
@ -18,7 +17,7 @@ export function getUtcFreeMarketResetTime(previous: boolean) {
|
||||||
|
|
||||||
// if it's after 4pm UTC today
|
// if it's after 4pm UTC today
|
||||||
if (localTimeNow.getTime() > utc4pmToday.valueOf()) {
|
if (localTimeNow.getTime() > utc4pmToday.valueOf()) {
|
||||||
return previous
|
return previousTime
|
||||||
? // Return it as it is
|
? // Return it as it is
|
||||||
utc4pmToday.valueOf()
|
utc4pmToday.valueOf()
|
||||||
: // Or add 24 hours to get the next 4pm UTC time:
|
: // 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
|
// 4pm UTC today is coming up
|
||||||
return previous
|
return previousTime
|
||||||
? // Subtract 24 hours to get the previous 4pm UTC time:
|
? // Subtract 24 hours to get the previous 4pm UTC time:
|
||||||
utc4pmToday.valueOf() - 24 * 60 * 60 * 1000
|
utc4pmToday.valueOf() - 24 * 60 * 60 * 1000
|
||||||
: // Return it as it is
|
: // Return it as it is
|
||||||
|
@ -39,9 +38,12 @@ export const useHasCreatedContractToday = (user: User | null | undefined) => {
|
||||||
>('loading')
|
>('loading')
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const nextUtcResetTime = getUtcFreeMarketResetTime({ previousTime: false })
|
||||||
setHasCreatedContractToday('loading')
|
setHasCreatedContractToday('loading')
|
||||||
const previousResetTime = getUtcFreeMarketResetTime(true)
|
|
||||||
async function listUserContractsForToday() {
|
async function listUserContractsForToday() {
|
||||||
|
const previousResetTime = getUtcFreeMarketResetTime({
|
||||||
|
previousTime: true,
|
||||||
|
})
|
||||||
if (!user) return
|
if (!user) return
|
||||||
|
|
||||||
const contracts = await listContracts(user.id)
|
const contracts = await listContracts(user.id)
|
||||||
|
@ -49,11 +51,15 @@ export const useHasCreatedContractToday = (user: User | null | undefined) => {
|
||||||
(contract) => contract.createdTime > previousResetTime
|
(contract) => contract.createdTime > previousResetTime
|
||||||
)
|
)
|
||||||
|
|
||||||
sessionCreatedContractToday = todayContracts.length > 0
|
setHasCreatedContractToday(todayContracts.length > 0)
|
||||||
setHasCreatedContractToday(sessionCreatedContractToday)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const timeoutUntilNextFreeMarket = setTimeout(() => {
|
||||||
|
setHasCreatedContractToday(false)
|
||||||
|
}, nextUtcResetTime - Date.now())
|
||||||
|
|
||||||
listUserContractsForToday()
|
listUserContractsForToday()
|
||||||
|
return () => clearTimeout(timeoutUntilNextFreeMarket)
|
||||||
}, [user])
|
}, [user])
|
||||||
|
|
||||||
return hasCreatedContractToday
|
return hasCreatedContractToday
|
||||||
|
|
Loading…
Reference in New Issue
Block a user