Handle no info.downloads use case for custom catalogues

pull/5108/head
Joe Pavitt 2025-04-15 17:11:25 +01:00
parent ea25406d00
commit a2cb730a52
1 changed files with 18 additions and 2 deletions

View File

@ -597,10 +597,26 @@ RED.palette.editor = (function() {
}
}
function sortModulesRelevance(A,B) {
return sortModulesDownloads(A,B);
const defaultSort = sortModulesDownloads(A,B);
const currentFilter = searchInput.searchBox('value').trim();
if (defaultSort === 0) {
// same number of downloads
if (currentFilter === "") {
return sortModulesAZ(A,B);
}
var i = A.info.index.indexOf(currentFilter) - B.info.index.indexOf(currentFilter);
if (i === 0) {
return sortModulesAZ(A,B);
}
return i;
}
return defaultSort;
}
function sortModulesDownloads(A,B) {
return B.info.downloads.week - A.info.downloads.week;
// check A has the info.downloads property - if not exising, prioritise it (likely a custom user catalogue)
const a = A.info && typeof A.info.downloads !== 'undefined' ? A.info.downloads.week : Number.MAX_SAFE_INTEGER;
const b = B.info && typeof B.info.downloads !== 'undefined' ? B.info.downloads.week : Number.MAX_SAFE_INTEGER;
return b - a;
}
function sortModulesAZ(A,B) {
return A.info.id.localeCompare(B.info.id);