import React from 'react'
import {zonedTimeToUtc} from 'date-fns-tz'
import {isAfter, isBefore, isValid} from 'date-fns'
type TimeCheckReturn = {
before: boolean
after: boolean
}
//TODO: JSDOC this hook
/**
* Custom hook for checking whether a provided time occurs before or after 'now'.
*
* This hook is purely to support consistency in {@link DisplayWhen}'s use of hooks.
*
* It does not do exhaustive validation of data etc... and only operates within the tightly
* defined boundaries outlined in the {@link DisplayWhen} documentation.
*
* This hook should **NOT** be used directly by developers unless you **REALLY REALLY** have to
* and you know what you're doing.
*
* @category Hooks
* @module useTimeCheck
* @deprecated
*/
export function useTimeCheck(time: string) : TimeCheckReturn {
let before = false
let after = false
// If time is blank, return false for everything
if (!time) return {before, after}
try {
let convertedDate = zonedTimeToUtc(time, '+10')
if (!isValid(convertedDate)) {
//TODO: Do we throw exceptions or return "it errored" logic values to the developer?
throw new DOMException("`time` value provided to useTimeCheck is not a valid date.")
}
// Determine if current time is before or after the provided time
before = isBefore(Date.now(), convertedDate)
after = isAfter(Date.now(), convertedDate)
} catch (err) {
//TODO: Richer error handling
console.log(err)
before = false
after = false
}
return {before, after}
}
Source