52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
/**
|
|
* Copyright (c) 2023-present Plane Software, Inc. and contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
* See the LICENSE file for details.
|
|
*/
|
|
|
|
import { useMemo } from "react";
|
|
import { useLocation, useNavigate, useParams as useParamsRR, useSearchParams as useSearchParamsRR } from "react-router";
|
|
import { ensureTrailingSlash } from "./helper";
|
|
|
|
export function useRouter() {
|
|
const navigate = useNavigate();
|
|
return useMemo(
|
|
() => ({
|
|
push: (to: string) => {
|
|
// Defer navigation to avoid state updates during render
|
|
setTimeout(() => navigate(ensureTrailingSlash(to)), 0);
|
|
},
|
|
replace: (to: string) => {
|
|
// Defer navigation to avoid state updates during render
|
|
setTimeout(() => navigate(ensureTrailingSlash(to), { replace: true }), 0);
|
|
},
|
|
back: () => {
|
|
setTimeout(() => navigate(-1), 0);
|
|
},
|
|
forward: () => {
|
|
setTimeout(() => navigate(1), 0);
|
|
},
|
|
refresh: () => {
|
|
location.reload();
|
|
},
|
|
prefetch: async (_to: string) => {
|
|
// no-op in this shim
|
|
},
|
|
}),
|
|
[navigate]
|
|
);
|
|
}
|
|
|
|
export function usePathname(): string {
|
|
const { pathname } = useLocation();
|
|
return pathname;
|
|
}
|
|
|
|
export function useSearchParams(): URLSearchParams {
|
|
const [searchParams] = useSearchParamsRR();
|
|
return searchParams;
|
|
}
|
|
|
|
export function useParams() {
|
|
return useParamsRR();
|
|
}
|