ant-design/.dumi/hooks/useLocation.ts
lijianan ea8ed28209
chore: unified import method (#42149)
* chore: unified import method

* fix lint
2023-05-05 20:52:44 +08:00

48 lines
1.0 KiB
TypeScript

import { useLocation as useDumiLocation } from 'dumi';
import 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,
};
}