diff --git a/landing/landing.react.js b/landing/landing.react.js index 2e0c3ba5c..9b0fbf69f 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 useScrollToTopOnNavigate from './use-scroll-to-top-on-navigate.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]); - + useScrollToTopOnNavigate(); 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/use-scroll-to-top-on-navigate.react.js b/landing/use-scroll-to-top-on-navigate.react.js new file mode 100644 index 000000000..238d1f0ea --- /dev/null +++ b/landing/use-scroll-to-top-on-navigate.react.js @@ -0,0 +1,20 @@ +// @flow + +import * as React from 'react'; +import { useHistory } from 'react-router-dom'; + +function useScrollToTopOnNavigate(): void { + const history = useHistory(); + + return React.useEffect(() => { + const unlisten = history.listen(() => { + window.scrollTo(0, 0); + }); + + return () => { + unlisten(); + }; + }, [history]); +} + +export default useScrollToTopOnNavigate;