Files
docmost/apps/client/src/hooks/use-match-path.tsx
T
2024-01-09 18:58:26 +01:00

17 lines
394 B
TypeScript

import { useLocation } from 'react-router-dom';
export const useMatchPath = () => {
const location = useLocation();
const matchPath = (pattern) => {
const modifiedPattern = pattern
.replace(/:([^/]+)/g, '([^/]+)(?:/.*)?')
.replace(/\//g, '\\/');
const regex = new RegExp(`^${modifiedPattern}$`);
return regex.test(location.pathname);
};
return matchPath;
};