diff --git a/src/frontend/clientJS/post-archive.ts b/src/frontend/clientJS/post-archive.ts index 0c582e4..4547102 100644 --- a/src/frontend/clientJS/post-archive.ts +++ b/src/frontend/clientJS/post-archive.ts @@ -244,4 +244,55 @@ window.addEventListener('popstate', () => { initializeArchiveState(); // Update the active post indicator after initializing the archive state setTimeout(updateActivePost, 10); // Small delay to ensure DOM is updated -}); \ No newline at end of file +}); + +// Listen for URL changes (including navigation via other components) +function setupUrlChangeListener() { + // Store the current URL to detect changes + let currentUrl = window.location.pathname; + + // Use MutationObserver to detect DOM changes in the main element + // This helps catch navigation changes from other components + const observer = new MutationObserver((mutations) => { + // Check if URL has changed + if (window.location.pathname !== currentUrl) { + currentUrl = window.location.pathname; + + // Wait a bit for any DOM updates to complete + setTimeout(() => { + updateActivePost(); + initializeArchiveState(); + }, 50); + } + }); + + // Start observing the main element for changes + const mainElement = document.querySelector('main'); + if (mainElement) { + observer.observe(mainElement, { childList: true, subtree: true }); + } + + // Also observe direct URL changes (e.g., when using browser back/forward) + const intervalId = setInterval(() => { + if (window.location.pathname !== currentUrl) { + currentUrl = window.location.pathname; + updateActivePost(); + initializeArchiveState(); + } + }, 500); // Check every 500ms + + // Clean up on page unload + window.addEventListener('beforeunload', () => { + clearInterval(intervalId); + observer.disconnect(); + }); +} + +// Initialize the URL change listener +if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', () => { + setupUrlChangeListener(); + }); +} else { + setupUrlChangeListener(); +} \ No newline at end of file