Files
enterprise-addons/knowledge/static/src/portal_webclient/router_utils.js
T

40 lines
1.3 KiB
JavaScript

import { parseSearchQuery, PATH_KEYS } from "@web/core/browser/router";
import { omit } from "@web/core/utils/objects";
import { isNumeric } from "@web/core/utils/strings";
import { objectToUrlEncodedString } from "@web/core/utils/urls";
// Prefixes should be sorted by desc. length.
export const PREFIXES = ["/knowledge/article", "/knowledge/home"];
/**
* @param {{ [key: string]: any }} state
* @returns {string}
*/
export function stateToUrl(state) {
let pathname;
if (!state.resId) {
pathname = "/knowledge/home";
} else {
pathname = `/knowledge/article/${state.resId}`;
}
const search = objectToUrlEncodedString(omit(state, "actionStack", ...PATH_KEYS));
return `${pathname}${search ? `?${search}` : ""}`;
}
/**
* @param {URL} urlObj
* @returns {{ [key: string]: any }}
*/
export function urlToState(urlObj) {
const { pathname, search } = urlObj;
const state = parseSearchQuery(search);
const prefix = PREFIXES.find((prefix) => pathname.startsWith(prefix));
if (prefix === "/knowledge/article") {
const splitPath = pathname.replace(prefix, "").split("/").toSpliced(0, 1);
if (isNumeric(splitPath.at(0))) {
state.resId = parseInt(splitPath.at(0));
}
}
return state;
}