« get me outta code hell

generateArtistInfoPageTracksChunkedList.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/generateArtistInfoPageTracksChunkedList.js
blob: 6a1a1700dfef0601685bd7c1b228460e9521b830 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import {sortAlbumsTracksChronologically, sortEntryThingPairs} from '#sort';
import {chunkByProperties, stitchArrays} from '#sugar';

export default {
  contentDependencies: [
    'generateArtistInfoPageChunkedList',
    'generateArtistInfoPageTracksChunk',
  ],

  query(artist) {
    const processTrackEntry = ({track, contribs}) => ({
      thing: track,
      entry: {
        track: track,
        album: track.album,
        date: track.date,
        contribs: contribs,
      },
    });

    const processTrackEntries = ({tracks, contribs}) =>
      stitchArrays({
        track: tracks,
        contribs: contribs,
      }).map(processTrackEntry);

    const {tracksAsArtist, tracksAsContributor} = artist;

    const tracksAsArtistAndContributor =
      tracksAsArtist
        .filter(track => tracksAsContributor.includes(track));

    const tracksAsArtistOnly =
      tracksAsArtist
        .filter(track => !tracksAsContributor.includes(track));

    const tracksAsContributorOnly =
      tracksAsContributor
        .filter(track => !tracksAsArtist.includes(track));

    const tracksAsArtistAndContributorContribs =
      tracksAsArtistAndContributor
        .map(track => [
          ...
            track.artistContribs
              .map(contrib => ({...contrib, kind: 'artist'})),
          ...
            track.contributorContribs
              .map(contrib => ({...contrib, kind: 'contributor'})),
        ]);

    const tracksAsArtistOnlyContribs =
      tracksAsArtistOnly
        .map(track => track.artistContribs
          .map(contrib => ({...contrib, kind: 'artist'})));

    const tracksAsContributorOnlyContribs =
      tracksAsContributorOnly
        .map(track => track.contributorContribs
          .map(contrib => ({...contrib, kind: 'contributor'})));

    const tracksAsArtistAndContributorEntries =
      processTrackEntries({
        tracks: tracksAsArtistAndContributor,
        contribs: tracksAsArtistAndContributorContribs,
      });

    const tracksAsArtistOnlyEntries =
      processTrackEntries({
        tracks: tracksAsArtistOnly,
        contribs: tracksAsArtistOnlyContribs,
      });

    const tracksAsContributorOnlyEntries =
      processTrackEntries({
        tracks: tracksAsContributorOnly,
        contribs: tracksAsContributorOnlyContribs,
      });

    const entries = [
      ...tracksAsArtistAndContributorEntries,
      ...tracksAsArtistOnlyEntries,
      ...tracksAsContributorOnlyEntries,
    ];

    sortEntryThingPairs(entries, sortAlbumsTracksChronologically);

    const chunks =
      chunkByProperties(
        entries.map(({entry}) => entry),
        ['album', 'date']);

    return {chunks};
  },

  relations: (relation, query, artist) => ({
    chunkedList:
      relation('generateArtistInfoPageChunkedList'),

    chunks:
      query.chunks.map(({chunk, album}) =>
        relation('generateArtistInfoPageTracksChunk',
          artist,
          album,
          chunk.map(entry => entry.track),
          chunk.map(entry => entry.contribs))),
  }),

  generate: (relations) =>
    relations.chunkedList.slots({
      chunks: relations.chunks,
    }),
};