1
0
mirror of https://github.com/kremalicious/umami.git synced 2024-12-23 18:04:47 +01:00
umami/hooks/usePageQuery.js

35 lines
759 B
JavaScript
Raw Normal View History

2020-09-26 07:31:18 +02:00
import { useMemo } from 'react';
import { useRouter } from 'next/router';
2020-11-10 06:01:53 +01:00
import { getQueryString } from 'lib/url';
2020-09-26 07:31:18 +02:00
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 });
2020-11-10 06:01:53 +01:00
const { asPath } = router;
return `${asPath.split('?')[0]}${search}`;
2020-09-26 07:31:18 +02:00
}
return { pathname, query, resolve, router };
}