Corrected forward and back logic

This commit is contained in:
Connor Yoh 2025-08-28 12:23:50 +01:00
parent fe1c44fdaa
commit 1e866bc31e
2 changed files with 16 additions and 5 deletions

View File

@ -2,7 +2,7 @@
* URL synchronization hooks for tool routing with registry support
*/
import { useEffect, useCallback } from 'react';
import { useEffect, useCallback, useRef } from 'react';
import { WorkbenchType, ToolId } from '../types/navigation';
import { parseToolRoute, updateToolRoute, clearToolRoute } from '../utils/urlRouting';
import { ToolRegistry } from '../data/toolsTaxonomy';
@ -18,10 +18,16 @@ export function useNavigationUrlSync(
registry: ToolRegistry,
enableSync: boolean = true
) {
const hasInitialized = useRef(false);
const prevSelectedTool = useRef<ToolId | null>(null);
// Initialize workbench and tool from URL on mount
useEffect(() => {
if (!enableSync) return;
// Fire pixel for initial page load
const currentPath = window.location.pathname;
firePixel(currentPath);
const route = parseToolRoute(registry);
if (route.toolId !== selectedTool) {
if (route.toolId) {
@ -32,6 +38,8 @@ export function useNavigationUrlSync(
clearToolSelection();
}
}
hasInitialized.current = true;
}, []); // Only run on mount
// Update URL when tool or workbench changes
@ -40,12 +48,15 @@ export function useNavigationUrlSync(
if (selectedTool) {
updateToolRoute(selectedTool, registry, false); // Use pushState for user navigation
} else {
// Only clear URL if we're not on the home page already
} else if (prevSelectedTool.current !== null) {
// Only clear URL if we had a tool before (user navigated away)
// Don't clear on initial load when both current and previous are null
if (window.location.pathname !== '/') {
clearToolRoute(false); // Use pushState for user navigation
}
}
prevSelectedTool.current = selectedTool;
}, [selectedTool, registry, enableSync]);
// Handle browser back/forward navigation