diff --git a/extension/src/plots/paths/collect.ts b/extension/src/plots/paths/collect.ts index e75a46bd8a..65891cf6e2 100644 --- a/extension/src/plots/paths/collect.ts +++ b/extension/src/plots/paths/collect.ts @@ -51,19 +51,10 @@ const collectType = (plots: Plot[]) => { ? type.add(PathType.TEMPLATE_MULTI) : type.add(PathType.TEMPLATE_SINGLE) } - return type } -const getType = ( - data: PlotsData, - hasChildren: boolean, - path: string -): Set | undefined => { - if (hasChildren) { - return - } - +const getType = (data: PlotsData, path: string): Set | undefined => { const plots = data[path] if (!definedAndNonEmpty(plots)) { return @@ -118,6 +109,46 @@ const collectPathRevisions = (data: PlotsData, path: string): Set => { return revisions } +const collectPlotPathType = ( + plotPath: PlotPath, + data: PlotsData, + hasChildren: boolean, + path: string +) => { + if (hasChildren) { + return + } + + const type = getType(data, path) + + if (type) { + plotPath.type = type + } +} + +const updateExistingPlotPath = ( + acc: PlotPath[], + data: PlotsData, + hasChildren: boolean, + revisions: Set, + path: string +) => + acc.map(existing => { + const plotPath = { ...existing } + + if (existing.path !== path) { + return plotPath + } + + plotPath.revisions = new Set([...existing.revisions, ...revisions]) + + if (!plotPath.type) { + collectPlotPathType(plotPath, data, hasChildren, path) + } + + return plotPath + }) + const collectOrderedPath = ( acc: PlotPath[], data: PlotsData, @@ -126,20 +157,12 @@ const collectOrderedPath = ( idx: number ): PlotPath[] => { const path = getPath(pathArray, idx) + const hasChildren = idx !== pathArray.length if (acc.some(({ path: existingPath }) => existingPath === path)) { - return acc.map(existing => - existing.path === path - ? { - ...existing, - revisions: new Set([...existing.revisions, ...revisions]) - } - : existing - ) + return updateExistingPlotPath(acc, data, hasChildren, revisions, path) } - const hasChildren = idx !== pathArray.length - const plotPath: PlotPath = { hasChildren, parentPath: getParent(pathArray, idx), @@ -147,10 +170,7 @@ const collectOrderedPath = ( revisions } - const type = getType(data, hasChildren, path) - if (type) { - plotPath.type = type - } + collectPlotPathType(plotPath, data, hasChildren, path) acc.push(plotPath) return acc @@ -228,7 +248,6 @@ export const collectPaths = ( if (errors?.length) { acc = collectErrorPaths(acc, data, errors) } - return acc }