« get me outta code hell

content: generateAlbumSecondaryNav: match group pages' sort - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-01-14 16:32:16 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-01-14 16:35:05 -0400
commit40068a42c31bd25ae62f519146513da5f689c1e7 (patch)
treecb7814a46fbd052efca6c6ed1191ccdcc35c044f /src/content/dependencies
parent872cee8edbfdd1678b826c912c8e37829e5c87a0 (diff)
content: generateAlbumSecondaryNav: match group pages' sort
Diffstat (limited to 'src/content/dependencies')
-rw-r--r--src/content/dependencies/generateAlbumSecondaryNav.js76
1 files changed, 45 insertions, 31 deletions
diff --git a/src/content/dependencies/generateAlbumSecondaryNav.js b/src/content/dependencies/generateAlbumSecondaryNav.js
index e574455c..6ffb9351 100644
--- a/src/content/dependencies/generateAlbumSecondaryNav.js
+++ b/src/content/dependencies/generateAlbumSecondaryNav.js
@@ -1,4 +1,5 @@
-import {stitchArrays} from '#sugar';
+import {atOffset, stitchArrays} from '#sugar';
+import {sortChronologically} from '#wiki-data';
 
 export default {
   contentDependencies: [
@@ -15,24 +16,37 @@ export default {
   query(album) {
     const query = {};
 
+    query.groups =
+      album.groups;
+
     if (album.date) {
-      query.adjacentGroupInfo =
-        album.groups.map(group => {
-          const albums = group.albums.filter(album => album.date);
-          const index = albums.indexOf(album);
-
-          return {
-            previousAlbum:
-              (index > 0
-                ? albums[index - 1]
-                : null),
-
-            nextAlbum:
-              (index < albums.length - 1
-                ? albums[index + 1]
-                : null),
-          };
-        });
+      // 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.
+      const groupAlbums =
+        query.groups.map(group =>
+          sortChronologically(
+            group.albums.filter(album => album.date),
+            {latestFirst: true}));
+
+      const groupCurrentIndex =
+        groupAlbums.map(albums =>
+          albums.indexOf(album));
+
+      query.groupPreviousAlbum =
+        stitchArrays({
+          albums: groupAlbums,
+          index: groupCurrentIndex,
+        }).map(({albums, index}) =>
+            atOffset(albums, index, +1));
+
+      query.groupNextAlbum =
+        stitchArrays({
+          albums: groupAlbums,
+          index: groupCurrentIndex,
+        }).map(({albums, index}) =>
+            atOffset(albums, index, -1));
     }
 
     return query;
@@ -52,27 +66,27 @@ export default {
       album.groups
         .map(group => relation('generateColorStyleAttribute', group.color));
 
-    if (query.adjacentGroupInfo) {
+    if (album.date) {
       relations.previousNextLinks =
-        query.adjacentGroupInfo
-          .map(({previousAlbum, nextAlbum}) =>
+        stitchArrays({
+          previousAlbum: query.groupPreviousAlbum,
+          nextAlbum: query.groupNextAlbum
+        }).map(({previousAlbum, nextAlbum}) =>
             (previousAlbum || nextAlbum
               ? relation('generatePreviousNextLinks')
               : null));
 
       relations.previousAlbumLinks =
-        query.adjacentGroupInfo
-          .map(({previousAlbum}) =>
-            (previousAlbum
-              ? relation('linkAlbumDynamically', previousAlbum)
-              : null));
+        query.groupPreviousAlbum.map(previousAlbum =>
+          (previousAlbum
+            ? relation('linkAlbumDynamically', previousAlbum)
+            : null));
 
       relations.nextAlbumLinks =
-        query.adjacentGroupInfo
-          .map(({nextAlbum}) =>
-            (nextAlbum
-              ? relation('linkAlbumDynamically', nextAlbum)
-              : null));
+        query.groupNextAlbum.map(nextAlbum =>
+          (nextAlbum
+            ? relation('linkAlbumDynamically', nextAlbum)
+            : null));
     }
 
     return relations;