From cf0000fb8073e4379514f67bd2db68195546f7d6 Mon Sep 17 00:00:00 2001 From: Simon Cordt Date: Sat, 27 Sep 2025 09:02:24 +0200 Subject: [PATCH] Refactor: Centralize History Row comparison function --- web/js/pqueue.ui.core.js | 11 +++++++++++ web/js/pqueue.ui.history.js | 14 ++------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/web/js/pqueue.ui.core.js b/web/js/pqueue.ui.core.js index 716ce83..662ba56 100644 --- a/web/js/pqueue.ui.core.js +++ b/web/js/pqueue.ui.core.js @@ -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; diff --git a/web/js/pqueue.ui.history.js b/web/js/pqueue.ui.history.js index f5ff27b..f85543c 100644 --- a/web/js/pqueue.ui.history.js +++ b/web/js/pqueue.ui.history.js @@ -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) {