« get me outta code hell

content: listArtistsByDuration - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-07-04 10:02:54 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-07-04 10:13:56 -0300
commit882eb4325aa21d71fd8bbb466bb98bd9e22b4384 (patch)
tree37abc2fe1e5246d779119ab84d16e2c189497b44 /src
parent5408d6660b22b9ddee8c4a297c89fca92ae2d505 (diff)
content: listArtistsByDuration
Diffstat (limited to 'src')
-rw-r--r--src/content/dependencies/listArtistsByDuration.js55
-rw-r--r--src/listing-spec.js19
2 files changed, 56 insertions, 18 deletions
diff --git a/src/content/dependencies/listArtistsByDuration.js b/src/content/dependencies/listArtistsByDuration.js
new file mode 100644
index 00000000..478e99bb
--- /dev/null
+++ b/src/content/dependencies/listArtistsByDuration.js
@@ -0,0 +1,55 @@
+import {stitchArrays} from '../../util/sugar.js';
+import {filterByCount, getTotalDuration, sortByCount} from '../../util/wiki-data.js';
+
+export default {
+  contentDependencies: ['generateListingPage', 'linkArtist'],
+  extraDependencies: ['language', 'wikiData'],
+
+  sprawl({artistData}) {
+    return {artistData};
+  },
+
+  query({artistData}, spec) {
+    const artists = artistData.slice();
+    const durations = artists.map(artist =>
+      getTotalDuration([
+        ...(artist.tracksAsArtist ?? []),
+        ...(artist.tracksAsContributor ?? []),
+      ], {originalReleasesOnly: true}));
+
+    filterByCount(artists, durations);
+    sortByCount(artists, durations, {greatestFirst: true});
+
+    return {spec, artists, durations};
+  },
+
+  relations(relation, query) {
+    return {
+      page: relation('generateListingPage', query.spec),
+
+      artistLinks:
+        query.artists
+          .map(artist => relation('linkArtist', artist)),
+    };
+  },
+
+  data(query) {
+    return {
+      durations: query.durations,
+    };
+  },
+
+  generate(data, relations, {language}) {
+    return relations.page.slots({
+      type: 'rows',
+      rows:
+        stitchArrays({
+          link: relations.artistLinks,
+          duration: data.durations,
+        }).map(({link, duration}) => ({
+            artist: link,
+            duration: language.formatDuration(duration),
+          })),
+    });
+  },
+};
diff --git a/src/listing-spec.js b/src/listing-spec.js
index 1b0fe978..352f89db 100644
--- a/src/listing-spec.js
+++ b/src/listing-spec.js
@@ -77,24 +77,7 @@ listingSpec.push({
 listingSpec.push({
   directory: 'artists/by-duration',
   stringsKey: 'listArtists.byDuration',
-
-  data: ({wikiData: {artistData}}) =>
-    artistData
-      .map((artist) => ({
-        artist,
-        duration: getTotalDuration([
-          ...(artist.tracksAsArtist ?? []),
-          ...(artist.tracksAsContributor ?? []),
-        ], {originalReleasesOnly: true}),
-      }))
-      .filter(({duration}) => duration > 0)
-      .sort((a, b) => b.duration - a.duration),
-
-  row: ({artist, duration}, {language, link}) =>
-    language.$('listingPage.listArtists.byDuration.item', {
-      artist: link.artist(artist),
-      duration: language.formatDuration(duration),
-    }),
+  contentFunction: 'listArtistsByDuration',
 });
 
 listingSpec.push({