Files
Stirling-PDF/frontend/src/hooks/useUrlSync.ts
T
Anthony StirlingandGitHub 166f6d2d98 path (#4488)
# Description of Changes

<!--
Please provide a summary of the changes, including:

- What was changed
- Why the change was made
- Any challenges encountered

Closes #(issue_number)
-->

---

## Checklist

### General

- [ ] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [ ] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md)
(if applicable)
- [ ] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md)
(if applicable)
- [ ] I have performed a self-review of my own code
- [ ] My changes generate no new warnings

### Documentation

- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)

### UI Changes (if applicable)

- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)

### Testing (if applicable)

- [ ] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing)
for more details.
2025-09-24 20:37:51 +01:00

126 lines
3.7 KiB
TypeScript

/**
* URL synchronization hooks for tool routing with registry support
*/
import { useEffect, useCallback, useRef } from 'react';
import { ToolId } from '../types/toolId';
import { parseToolRoute, updateToolRoute, clearToolRoute } from '../utils/urlRouting';
import { ToolRegistry } from '../data/toolsTaxonomy';
import { firePixel } from '../utils/scarfTracking';
import { withBasePath } from '../constants/app';
/**
* Hook to sync workbench and tool with URL using registry
*/
export function useNavigationUrlSync(
selectedTool: ToolId | null,
handleToolSelect: (toolId: string) => void,
clearToolSelection: () => void,
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) {
handleToolSelect(route.toolId);
} else if (selectedTool !== null) {
// Only clear selection if we actually had a tool selected
// Don't clear on initial load when selectedTool starts as null
clearToolSelection();
}
}
hasInitialized.current = true;
}, []); // Only run on mount
// Update URL when tool or workbench changes
useEffect(() => {
if (!enableSync) return;
if (selectedTool) {
updateToolRoute(selectedTool, registry, false); // Use pushState for user navigation
} 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
const homePath = withBasePath('/');
if (window.location.pathname !== homePath) {
clearToolRoute(false); // Use pushState for user navigation
}
}
prevSelectedTool.current = selectedTool;
}, [selectedTool, registry, enableSync]);
// Handle browser back/forward navigation
useEffect(() => {
if (!enableSync) return;
const handlePopState = () => {
const route = parseToolRoute(registry);
if (route.toolId !== selectedTool) {
// Fire pixel for back/forward navigation
const currentPath = window.location.pathname;
firePixel(currentPath);
if (route.toolId) {
handleToolSelect(route.toolId);
} else {
clearToolSelection();
}
}
};
window.addEventListener('popstate', handlePopState);
return () => window.removeEventListener('popstate', handlePopState);
}, [selectedTool, handleToolSelect, clearToolSelection, registry, enableSync]);
}
/**
* Hook to programmatically navigate to tools with registry support
*/
export function useToolNavigation(registry: ToolRegistry) {
const navigateToTool = useCallback((toolId: ToolId) => {
updateToolRoute(toolId, registry);
// Dispatch a custom event to notify other components
window.dispatchEvent(new CustomEvent('toolNavigation', {
detail: { toolId }
}));
}, [registry]);
const navigateToHome = useCallback(() => {
clearToolRoute();
// Dispatch a custom event to notify other components
window.dispatchEvent(new CustomEvent('toolNavigation', {
detail: { toolId: null }
}));
}, []);
return {
navigateToTool,
navigateToHome
};
}
/**
* Hook to get current URL route information with registry support
*/
export function useCurrentRoute(registry: ToolRegistry) {
const getCurrentRoute = useCallback(() => {
return parseToolRoute(registry);
}, [registry]);
return getCurrentRoute;
}