mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
26 lines
715 B
TypeScript
26 lines
715 B
TypeScript
import { compile } from 'path-to-regexp';
|
|
import { useCallback } from 'react';
|
|
import { useRouteMap } from './use-route-map';
|
|
|
|
function buildUrl(pattern: string, params: Record<string, string> = {}): string {
|
|
try {
|
|
return compile(pattern)(params);
|
|
} catch (err) {
|
|
console.warn(`[Hydro] Failed to build URL for pattern "${pattern}":`, err);
|
|
return '#';
|
|
}
|
|
}
|
|
|
|
export function useUrl() {
|
|
const routeMap = useRouteMap();
|
|
|
|
return useCallback((name: string, params: Record<string, string> = {}): string => {
|
|
const pattern = routeMap[name];
|
|
if (!pattern) {
|
|
console.warn(`[Hydro] Unknown route: ${name}`);
|
|
return '#';
|
|
}
|
|
return buildUrl(pattern, params);
|
|
}, [routeMap]);
|
|
}
|