Figure out a better hack to fake the url on home page. (And fix bug navigating back to home.)

This commit is contained in:
James Grugett 2022-06-15 21:00:29 -05:00
parent 6a73dc042c
commit b1597c0f24

View File

@ -75,7 +75,7 @@ const useContractPage = () => {
const [contract, setContract] = useState<Contract | undefined>() const [contract, setContract] = useState<Contract | undefined>()
useEffect(() => { useEffect(() => {
const onBack = () => { const updateContract = () => {
const path = location.pathname.split('/').slice(1) const path = location.pathname.split('/').slice(1)
if (path[0] === 'home') setContract(undefined) if (path[0] === 'home') setContract(undefined)
else { else {
@ -87,23 +87,24 @@ const useContractPage = () => {
} }
} }
} }
window.addEventListener('popstate', onBack)
// Hack. Listen to changes in href to clear contract on navigate home. const { pushState, replaceState } = window.history
let href = document.location.href
const observer = new MutationObserver(function (_mutations) {
if (href != document.location.href) {
href = document.location.href
const path = location.pathname.split('/').slice(1) window.history.pushState = function () {
if (path[0] === 'home') setContract(undefined) // eslint-disable-next-line prefer-rest-params
} pushState.apply(history, arguments as any)
}) updateContract()
observer.observe(document, { subtree: true, childList: true }) }
window.history.replaceState = function () {
// eslint-disable-next-line prefer-rest-params
replaceState.apply(history, arguments as any)
updateContract()
}
return () => { return () => {
window.removeEventListener('popstate', onBack) window.history.pushState = pushState
observer.disconnect() window.history.replaceState = replaceState
} }
}, []) }, [])