Skip to content

Commit

Permalink
fix(config): handle query config options (#9824)
Browse files Browse the repository at this point in the history
Refs #9807

---------

Co-authored-by: Vladimír Gorej <vladimir.gorej@gmail.com>
  • Loading branch information
glowcloud and char0n committed Apr 16, 2024
1 parent cd7fb27 commit 6923111
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
35 changes: 26 additions & 9 deletions src/core/config/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,49 @@
* 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
*/
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
}

Expand Down
12 changes: 11 additions & 1 deletion src/core/config/sources/query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @prettier
*/
import set from "lodash/set"
import { parseSearch } from "core/utils"

/**
Expand All @@ -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
2 changes: 1 addition & 1 deletion src/standalone/plugins/top-bar/components/TopBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 6923111

Please sign in to comment.