Refactor: Centralize History Row comparison function

This commit is contained in:
Simon Cordt
2025-09-27 09:02:24 +02:00
parent 9b75f41ffd
commit cf0000fb80
2 changed files with 13 additions and 12 deletions
+11
View File
@@ -162,6 +162,17 @@
return [ms, id];
};
UI.compareHistoryRows = function compareHistoryRows(a, b, dir = 'desc') {
try {
const [ams, aid] = UI.historyKeyParts(a);
const [bms, bid] = UI.historyKeyParts(b);
if (ams !== bms) return dir === 'desc' ? (bms - ams) : (ams - bms);
return dir === 'desc' ? (bid - aid) : (aid - bid);
} catch (err) {
return 0;
}
};
UI.getScrollContainer = function getScrollContainer() {
try {
if (state?.dom?.scrollContainer && document.body.contains(state.dom.scrollContainer)) return state.dom.scrollContainer;
+2 -12
View File
@@ -330,12 +330,7 @@
let list = Array.isArray(paged?.history) ? paged.history : [];
try {
const dir = (state.historyPaging?.params?.sort_dir === 'asc') ? 'asc' : 'desc';
list = list.slice().sort((a, b) => {
const [ams, aid] = UI.historyKeyParts(a);
const [bms, bid] = UI.historyKeyParts(b);
if (ams !== bms) return dir === 'desc' ? (bms - ams) : (ams - bms);
return dir === 'desc' ? (bid - aid) : (aid - bid);
});
list = list.slice().sort((a, b) => UI.compareHistoryRows(a, b, dir));
} catch (err) { /* noop */ }
if (!state.historyIds) state.historyIds = new Set();
UI.withStableAnchor(() => {
@@ -392,12 +387,7 @@
const existingIds = new Set(Array.from(grid.querySelectorAll('.pqueue-history-card')).map((n) => n.getAttribute('data-id')));
const ordered = Array.isArray(state.history) ? state.history.slice() : [];
try {
ordered.sort((a, b) => {
const [ams, aid] = UI.historyKeyParts(a);
const [bms, bid] = UI.historyKeyParts(b);
if (ams !== bms) return dir === 'desc' ? (bms - ams) : (ams - bms);
return dir === 'desc' ? (bid - aid) : (aid - bid);
});
ordered.sort((a, b) => UI.compareHistoryRows(a, b, dir));
} catch (err) { /* noop */ }
for (const row of ordered) {