mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
38 lines
810 B
JavaScript
38 lines
810 B
JavaScript
import { useMemo } from 'react';
|
|
import { useRouter } from 'next/router';
|
|
|
|
function getQueryString(params) {
|
|
return new URLSearchParams({ ...params }).toString();
|
|
}
|
|
|
|
export default function usePageQuery() {
|
|
const router = useRouter();
|
|
const { pathname, search } = location;
|
|
|
|
const query = useMemo(() => {
|
|
if (!search) {
|
|
return {};
|
|
}
|
|
|
|
const params = search.substring(1).split('&');
|
|
|
|
return params.reduce((obj, item) => {
|
|
const [key, value] = item.split('=');
|
|
|
|
obj[key] = decodeURIComponent(value);
|
|
|
|
return obj;
|
|
}, {});
|
|
}, [search]);
|
|
|
|
function resolve(params) {
|
|
const search = getQueryString({ ...query, ...params });
|
|
|
|
const { asPath } = router;
|
|
|
|
return `${asPath.split('?')[0]}${search}`;
|
|
}
|
|
|
|
return { pathname, query, resolve, router };
|
|
}
|