umami/hooks/usePageQuery.js

33 lines
718 B
JavaScript
Raw Normal View History

2020-09-26 07:31:18 +02:00
import { useMemo } from 'react';
import { useRouter } from 'next/router';
import { getQueryString } from '../lib/url';
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 });
return `${pathname}${search}`;
}
return { pathname, query, resolve, router };
}