import { useState , useEffect } from 'react' ; import { Subscription } from 'rxjs' ; import { DataPublicPluginStart , TimeRange , RefreshInterval } from 'src/plugins/data/public' ; interface UseTimefilterProps { dateRangeFrom ? : string ; dateRangeTo ? : string ; refreshInterval ? : number ; isRefreshPaused ? : boolean ; timefilter : DataPublicPluginStart [ 'query' ] [ 'timefilter' ] [ 'timefilter' ] ; } export const useTimefilter = ( props ) => { const initialTimeRange = { from : props . dateRangeFrom || props . timefilter . getTime ( ) . from , to : props . dateRangeTo || props . timefilter . getTime ( ) . to , } ; const initialRefreshInterval = { value : props . refreshInterval || props . timefilter . getRefreshInterval ( ) . value , pause : props . isRefreshPaused || props . timefilter . getRefreshInterval ( ) . pause , } ; const [ timeRange , setTimerange ] = useState ( initialTimeRange ) ; const [ refreshInterval , setRefreshInterval ] = useState ( initialRefreshInterval ) ; useEffect ( ( ) => { const subscriptions = new Subscription ( ) ; subscriptions . add ( props . timefilter . getRefreshIntervalUpdate$ ( ) . subscribe ( { next : ( ) => { const newRefreshInterval = props . timefilter . getRefreshInterval ( ) ; setRefreshInterval ( newRefreshInterval ) ; } , } ) ) ; subscriptions . add ( props . timefilter . getTimeUpdate$ ( ) . subscribe ( { next : ( ) => { setTimerange ( props . timefilter . getTime ( ) ) ; } , } ) ) ; return ( ) => { subscriptions . unsubscribe ( ) ; } ; } , [ props . timefilter ] ) ; return { refreshInterval , timeRange , } ; } ;