From ae3f847cef317f86fde4e125bd7606681a396045 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 27 Oct 2022 02:19:42 -0700 Subject: [PATCH] Sort layout groups by default, simplify sorting --- src/utils/config/api-response.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/utils/config/api-response.js b/src/utils/config/api-response.js index c1651a783..d3561a553 100644 --- a/src/utils/config/api-response.js +++ b/src/utils/config/api-response.js @@ -77,6 +77,7 @@ export async function servicesResponse() { ]; const mergedGroups = []; + const definedLayouts = initialSettings.layout ? Object.keys(initialSettings.layout) : null; mergedGroupsNames.forEach((groupName) => { const discoveredGroup = discoveredServices.find((group) => group.name === groupName) || { services: [] }; @@ -87,21 +88,14 @@ export async function servicesResponse() { services: [...discoveredGroup.services, ...configuredGroup.services].filter((service) => service), }; - mergedGroups.push(mergedGroup); - }); - - let sortedServices = []; - - const layouts = Object.keys(initialSettings.layout); - layouts.forEach((currentServer) => { - if (initialSettings.layout[currentServer]?.sort) { - const idx = mergedGroups.findIndex((service) => service.name === currentServer); - sortedServices.push(...mergedGroups.splice(idx, 1)); + if (definedLayouts) { + const layoutIndex = definedLayouts.findIndex(layout => layout === mergedGroup.name); + if (layoutIndex > -1) mergedGroups.splice(layoutIndex, 0, mergedGroup); + else mergedGroups.push(mergedGroup); + } else { + mergedGroups.push(mergedGroup); } }); - if (mergedGroups.length) { - sortedServices = sortedServices.concat(mergedGroups); - } - return sortedServices; + return mergedGroups; }