25 lines
702 B
TypeScript
25 lines
702 B
TypeScript
import { createContext, useCallback, useContext } from "react";
|
|
import { useNavigate, type NavigateOptions } from "react-router-dom";
|
|
|
|
export const RouteBaseContext = createContext("/page/workspace");
|
|
export const RouteBaseProvider = RouteBaseContext.Provider;
|
|
|
|
export function useRouteBase() {
|
|
return useContext(RouteBaseContext);
|
|
}
|
|
|
|
export function useRouteNavigate() {
|
|
const navigate = useNavigate();
|
|
const base = useRouteBase();
|
|
return useCallback(
|
|
(path: string | number, opts?: NavigateOptions) => {
|
|
if (typeof path === "string") {
|
|
navigate(path.replace("/page/workspace", base), opts);
|
|
} else {
|
|
navigate(path);
|
|
}
|
|
},
|
|
[navigate, base],
|
|
);
|
|
}
|