diff --git a/packages/bruno-app/src/utils/common/index.js b/packages/bruno-app/src/utils/common/index.js index 6402d5a1e..1c647ae28 100644 --- a/packages/bruno-app/src/utils/common/index.js +++ b/packages/bruno-app/src/utils/common/index.js @@ -242,7 +242,7 @@ export const sortByNameThenSequence = items => { // Check if there's already an item with the same sequence number const hasItemWithSameSeq = Array.isArray(existingItem) - ? existingItem[0].seq === item.seq + ? existingItem?.[0]?.seq === item.seq : existingItem?.seq === item.seq; if (hasItemWithSameSeq) { diff --git a/packages/bruno-cli/src/utils/collection.js b/packages/bruno-cli/src/utils/collection.js index 4b698ced9..09d78506c 100644 --- a/packages/bruno-cli/src/utils/collection.js +++ b/packages/bruno-cli/src/utils/collection.js @@ -55,7 +55,7 @@ const createCollectionJsonFromPathname = (collectionPath) => { } } let currentDirFolderItems = currentDirItems?.filter((iter) => iter.type === 'folder'); - let sortedFolderItems = currentDirFolderItems?.sort((a, b) => a.seq - b.seq); + let sortedFolderItems = sortByNameThenSequence(currentDirFolderItems); let currentDirRequestItems = currentDirItems?.filter((iter) => iter.type !== 'folder'); let sortedRequestItems = currentDirRequestItems?.sort((a, b) => a.seq - b.seq); @@ -512,6 +512,48 @@ const processCollectionItems = async (items = [], currentPath) => { } }; +const sortByNameThenSequence = items => { + const isSeqValid = seq => Number.isFinite(seq) && Number.isInteger(seq) && seq > 0; + + // Sort folders alphabetically by name + const alphabeticallySorted = [...items].sort((a, b) => a.name && b.name && a.name.localeCompare(b.name)); + + // Extract folders without 'seq' + const withoutSeq = alphabeticallySorted.filter(f => !isSeqValid(f['seq'])); + + // Extract folders with 'seq' and sort them by 'seq' + const withSeq = alphabeticallySorted.filter(f => isSeqValid(f['seq'])).sort((a, b) => a.seq - b.seq); + + const sortedItems = withoutSeq; + + // Insert folders with 'seq' at their specified positions + withSeq.forEach((item) => { + const position = item.seq - 1; + const existingItem = withoutSeq[position]; + + // Check if there's already an item with the same sequence number + const hasItemWithSameSeq = Array.isArray(existingItem) + ? existingItem?.[0]?.seq === item.seq + : existingItem?.seq === item.seq; + + if (hasItemWithSameSeq) { + // If there's a conflict, group items with same sequence together + const newGroup = Array.isArray(existingItem) + ? [...existingItem, item] + : [existingItem, item]; + + withoutSeq.splice(position, 1, newGroup); + } else { + // Insert item at the specified position + withoutSeq.splice(position, 0, item); + } + }); + + // return flattened sortedItems + return sortedItems.flat(); +}; + + module.exports = { createCollectionJsonFromPathname, mergeHeaders, diff --git a/packages/bruno-electron/src/utils/collection.js b/packages/bruno-electron/src/utils/collection.js index dc330dc97..c7120779e 100644 --- a/packages/bruno-electron/src/utils/collection.js +++ b/packages/bruno-electron/src/utils/collection.js @@ -489,7 +489,7 @@ const sortByNameThenSequence = items => { // Check if there's already an item with the same sequence number const hasItemWithSameSeq = Array.isArray(existingItem) - ? existingItem[0].seq === item.seq + ? existingItem?.[0]?.seq === item.seq : existingItem?.seq === item.seq; if (hasItemWithSameSeq) {