Refactored to use app folder.

This commit is contained in:
Mike Cao 2023-09-29 05:29:22 -07:00
parent 40cfcd41e9
commit 9a52cdd2e1
258 changed files with 2025 additions and 2258 deletions

View file

@ -1,30 +1,24 @@
import { useMemo } from 'react';
import { useRouter } from 'next/router';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import { buildUrl } from 'next-basics';
export function usePageQuery() {
const router = useRouter();
const { pathname, search } = location;
const { asPath } = router;
const pathname = usePathname();
const params = useSearchParams();
const query = useMemo(() => {
if (!search) {
return {};
const obj = {};
for (const [key, value] of params.entries()) {
obj[key] = decodeURIComponent(value);
}
const params = search.substring(1).split('&');
return params.reduce((obj, item) => {
const [key, value] = item.split('=');
obj[key] = decodeURIComponent(value);
return obj;
}, {});
}, [search]);
return obj;
}, [params]);
function resolveUrl(params, reset) {
return buildUrl(asPath.split('?')[0], { ...(reset ? {} : query), ...params });
return buildUrl(pathname, { ...(reset ? {} : query) });
}
return { pathname, query, resolveUrl, router };