« get me outta code hell

content: listArtistsByName: divide by main groups - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-08-20 21:22:15 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-08-20 22:08:13 -0300
commit60b6715b38d137f8d6d0ce3c537a546a507ecf1f (patch)
tree1cfeb23a2060793934a3df952a45b9d4a3ee36b0 /src/content
parent93fd751aeb3a9c0b60889db0073e7907c87f90fe (diff)
content: listArtistsByName: divide by main groups
Diffstat (limited to 'src/content')
-rw-r--r--src/content/dependencies/listArtistsByName.js123
1 files changed, 94 insertions, 29 deletions
diff --git a/src/content/dependencies/listArtistsByName.js b/src/content/dependencies/listArtistsByName.js
index 6c0ad836..d83150e8 100644
--- a/src/content/dependencies/listArtistsByName.js
+++ b/src/content/dependencies/listArtistsByName.js
@@ -1,51 +1,116 @@
-import {stitchArrays} from '#sugar';
+import {empty, stitchArrays, unique} from '#sugar';
 import {getArtistNumContributions, sortAlphabetically} from '#wiki-data';
 
 export default {
-  contentDependencies: ['generateListingPage', 'linkArtist'],
+  contentDependencies: ['generateListingPage', 'linkArtist', 'linkGroup'],
   extraDependencies: ['language', 'wikiData'],
 
-  sprawl({artistData}) {
-    return {artistData};
+  sprawl({artistData, wikiInfo}) {
+    return {artistData, wikiInfo};
   },
 
-  query({artistData}, spec) {
-    return {
-      spec,
+  query(sprawl, spec) {
+    const artists = sortAlphabetically(sprawl.artistData.slice());
+    const groups = sprawl.wikiInfo.divideTrackListsByGroups;
 
-      artists: sortAlphabetically(artistData.slice()),
-    };
+    if (empty(groups)) {
+      return {spec, artists};
+    }
+
+    const artistGroups =
+      artists.map(artist =>
+        unique(
+          unique([
+            ...artist.albumsAsAny,
+            ...artist.tracksAsAny.map(track => track.album),
+          ]).flatMap(album => album.groups)))
+
+    const artistsByGroup =
+      groups.map(group =>
+        artists.filter((artist, index) => artistGroups[index].includes(group)));
+
+    return {spec, groups, artistsByGroup};
   },
 
   relations(relation, query) {
-    return {
-      page: relation('generateListingPage', query.spec),
+    const relations = {};
 
-      artistLinks:
+    relations.page =
+      relation('generateListingPage', query.spec);
+
+    if (query.artists) {
+      relations.artistLinks =
         query.artists
-          .map(artist => relation('linkArtist', artist)),
-    };
+          .map(artist => relation('linkArtist', artist));
+    }
+
+    if (query.artistsByGroup) {
+      relations.groupLinks =
+        query.groups
+          .map(group => relation('linkGroup', group));
+
+      relations.artistLinksByGroup =
+        query.artistsByGroup
+          .map(artists => artists
+            .map(artist => relation('linkArtist', artist)));
+    }
+
+    return relations;
   },
 
   data(query) {
-    return {
-      counts:
+    const data = {};
+
+    if (query.artists) {
+      data.counts =
         query.artists
-          .map(artist => getArtistNumContributions(artist)),
-    };
+          .map(artist => getArtistNumContributions(artist));
+    }
+
+    if (query.artistsByGroup) {
+      data.countsByGroup =
+        query.artistsByGroup
+          .map(artists => artists
+            .map(artist => getArtistNumContributions(artist)));
+    }
+
+    return data;
   },
 
   generate(data, relations, {language}) {
-    return relations.page.slots({
-      type: 'rows',
-      rows:
-        stitchArrays({
-          link: relations.artistLinks,
-          count: data.counts,
-        }).map(({link, count}) => ({
-            artist: link,
-            contributions: language.countContributions(count, {unit: true}),
-          })),
-    });
+    return (
+      (relations.artistLinksByGroup
+        ? relations.page.slots({
+            type: 'chunks',
+
+            chunkTitles:
+              relations.groupLinks.map(groupLink => ({
+                group: groupLink,
+              })),
+
+            chunkRows:
+              stitchArrays({
+                artistLinks: relations.artistLinksByGroup,
+                counts: data.countsByGroup,
+              }).map(({artistLinks, counts}) =>
+                  stitchArrays({
+                    link: artistLinks,
+                    count: counts,
+                  }).map(({link, count}) => ({
+                      artist: link,
+                      contributions: language.countContributions(count, {unit: true}),
+                    }))),
+          })
+        : relations.page.slots({
+            type: 'rows',
+            rows:
+              stitchArrays({
+                link: relations.artistLinks,
+                count: data.counts,
+              }).map(({link, count}) => ({
+                  artist: link,
+                  contributions: language.countContributions(count, {unit: true}),
+                })),
+          })));
   },
 };