diff --git a/docs/assets/js/main.js b/docs/assets/js/main.js index 9e4880780e91..8b7fa1377fb0 100644 --- a/docs/assets/js/main.js +++ b/docs/assets/js/main.js @@ -49,27 +49,41 @@ function isInRange(num, min, max) { * If a page is directly accessed (e.g., via deep link, bookmark, or opened in a new tab),the user will be navigated * back to the relevant hub page of that article. */ -function navigateBack() { - const currentHost = window.location.host; - const referrer = document.referrer; - - if (referrer.includes(currentHost) && window.history.length > 1) { - window.history.back(); - return; - } - // Path name is of the form /articles/[platform]/[hub]/[resource] - const path = window.location.pathname.split('/'); - if (path[2] && path[3]) { - window.location.href = `/${path[2]}/hubs/${path[3]}`; +function navigateBack() { + const currentPath = window.location.pathname; + const pathSegments = currentPath.split('/'); + let newPath; + + // Check if we're in an article page + if (pathSegments.length >= 5 && pathSegments[1] === 'articles') { + // Extract the product (new-expensify or expensify-classic) and the hub + const product = pathSegments[2]; + const hub = pathSegments[3]; + + // Construct the path to the hub page + newPath = `/${product}/hubs/${hub}/`; + } else if (pathSegments.length > 4 && (pathSegments[2] === 'new-expensify' || pathSegments[2] === 'expensify-classic')) { + // This is the existing logic for other cases + newPath = `/${pathSegments[2]}/hubs/${pathSegments[3]}/`; } else { - window.location.href = '/'; + // If we're not in an article or a recognizable path, go to the home page + newPath = '/'; } - // Add a little delay to avoid showing the previous content in a fraction of a time - setTimeout(toggleHeaderMenu, 250); + if (newPath !== currentPath) { + window.location.href = newPath; + + // Add a little delay to avoid showing the previous content in a fraction of a time + setTimeout(toggleHeaderMenu, 250); + } } +window.addEventListener('popstate', (e)=>{ + e.preventDefault() + navigateBack() +}); + function injectFooterCopywrite() { const footer = document.getElementById('footer-copywrite-date'); footer.innerHTML = `©2008-${new Date().getFullYear()} Expensify, Inc.`; @@ -272,11 +286,13 @@ window.addEventListener('DOMContentLoaded', () => { }); document.getElementById('header-button').addEventListener('click', toggleHeaderMenu); - - // Back button doesn't exist on all the pages + const backButton = document.getElementById('back-button'); if (backButton) { - backButton.addEventListener('click', navigateBack); + backButton.addEventListener('click', function(event) { + event.preventDefault(); + navigateBack(); + }); } const articleContent = document.getElementById('article-content'); @@ -293,6 +309,7 @@ window.addEventListener('DOMContentLoaded', () => { } } }); + lhnContent.addEventListener('wheel', (e) => { const scrollTop = lhnContent.scrollTop; const isScrollingPastLHNTop = e.deltaY < 0 && scrollTop === 0; @@ -301,6 +318,7 @@ window.addEventListener('DOMContentLoaded', () => { e.preventDefault(); } }); + window.addEventListener('scroll', (e) => { const scrollingElement = e.target.scrollingElement; const scrollPercentageInArticleContent = clamp(scrollingElement.scrollTop - articleContent.offsetTop, 0, articleContent.scrollHeight) / articleContent.scrollHeight;