« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-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;