From b1597c0f2478a44e2ddcc4233846c54549820b39 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Wed, 15 Jun 2022 21:00:29 -0500 Subject: [PATCH] Figure out a better hack to fake the url on home page. (And fix bug navigating back to home.) --- web/pages/home.tsx | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/web/pages/home.tsx b/web/pages/home.tsx index ee789eea..14e6cf2b 100644 --- a/web/pages/home.tsx +++ b/web/pages/home.tsx @@ -75,7 +75,7 @@ const useContractPage = () => { const [contract, setContract] = useState() useEffect(() => { - const onBack = () => { + const updateContract = () => { const path = location.pathname.split('/').slice(1) if (path[0] === 'home') setContract(undefined) else { @@ -87,23 +87,24 @@ const useContractPage = () => { } } } - window.addEventListener('popstate', onBack) - // Hack. Listen to changes in href to clear contract on navigate home. - let href = document.location.href - const observer = new MutationObserver(function (_mutations) { - if (href != document.location.href) { - href = document.location.href + const { pushState, replaceState } = window.history - const path = location.pathname.split('/').slice(1) - if (path[0] === 'home') setContract(undefined) - } - }) - observer.observe(document, { subtree: true, childList: true }) + window.history.pushState = function () { + // eslint-disable-next-line prefer-rest-params + pushState.apply(history, arguments as any) + updateContract() + } + + window.history.replaceState = function () { + // eslint-disable-next-line prefer-rest-params + replaceState.apply(history, arguments as any) + updateContract() + } return () => { - window.removeEventListener('popstate', onBack) - observer.disconnect() + window.history.pushState = pushState + window.history.replaceState = replaceState } }, [])