diff --git a/landing/landing.react.js b/landing/landing.react.js index 2e0c3ba5c..5f8a65ca1 100644 --- a/landing/landing.react.js +++ b/landing/landing.react.js @@ -1,61 +1,58 @@ // @flow import * as React from 'react'; -import { useLocation, useRouteMatch } from 'react-router-dom'; +import { useRouteMatch } from 'react-router-dom'; import AppLanding from './app-landing.react'; import Footer from './footer.react'; import Header from './header.react'; import Keyservers from './keyservers.react'; import Privacy from './privacy.react'; import Support from './support.react'; import Terms from './terms.react'; +import useScrollToTop from './useScrollToTop.react'; export type LandingPageName = | 'app' | 'keyservers' | 'privacy' | 'terms' | 'support'; type ActivePage = { name: LandingPageName, node: React.Node }; function Landing(): React.Node { - const { pathname } = useLocation(); - React.useEffect(() => { - window?.scrollTo(0, 0); - }, [pathname]); - + useScrollToTop(); const onPrivacy = useRouteMatch({ path: '/privacy' }); const onTerms = useRouteMatch({ path: '/terms' }); const onSupport = useRouteMatch({ path: '/support' }); const onKeyservers = useRouteMatch({ path: '/keyservers' }); const isLegalPage: boolean = React.useMemo( () => !!(onPrivacy || onTerms || onSupport), [onPrivacy, onSupport, onTerms], ); const activePage: ActivePage = React.useMemo(() => { if (onPrivacy) { return { name: 'privacy', node: }; } else if (onTerms) { return { name: 'terms', node: }; } else if (onSupport) { return { name: 'support', node: }; } else if (onKeyservers) { return { name: 'keyservers', node: }; } else { return { name: 'app', node: }; } }, [onKeyservers, onPrivacy, onSupport, onTerms]); return (
{activePage.node}
); } export default Landing; diff --git a/landing/useScrollToTop.react.js b/landing/useScrollToTop.react.js new file mode 100644 index 000000000..5472aee86 --- /dev/null +++ b/landing/useScrollToTop.react.js @@ -0,0 +1,19 @@ +// @flow +import * as React from 'react'; +import { useHistory } from 'react-router-dom'; + +function useScrollToTop(): void { + const history = useHistory(); + + return React.useEffect(() => { + const unlisten = history.listen(() => { + window.scrollTo(0, 0); + }); + + return () => { + unlisten(); + }; + }, [history.location.pathname, history]); +} + +export default useScrollToTop;