From 692311105b087971b7b6ba312985db3595a75e83 Mon Sep 17 00:00:00 2001 From: Oliwia Rogala Date: Tue, 16 Apr 2024 09:22:28 +0200 Subject: [PATCH] fix(config): handle query config options (#9824) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs #9807 --------- Co-authored-by: VladimĂ­r Gorej --- src/core/config/merge.js | 35 ++++++++++++++----- src/core/config/sources/query.js | 12 ++++++- .../plugins/top-bar/components/TopBar.jsx | 2 +- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/core/config/merge.js b/src/core/config/merge.js index bcfac221b7a..e56304fb885 100644 --- a/src/core/config/merge.js +++ b/src/core/config/merge.js @@ -8,6 +8,7 @@ * NOTE1: lodash.merge & lodash.mergeWith prefers to ignore undefined values * NOTE2: special handling of `domNode` option is now required as `deep-extend` will corrupt it (lodash.merge handles it correctly) * NOTE3: oauth2RedirectUrl and withCredentials options can be set to undefined. By expecting null instead of undefined, we can't use lodash.merge. + * NOTE4: urls.primaryName needs to handled in special way, because it's an arbitrary property on Array instance * * TODO(vladimir.gorej@gmail.com): remove deep-extend in favor of lodash.merge */ @@ -15,25 +16,41 @@ import deepExtend from "deep-extend" const merge = (target, ...sources) => { let domNode = Symbol.for("domNode") - const sourcesWithoutDomNode = [] + let primaryName = Symbol.for("primaryName") + const sourcesWithoutExceptions = [] for (const source of sources) { - if (Object.hasOwn(source, "domNode")) { - domNode = source.domNode - const sourceWithoutDomNode = { ...source } - delete sourceWithoutDomNode.domNode - sourcesWithoutDomNode.push(sourceWithoutDomNode) - } else { - sourcesWithoutDomNode.push(source) + const sourceWithoutExceptions = { ...source } + + if (Object.hasOwn(sourceWithoutExceptions, "domNode")) { + domNode = sourceWithoutExceptions.domNode + delete sourceWithoutExceptions.domNode + } + + if (Object.hasOwn(sourceWithoutExceptions, "urls.primaryName")) { + primaryName = sourceWithoutExceptions["urls.primaryName"] + delete sourceWithoutExceptions["urls.primaryName"] + } else if ( + Array.isArray(sourceWithoutExceptions.urls) && + Object.hasOwn(sourceWithoutExceptions.urls, "primaryName") + ) { + primaryName = sourceWithoutExceptions.urls.primaryName + delete sourceWithoutExceptions.urls.primaryName } + + sourcesWithoutExceptions.push(sourceWithoutExceptions) } - const merged = deepExtend(target, ...sourcesWithoutDomNode) + const merged = deepExtend(target, ...sourcesWithoutExceptions) if (domNode !== Symbol.for("domNode")) { merged.domNode = domNode } + if (primaryName !== Symbol.for("primaryName") && Array.isArray(merged.urls)) { + merged.urls.primaryName = primaryName + } + return merged } diff --git a/src/core/config/sources/query.js b/src/core/config/sources/query.js index ee1b36f28ad..805c745c194 100644 --- a/src/core/config/sources/query.js +++ b/src/core/config/sources/query.js @@ -1,6 +1,7 @@ /** * @prettier */ +import set from "lodash/set" import { parseSearch } from "core/utils" /** @@ -9,7 +10,16 @@ import { parseSearch } from "core/utils" */ const optionsFromQuery = () => (options) => { - return options.queryConfigEnabled ? parseSearch() : {} + const urlSearchParams = options.queryConfigEnabled ? parseSearch() : {} + + return Object.entries(urlSearchParams).reduce((acc, [key, value]) => { + if (key === "urls.primaryName") { + acc[key] = value + } else { + acc = set(acc, key, value) + } + return acc + }, {}) } export default optionsFromQuery diff --git a/src/standalone/plugins/top-bar/components/TopBar.jsx b/src/standalone/plugins/top-bar/components/TopBar.jsx index 407426a9e0d..1344d347939 100644 --- a/src/standalone/plugins/top-bar/components/TopBar.jsx +++ b/src/standalone/plugins/top-bar/components/TopBar.jsx @@ -87,7 +87,7 @@ class TopBar extends React.Component { if(urls && urls.length) { var targetIndex = this.state.selectedIndex let search = parseSearch() - let primaryName = search["urls.primaryName"] || configs["urls.primaryName"] + let primaryName = search["urls.primaryName"] || configs.urls.primaryName if(primaryName) { urls.forEach((spec, i) => {