첫 커밋
This commit is contained in:
37
src/shared/lib/hooks/use-navigate.ts
Normal file
37
src/shared/lib/hooks/use-navigate.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { PathType } from '@/shared/constants/paths';
|
||||
import {
|
||||
NavigateOptions,
|
||||
To,
|
||||
useNavigate as _useNavigate
|
||||
} from 'react-router';
|
||||
|
||||
export type NavigateTo = PathType | -1 | 0;
|
||||
|
||||
export const goBackWebview = (goBack: () => void) => {
|
||||
if (!window.ReactNativeWebView) {
|
||||
if (window.history.state?.idx > 0) {
|
||||
goBack();
|
||||
return;
|
||||
} else {
|
||||
window.close();
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
export const useNavigate = () => {
|
||||
const _navigate = _useNavigate();
|
||||
|
||||
const navigate = (to: NavigateTo, options?: NavigateOptions): void => {
|
||||
const path = typeof to === 'number' ? to : to.toString();
|
||||
_navigate(path as To, options);
|
||||
};
|
||||
|
||||
const reload = async () => {
|
||||
navigate(0);
|
||||
};
|
||||
|
||||
const navigateBack = () => goBackWebview(() => _navigate(-1));
|
||||
|
||||
return { navigate, navigateBack, reload };
|
||||
};
|
||||
Reference in New Issue
Block a user