@@ -40,6 +54,10 @@ const ResultViewList = ({
}}
/>
)}
+
+
+
+
-
diff --git a/src/utils/preserveTypesEncoder.js b/src/utils/preserveTypesEncoder.js
new file mode 100644
index 00000000..6d32a664
--- /dev/null
+++ b/src/utils/preserveTypesEncoder.js
@@ -0,0 +1,43 @@
+// From https://github.com/elastic/search-ui/blob/main/packages/search-ui/src/preserveTypesEncoder.ts
+// License: Apache-2.0
+
+function isTypeNumber(value) {
+ return value !== undefined && value !== null && typeof value === "number";
+}
+
+function isTypeBoolean(value) {
+ return value && typeof value === "boolean";
+}
+
+function toBoolean(value) {
+ if (value === "true") return true;
+ if (value === "false") return false;
+ throw "Invalid type parsed as Boolean value";
+}
+
+/* Encoder for qs library which preserve number types on the URL. Numbers
+ are padded with "n_{number}_n", and booleans with "b_{boolean}_b"*/
+
+export default {
+ encode(value, encode) {
+ if (isTypeNumber(value)) {
+ return `n_${value}_n`;
+ }
+ if (isTypeBoolean(value)) {
+ return `b_${value}_b`;
+ }
+ return encode(value);
+ },
+ decode(value, decode) {
+ //eslint-disable-next-line
+ if (/n_-?[\d\.]*_n/.test(value)) {
+ const numericValueString = value.substring(2, value.length - 2);
+ return Number(numericValueString);
+ }
+ if (/^b_(true|false)*_b$/.test(value)) {
+ const booleanValueString = value.substring(2, value.length - 2);
+ return toBoolean(booleanValueString);
+ }
+ return decode(value);
+ },
+};
diff --git a/src/utils/useNextRouting.ts b/src/utils/useNextRouting.ts
new file mode 100644
index 00000000..174f6414
--- /dev/null
+++ b/src/utils/useNextRouting.ts
@@ -0,0 +1,82 @@
+import {
+ Filter,
+ RequestState,
+ SearchDriverOptions,
+ SortOption,
+} from "@elastic/search-ui";
+import { usePathname, useRouter } from "next/navigation";
+var qs = require("qs");
+import preserveTypesEncoder from "./preserveTypesEncoder";
+
+export const useNextRouting = (
+ config: SearchDriverOptions,
+ basePathUrl: string,
+) => {
+ const router = useRouter();
+ const pathName = usePathname();
+
+ // From https://github.com/elastic/search-ui/blob/6583ad0c03056b3df0541d585337dfad12c16272/packages/search-ui/src/URLManager.ts#L9
+ type QueryParams = {
+ filters?: Filter[];
+ current?: number;
+ q?: string;
+ size?: number;
+ "sort-field"?: string;
+ "sort-direction"?: string;
+ sort?: SortOption[];
+ };
+
+ // From https://github.com/elastic/search-ui/blob/6583ad0c03056b3df0541d585337dfad12c16272/packages/search-ui/src/URLManager.ts#L84
+ function stateToParams({
+ searchTerm,
+ current,
+ filters,
+ resultsPerPage,
+ sortDirection,
+ sortField,
+ sortList,
+ }: RequestState): QueryParams {
+ const params: QueryParams = {};
+ if (current !== undefined && current > 1) params.current = current;
+ if (searchTerm) params.q = searchTerm;
+ if (resultsPerPage) params.size = resultsPerPage;
+ if (filters && filters.length > 0) {
+ params["filters"] = filters;
+ }
+ if (sortList && sortList.length > 0) {
+ params["sort"] = sortList;
+ } else if (sortField) {
+ params["sort-field"] = sortField;
+ params["sort-direction"] = sortDirection;
+ }
+ return params;
+ }
+
+ // From https://github.com/elastic/search-ui/blob/6583ad0c03056b3df0541d585337dfad12c16272/packages/search-ui/src/URLManager.ts#L109
+ function stateToQueryString(state: RequestState): string {
+ return qs.stringify(stateToParams(state), {
+ encoder: preserveTypesEncoder.encode,
+ });
+ }
+
+ const routingOptions = {
+ stateToUrl: (state: RequestState) => {
+ // if URL contains ?uid=... then we are in modal view, and hence we should not update the URL
+ if (window.location.search.includes("uid=")) {
+ console.log(
+ "stateToUrl :: in modal view, not updating URL. URL: ",
+ window.location.href,
+ );
+ return window.location.search.replace("?", ""); // get the query string without the ?, eg. uid=123
+ } else {
+ console.log("stateToUrl :: updating URL with state: ", state);
+ return stateToQueryString(state);
+ }
+ },
+ };
+
+ return {
+ ...config,
+ routingOptions,
+ };
+};