import { useMemo } from 'react' ; import { useTimeRangeId } from '../context/time_range_id/use_time_range_id' ; import { getDateRange } from '../context/url_params_context/helpers' ; interface TimeRange { start : string ; end : string ; exactStart : string ; exactEnd : string ; refreshTimeRange : ( ) => void ; timeRangeId : number ; } type PartialTimeRange = Pick < TimeRange , 'refreshTimeRange' | 'timeRangeId' > & Pick < Partial < TimeRange > , 'start' | 'end' | 'exactStart' | 'exactEnd' > ; export function useTimeRange ( range ) ; export function useTimeRange ( range ) ; export function useTimeRange ( { rangeFrom , rangeTo , optional , } ) { const { incrementTimeRangeId , timeRangeId } = useTimeRangeId ( ) ; const { start , end , exactStart , exactEnd } = useMemo ( ( ) => { return getDateRange ( { state : { } , rangeFrom , rangeTo , } ) ; } , [ rangeFrom , rangeTo , timeRangeId ] ) ; if ( ( ! start || ! end || ! exactStart || ! exactEnd ) && ! optional ) { throw new Error ( 'start and/or end were unexpectedly not set' ) ; } return { start , end , exactStart , exactEnd , refreshTimeRange : incrementTimeRangeId , timeRangeId , } ; }