From 2cada1697ccddd63ad00854f5619b51ab44a9643 Mon Sep 17 00:00:00 2001 From: Connor Yoh Date: Thu, 28 Aug 2025 11:50:23 +0100 Subject: [PATCH] Forward and back works without rerendering --- frontend/src/contexts/ToolWorkflowContext.tsx | 2 +- frontend/src/hooks/useUrlSync.ts | 4 ++-- frontend/src/utils/urlRouting.ts | 16 ++++++++++------ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/frontend/src/contexts/ToolWorkflowContext.tsx b/frontend/src/contexts/ToolWorkflowContext.tsx index f5fc1502e..aeddf9756 100644 --- a/frontend/src/contexts/ToolWorkflowContext.tsx +++ b/frontend/src/contexts/ToolWorkflowContext.tsx @@ -227,7 +227,7 @@ export function ToolWorkflowProvider({ children }: ToolWorkflowProviderProps) { useNavigationUrlSync( navigationState.selectedTool, handleToolSelect, - () => actions.setSelectedTool(null), + handleBackToTools, toolRegistry, true ); diff --git a/frontend/src/hooks/useUrlSync.ts b/frontend/src/hooks/useUrlSync.ts index a0366cebd..55e633076 100644 --- a/frontend/src/hooks/useUrlSync.ts +++ b/frontend/src/hooks/useUrlSync.ts @@ -38,11 +38,11 @@ export function useNavigationUrlSync( if (!enableSync) return; if (selectedTool) { - updateToolRoute(selectedTool, registry); + updateToolRoute(selectedTool, registry, false); // Use pushState for user navigation } else { // Only clear URL if we're not on the home page already if (window.location.pathname !== '/') { - clearToolRoute(); + clearToolRoute(false); // Use pushState for user navigation } } }, [selectedTool, registry, enableSync]); diff --git a/frontend/src/utils/urlRouting.ts b/frontend/src/utils/urlRouting.ts index 592aaab0d..a2957ef27 100644 --- a/frontend/src/utils/urlRouting.ts +++ b/frontend/src/utils/urlRouting.ts @@ -48,14 +48,18 @@ export function parseToolRoute(registry: ToolRegistry): ToolRoute { /** * Update URL and fire analytics pixel */ -function updateUrl(newPath: string, searchParams: URLSearchParams): void { +function updateUrl(newPath: string, searchParams: URLSearchParams, replace: boolean = false): void { const currentPath = window.location.pathname; const queryString = searchParams.toString(); const fullUrl = newPath + (queryString ? `?${queryString}` : ''); // Only update URL and fire pixel if something actually changed if (currentPath !== newPath || window.location.search !== (queryString ? `?${queryString}` : '')) { - window.history.replaceState(null, '', fullUrl); + if (replace) { + window.history.replaceState(null, '', fullUrl); + } else { + window.history.pushState(null, '', fullUrl); + } firePixel(newPath); } } @@ -63,7 +67,7 @@ function updateUrl(newPath: string, searchParams: URLSearchParams): void { /** * Update the URL to reflect the current tool selection */ -export function updateToolRoute(toolId: ToolId, registry: ToolRegistry): void { +export function updateToolRoute(toolId: ToolId, registry: ToolRegistry, replace: boolean = false): void { const tool = registry[toolId]; if (!tool) { console.warn(`Tool ${toolId} not found in registry`); @@ -76,17 +80,17 @@ export function updateToolRoute(toolId: ToolId, registry: ToolRegistry): void { // Remove tool query parameter since we're using path-based routing searchParams.delete('tool'); - updateUrl(newPath, searchParams); + updateUrl(newPath, searchParams, replace); } /** * Clear tool routing and return to home page */ -export function clearToolRoute(): void { +export function clearToolRoute(replace: boolean = false): void { const searchParams = new URLSearchParams(window.location.search); searchParams.delete('tool'); - updateUrl('/', searchParams); + updateUrl('/', searchParams, replace); } /**