import React , { useState , useEffect } from 'react' ; import useReactRouter from 'use-react-router' ; import * as GlobalData from '@app/common/global-data' ; import { useStore } from 'react-redux' ; import { saveScrollPosition , setScrollPosition } from '@app/redux/actions/scroll' ; import parseUrl from '@app/common/parse-url' ; type Props = { history : any ; location : { pathname : string ; search : string ; hash : string ; params : object ; } ; match : { path : string ; url : string ; isExact : boolean ; params : object ; } ; staticContext : { code : number ; } ; } ; export default function ( Component ) { const Page = function ( { history , location , match , staticContext } ) { if ( typeof window != 'undefined' ) { GlobalData . set ( 'history' , useReactRouter ( ) . history ) ; } const [ notFound , setNotFound ] = useState ( '' ) ; const store = useStore ( ) ; const { pathname , search } = location ; location . params = search ? parseUrl ( search ) : { } ; useEffect ( ( ) => { if ( typeof window != 'undefined' && window . location . pathname != '/' ) { setScrollPosition ( pathname + search ) ( store . dispatch , store . getState ) ; } return ( ) => { if ( typeof window != 'undefined' ) { GlobalData . remove ( 'history' ) ; } } ; } , [ ] ) ; if ( notFound ) { return ( < div className = "container text-center" > { notFound } < /div>); } return ( < Component match = { match } setNotFound = { setNotFound } / > ) ; } ; Page . loadDataOnServer = Component . loadDataOnServer || function ( ) { return { code : 200 } ; } ; return Page ; }