« get me outta code hell

generateArtistInfoPageTracksChunk.js « dependencies « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/generateArtistInfoPageTracksChunk.js
blob: cc9b898fae557178b78210ca4aa17d2c5d2de8ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import {empty, unique} from '#sugar';
import {getTotalDuration} from '#wiki-data';

export default {
  contentDependencies: [
    'generateArtistInfoPageChunk',
    'generateArtistInfoPageTracksChunkItem',
    'linkAlbum',
  ],

  relations: (relation, artist, album, trackContribLists) => ({
    template:
      relation('generateArtistInfoPageChunk'),

    albumLink:
      relation('linkAlbum', album),

    // Intentional mapping here: each item may be associated with
    // more than one contribution.
    items:
      trackContribLists.map(trackContribs =>
        relation('generateArtistInfoPageTracksChunkItem',
          artist,
          trackContribs)),
  }),

  data(_artist, album, trackContribLists) {
    const data = {};

    const allDates =
      trackContribLists
        .flat()
        .filter(contrib => contrib.date)
        .map(contrib => contrib.date);

    if (!empty(allDates)) {
      const earliestDate =
        allDates
          .reduce((a, b) => a <= b ? a : b);

      const latestDate =
        allDates
          .reduce((a, b) => a <= b ? b : a);

      if (+earliestDate === +latestDate) {
        data.date = earliestDate;
      } else {
        data.earliestDate = earliestDate;
        data.latestDate = latestDate;
      }
    }

    // TODO: Duration stuff should *maybe* be in proper data logic? Maaaybe?
    const durationTerms =
      unique(
        trackContribLists
          .flat()
          .filter(contrib => contrib.countInDurationTotals)
          .map(contrib => contrib.thing)
          .filter(track => track.isOriginalRelease)
          .filter(track => track.duration > 0));

    data.duration =
      getTotalDuration(durationTerms);

    data.durationApproximate =
      durationTerms.length > 1;

    return data;
  },

  generate: (data, relations) =>
    relations.template.slots({
      mode: 'album',

      albumLink: relations.albumLink,

      date: data.date ?? null,
      dateRangeStart: data.earliestDate ?? null,
      dateRangeEnd: data.latestDate ?? null,

      duration: data.duration,
      durationApproximate: data.durationApproximate,

      items: relations.items,
    }),
};