import { useMemo } from 'react' ; import { AllSeries , allSeriesKey , convertAllShortSeries , useSeriesStorage , } from './use_series_storage' ; import { ReportViewType , SeriesUrl } from '../types' ; import { ReportTypes } from '../configurations/constants' ; import { parseRelativeDate } from '../components/date_range_picker' ; export const combineTimeRanges = ( reportType , allSeries , firstSeries ? ) => { let to = '' ; let from = '' ; if ( reportType === ReportTypes . KPI ) { return firstSeries ?. time ; } allSeries . forEach ( ( series ) => { if ( series . dataType && series . selectedMetricField && series . time ) { const seriesFrom = parseRelativeDate ( series . time . from ) ! ; const seriesTo = parseRelativeDate ( series . time . to , { roundUp : true } ) ! ; if ( ! to || seriesTo > parseRelativeDate ( to , { roundUp : true } ) ) { to = series . time . to ; } if ( ! from || seriesFrom < parseRelativeDate ( from ) ) { from = series . time . from ; } } } ) ; return { to , from } ; } ; export const useExpViewTimeRange = ( ) => { const { storage , reportType , lastRefresh , firstSeries } = useSeriesStorage ( ) ; return useMemo ( ( ) => { const allSeriesFromUrl = convertAllShortSeries ( storage . get ( allSeriesKey ) ?? [ ] ) ; const firstSeriesT = allSeriesFromUrl ?. [ 0 ] ; return firstSeriesT ? combineTimeRanges ( reportType , allSeriesFromUrl , firstSeriesT ) : undefined ; } , [ reportType , storage , lastRefresh , firstSeries ] ) ; } ;