import { useEffect , useState } from 'react' ; import { useHistory } from 'react-router' ; import { initAxiosInterceptors } from '../../service/http/interceptors' ; import { RESIZE_LATENCY , MAINTENANCE_ALERT_POLLING_TIME , FLUSH_CHART_CACHE_POLLING_TIME } from '../../constants/common' ; import { AppCachedKeys } from '../../constants/cache' ; import { initNodeVersion } from '../../service/app/nodeInfo' ; import { AppDispatch } from '../reducer' ; import { fetchCachedData } from '../../utils/cache' ; import { changeLanguage } from '../../utils/i18n' ; import { useAppState , useDispatch } from '.' ; import { AppActions } from '../actions' ; import { useInterval } from '../../utils/hook' ; import { getMaintenanceInfo } from '../../service/app/alert' ; import flushCacheInfo from '../../service/app/charts/cache' ; const useWindowResize = ( dispatch ) => { useEffect ( ( ) => { let resizeTimer = null ; const resizeListener = ( ) => { if ( resizeTimer ) clearTimeout ( resizeTimer ) ; resizeTimer = setTimeout ( ( ) => { dispatch ( { type : AppActions . ResizeWindow , payload : { appWidth : window . innerWidth , appHeight : window . innerHeight , } , } ) ; resizeTimer = null ; } , RESIZE_LATENCY ) ; } ; window . addEventListener ( 'resize' , resizeListener ) ; return ( ) => { window . removeEventListener ( 'resize' , resizeListener ) ; } ; } , [ dispatch ] ) ; } ; const initAppLanguage = ( app , dispatch ) => { const language = fetchCachedData < 'zh' | 'en' > ( AppCachedKeys . AppLanguage ) || app . language ; setTimeout ( ( ) => { dispatch ( { type : AppActions . UpdateAppLanguage , payload : { language , } , } ) ; } , 0 ) ; changeLanguage ( language ) ; } ; export const useInitApp = ( ) => { const [ init , setInit ] = useState ( false ) ; const { app } = useAppState ( ) ; const dispatch = useDispatch ( ) ; const history = useHistory ( ) ; if ( ! init ) { setInit ( true ) ; initAxiosInterceptors ( dispatch , history ) ; initNodeVersion ( dispatch ) ; initAppLanguage ( app , dispatch ) ; getMaintenanceInfo ( dispatch ) ; flushCacheInfo ( ) ; } useWindowResize ( dispatch ) ; useInterval ( ( ) => { getMaintenanceInfo ( dispatch ) ; } , MAINTENANCE_ALERT_POLLING_TIME ) ; useInterval ( ( ) => { flushCacheInfo ( ) ; } , FLUSH_CHART_CACHE_POLLING_TIME ) ; } ; export default useInitApp ;