Commit e8059908 authored by tom's avatar tom

fix current route

parent ad89982b
...@@ -4,13 +4,18 @@ import React from 'react'; ...@@ -4,13 +4,18 @@ import React from 'react';
import type { RouteName } from 'lib/link/routes'; import type { RouteName } from 'lib/link/routes';
import { ROUTES } from 'lib/link/routes'; import { ROUTES } from 'lib/link/routes';
const PATH_PARAM_REGEXP = /\/:(\w+)/g;
export default function useCurrentRoute() { export default function useCurrentRoute() {
const { route: nextRoute } = useRouter(); const { route: nextRoute } = useRouter();
return React.useCallback((): RouteName => { return React.useCallback((): RouteName => {
for (const routeName in ROUTES) { for (const routeName in ROUTES) {
const route = ROUTES[routeName as RouteName]; const route = ROUTES[routeName as RouteName];
if (route.pattern === nextRoute) { const formattedRoute = route.pattern.replace(PATH_PARAM_REGEXP, (_, paramName: string) => {
return `/[${ paramName }]`;
});
if (formattedRoute === nextRoute) {
return routeName as RouteName; return routeName as RouteName;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment