@etsoo/toolpad
Version:
Dashboard framework extention based on Toolpad Core
38 lines (37 loc) • 1.41 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import * as React from "react";
import { useRouter } from "next/router";
import { asArray } from "../utils/collections";
import { AppProvider } from "../AppProvider/AppProviderComponent";
/**
* @ignore - internal component.
*/
export function AppProviderNextPages(props) {
const { push, replace, asPath, query } = useRouter();
const search = React.useMemo(() => {
const params = new URLSearchParams();
Object.entries(query ?? {}).forEach(([key, value]) => {
asArray(value ?? []).forEach((v) => {
params.append(key, v.toString());
});
});
return params.toString();
}, [query]);
// Stable search params object
const searchParams = React.useMemo(() => new URLSearchParams(search), [search]);
const navigate = React.useCallback((url, { history = "auto" } = {}) => {
if (history === "auto" || history === "push") {
return push(String(url));
}
if (history === "replace") {
return replace(String(url));
}
throw new Error(`Invalid history option: ${history}`);
}, [push, replace]);
const routerImpl = React.useMemo(() => ({
pathname: asPath,
searchParams,
navigate
}), [asPath, navigate, searchParams]);
return _jsx(AppProvider, { router: routerImpl, ...props });
}