« 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/content/dependencies
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/content/dependencies
parent5408d6660b22b9ddee8c4a297c89fca92ae2d505 (diff)
content: listArtistsByDuration
Diffstat (limited to 'src/content/dependencies')
-rw-r--r--src/content/dependencies/listArtistsByDuration.js55
1 files changed, 55 insertions, 0 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),
+          })),
+    });
+  },
+};