mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-17 22:46:48 +01:00
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import { useNavigate } from 'react-router-dom';
|
|
|
|
export type RouteParamsWorkspace = {
|
|
workspaceId: string;
|
|
};
|
|
|
|
export type RouteParamsRequest = RouteParamsWorkspace & {
|
|
requestId: string;
|
|
};
|
|
|
|
export const routePaths = {
|
|
workspaces() {
|
|
return '/workspaces';
|
|
},
|
|
workspace({ workspaceId } = { workspaceId: ':workspaceId' } as RouteParamsWorkspace) {
|
|
return `/workspaces/${workspaceId}`;
|
|
},
|
|
request(
|
|
{ workspaceId, requestId } = {
|
|
workspaceId: ':workspaceId',
|
|
requestId: ':requestId',
|
|
} as RouteParamsRequest,
|
|
) {
|
|
return `${this.workspace({ workspaceId })}/requests/${requestId}`;
|
|
},
|
|
};
|
|
|
|
export function useRoutes() {
|
|
const navigate = useNavigate();
|
|
return {
|
|
navigate<T extends keyof typeof routePaths>(
|
|
path: T,
|
|
...params: Parameters<(typeof routePaths)[T]>
|
|
) {
|
|
// Not sure how to make TS work here, but it's good from the
|
|
// outside caller perspective.
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const resolvedPath = routePaths[path](...(params as any));
|
|
navigate(resolvedPath);
|
|
},
|
|
paths: routePaths,
|
|
};
|
|
}
|