« get me outta code hell

content: generateAlbumSidebarGroupBox: match group pages' sorts - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-01-14 16:12:33 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-01-14 16:35:05 -0400
commit872cee8edbfdd1678b826c912c8e37829e5c87a0 (patch)
tree2f8e557204cb90673e2ba988d7ca757115191675
parentf641a620392c069b516e0b02491f21f007a03dea (diff)
content: generateAlbumSidebarGroupBox: match group pages' sorts
-rw-r--r--src/content/dependencies/generateAlbumSidebarGroupBox.js54
1 files changed, 37 insertions, 17 deletions
diff --git a/src/content/dependencies/generateAlbumSidebarGroupBox.js b/src/content/dependencies/generateAlbumSidebarGroupBox.js
index f3705450..9e1b4cde 100644
--- a/src/content/dependencies/generateAlbumSidebarGroupBox.js
+++ b/src/content/dependencies/generateAlbumSidebarGroupBox.js
@@ -1,4 +1,5 @@
-import {empty} from '#sugar';
+import {atOffset, empty} from '#sugar';
+import {sortChronologically} from '#wiki-data';
 
 export default {
   contentDependencies: [
@@ -10,7 +11,33 @@ export default {
 
   extraDependencies: ['html', 'language'],
 
-  relations(relation, album, group) {
+  query(album, group) {
+    const query = {};
+
+    if (album.date) {
+      const albums =
+        group.albums.filter(album => album.date);
+
+      // Sort by latest first. This matches the sorting order used on group
+      // gallery pages, ensuring that previous/next matches moving up/down
+      // the gallery. Note that this makes the index offsets "backwards"
+      // compared to how latest-last chronological lists are accessed.
+      sortChronologically(albums, {latestFirst: true});
+
+      const index =
+        albums.indexOf(album);
+
+      query.previousAlbum =
+        atOffset(albums, index, +1);
+
+      query.nextAlbum =
+        atOffset(albums, index, -1);
+    }
+
+    return query;
+  },
+
+  relations(relation, query, album, group) {
     const relations = {};
 
     relations.groupLink =
@@ -25,21 +52,14 @@ export default {
         relation('transformContent', group.descriptionShort);
     }
 
-    if (album.date) {
-      const albums = group.albums.filter(album => album.date);
-      const index = albums.indexOf(album);
-      const previousAlbum = (index > 0) && albums[index - 1];
-      const nextAlbum = (index < albums.length - 1) && albums[index + 1];
-
-      if (previousAlbum) {
-        relations.previousAlbumLink =
-          relation('linkAlbum', previousAlbum);
-      }
-
-      if (nextAlbum) {
-        relations.nextAlbumLink =
-          relation('linkAlbum', nextAlbum);
-      }
+    if (query.previousAlbum) {
+      relations.previousAlbumLink =
+        relation('linkAlbum', query.previousAlbum);
+    }
+
+    if (query.nextAlbum) {
+      relations.nextAlbumLink =
+        relation('linkAlbum', query.nextAlbum);
     }
 
     return relations;