import React , { useEffect } from 'react' ; import { withRouter } from 'react-router' ; import { History } from 'history' ; import { eventType } from '@ringcentral/web-apps-common' ; export * from '@ringcentral/web-apps-common' ; export const makeHistoryFromRouter = ( router ) : History => ( { listen : ( ... args ) => router . listen ( ... args ) , replace : ( ... args ) => router . replace ( ... args ) , push : ( ... args ) => router . push ( ... args ) , get location ( ) { return router . getCurrentLocation ( ) ; } , } as History ) ; export const normalizeHistory = ( { history , router } ) : History => ( router ? makeHistoryFromRouter ( router ) : history ) ; export const LocationSync = withRouter ( ( { history , router } ) => { useEffect ( ( ) => { const normalHistory = normalizeHistory ( { history , router } ) ; let timeout ; const check = ( ) => { if ( window . location . pathname === normalHistory . location . pathname ) return ; const windowHref = normalHistory . createHref ( window . location ) ; const historyHref = normalHistory . createHref ( normalHistory . location ) ; if ( windowHref === historyHref ) return ; console . log ( 'Synchronized URLs' , windowHref , '->' , historyHref ) ; normalHistory . replace ( windowHref ) ; } ; const tick = ( ) => { check ( ) ; timeout = setTimeout ( tick , 100 ) ; } ; tick ( ) ; return ( ) => clearTimeout ( timeout ) ; } , [ history , router ] ) ; return < > < />; } ) ; export const useListenerEffect = ( node , event , listener ) => { useEffect ( ( ) => { if ( ! node || ! listener ) return ; node . addEventListener ( event , listener ) ; return ( ) => node && node . removeEventListener ( event , listener ) ; } , [ node , listener , event ] ) ; } ;