« get me outta code hell

generateArtistInfoPageTracksChunkItem.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/generateArtistInfoPageTracksChunkItem.js
blob: a42d6feee3a833ce5df175ea359bd1491c2f44a5 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {sortChronologically} from '#sort';
import {empty} from '#sugar';

export default {
  contentDependencies: [
    'generateArtistInfoPageChunkItem',
    'generateArtistInfoPageFirstReleaseTooltip',
    'generateArtistInfoPageOtherArtistLinks',
    'generateArtistInfoPageRereleaseTooltip',
    'linkTrack',
  ],

  extraDependencies: ['html', 'language'],

  query (_artist, contribs) {
    const query = {};

    // TODO: Very mysterious what to do if the set of contributions is,
    // in total, associated with more than one thing. No design yet.
    query.track =
      contribs[0].thing;

    const creditedAsArtist =
      contribs
        .some(contrib => contrib.isArtistContribution);

    const creditedAsContributor =
      contribs
        .some(contrib => contrib.isContributorContribution);

    const annotatedContribs =
      contribs
        .filter(contrib => contrib.annotation);

    const annotatedArtistContribs =
      annotatedContribs
        .filter(contrib => contrib.isArtistContribution);

    const annotatedContributorContribs =
      annotatedContribs
        .filter(contrib => contrib.isContributorContribution);

    // Don't display annotations associated with crediting in the
    // Contributors field if the artist is also credited as an Artist
    // *and* the Artist-field contribution is non-annotated. This is
    // so that we don't misrepresent the artist - the contributor
    // annotation tends to be for "secondary" and performance roles.
    // For example, this avoids crediting Marcy Nabors on Renewed
    // Return seemingly only for "bass clarinet" when they're also
    // the one who composed and arranged Renewed Return!
    if (
      creditedAsArtist &&
      creditedAsContributor &&
      empty(annotatedArtistContribs)
    ) {
      query.displayedContributions = null;
    } else if (
      !empty(annotatedArtistContribs) ||
      !empty(annotatedContributorContribs)
    ) {
      query.displayedContributions = [
        ...annotatedArtistContribs,
        ...annotatedContributorContribs,
      ];
    }

    // It's kinda awkward to perform this chronological sort here,
    // per track, rather than just reusing the one that's done to
    // sort all the items on the page altogether... but then, the
    // sort for the page is actually *a different* sort, on purpsoe.
    // That sort is according to the dates of the contributions;
    // this is according to the dates of the tracks. Those can be
    // different - and it's the latter that determines whether the
    // track is a rerelease!
    const allReleasesChronologically =
      sortChronologically(query.track.allReleases);

    query.isFirstRelease =
      allReleasesChronologically[0] === query.track;

    query.isRerelease =
      allReleasesChronologically[0] !== query.track;

    query.hasOtherReleases =
      !empty(query.track.otherReleases);

    return query;
  },

  relations: (relation, query, artist, contribs) => ({
    template:
      relation('generateArtistInfoPageChunkItem'),

    trackLink:
      relation('linkTrack', query.track),

    otherArtistLinks:
      relation('generateArtistInfoPageOtherArtistLinks', contribs),

    rereleaseTooltip:
      (query.isRerelease
        ? relation('generateArtistInfoPageRereleaseTooltip', query.track, artist)
        : null),

    firstReleaseTooltip:
      (query.isFirstRelease && query.hasOtherReleases
        ? relation('generateArtistInfoPageFirstReleaseTooltip', query.track, artist)
        : null),
  }),

  data: (query) => ({
    duration:
      query.track.duration,

    contribAnnotations:
      (query.displayedContributions
        ? query.displayedContributions
            .map(contrib => contrib.annotation)
        : null),
  }),

  generate: (data, relations, {html, language}) =>
    relations.template.slots({
      otherArtistLinks: relations.otherArtistLinks,
      rereleaseTooltip: relations.rereleaseTooltip,
      firstReleaseTooltip: relations.firstReleaseTooltip,

      annotation:
        (data.contribAnnotations
          ? language.formatUnitList(data.contribAnnotations)
          : html.blank()),

      content:
        language.encapsulate('artistPage.creditList.entry.track', workingCapsule => {
          const workingOptions = {track: relations.trackLink};

          if (data.duration) {
            workingCapsule += '.withDuration';
            workingOptions.duration =
              language.formatDuration(data.duration);
          }

          return language.$(workingCapsule, workingOptions);
        }),
    }),
};