Stirling-PDF/frontend/src/core/hooks/useToolNavigation.ts
James Brunton d2b38ef4b8
Restructure frontend code to allow for extensions (#4721)
# Description of Changes
Move frontend code into `core` folder and add infrastructure for
`proprietary` folder to include premium, non-OSS features
2025-10-28 10:29:36 +00:00

47 lines
1.6 KiB
TypeScript

import { useCallback } from 'react';
import { ToolRegistryEntry, getToolUrlPath } from '@app/data/toolsTaxonomy';
import { useToolWorkflow } from '@app/contexts/ToolWorkflowContext';
import { handleUnlessSpecialClick } from '@app/utils/clickHandlers';
import { ToolId } from '@app/types/toolId';
export interface ToolNavigationProps {
/** Full URL for the tool (for href attribute) */
href: string;
/** Click handler that maintains SPA behavior */
onClick: (e: React.MouseEvent) => void;
}
/**
* Hook that provides URL and navigation handlers for tools
* Enables right-click "Open in New Tab" while maintaining SPA behavior for regular clicks
*/
export function useToolNavigation(): {
getToolNavigation: (toolId: string, tool: ToolRegistryEntry) => ToolNavigationProps;
} {
const { handleToolSelect } = useToolWorkflow();
const getToolNavigation = useCallback((toolId: string, tool: ToolRegistryEntry): ToolNavigationProps => {
// Generate SSR-safe relative path
const path = getToolUrlPath(toolId);
const href = path; // Relative path, no window.location needed
// Click handler that maintains SPA behavior
const onClick = (e: React.MouseEvent) => {
handleUnlessSpecialClick(e, () => {
// Handle external links normally
if (tool.link) {
window.open(tool.link, '_blank', 'noopener,noreferrer');
return;
}
// Use SPA navigation for internal tools
handleToolSelect(toolId as ToolId);
});
};
return { href, onClick };
}, [handleToolSelect]);
return { getToolNavigation };
}