mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-15 17:19:11 +08:00
48 lines
1.0 KiB
TypeScript
48 lines
1.0 KiB
TypeScript
|
import { useLocation as useDumiLocation } from 'dumi';
|
||
|
import * as React from 'react';
|
||
|
import useLocale from './useLocale';
|
||
|
|
||
|
function clearPath(path: string) {
|
||
|
return path.replace('-cn', '').replace(/\/$/, '');
|
||
|
}
|
||
|
|
||
|
export default function useLocation() {
|
||
|
const location = useDumiLocation();
|
||
|
const { search } = location;
|
||
|
const [, localeType] = useLocale();
|
||
|
|
||
|
const getLink = React.useCallback(
|
||
|
(path: string, hash?: string | { cn: string; en: string }) => {
|
||
|
let pathname = clearPath(path);
|
||
|
|
||
|
if (localeType === 'cn') {
|
||
|
pathname = `${pathname}-cn`;
|
||
|
}
|
||
|
|
||
|
if (search) {
|
||
|
pathname = `${pathname}${search}`;
|
||
|
}
|
||
|
|
||
|
if (hash) {
|
||
|
let hashStr: string;
|
||
|
if (typeof hash === 'object') {
|
||
|
hashStr = hash[localeType];
|
||
|
} else {
|
||
|
hashStr = hash;
|
||
|
}
|
||
|
|
||
|
pathname = `${pathname}#${hashStr}`;
|
||
|
}
|
||
|
|
||
|
return pathname;
|
||
|
},
|
||
|
[localeType, search],
|
||
|
);
|
||
|
|
||
|
return {
|
||
|
...location,
|
||
|
pathname: clearPath(location.pathname),
|
||
|
getLink,
|
||
|
};
|
||
|
}
|