« get me outta code hell

generateArtistInfoPageChunk.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/generateArtistInfoPageChunk.js
blob: 3fa46c614c19ffa445702edecce1392295ac8a72 (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
import {empty} from '#sugar';

export default {
  slots: {
    mode: {
      validate: v => v.is('flash', 'album'),
    },

    id: {type: 'string'},

    link: {
      type: 'html',
      mutable: false,
    },

    list: {
      type: 'html',
      mutable: false,
    },

    dates: {
      validate: v => v.sparseArrayOf(v.isDate),
    },

    duration: {validate: v => v.isDuration},
    durationApproximate: {type: 'boolean'},
  },

  generate(slots, {html, language}) {
    let earliestDate = null;
    let latestDate = null;
    let onlyDate = null;

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

      latestDate =
        slots.dates
          .reduce((a, b) => a <= b ? b : a);

      if (+earliestDate === +latestDate) {
        onlyDate = earliestDate;
      }
    }

    let accentedLink;
    switch (slots.mode) {
      case 'album': {
        const options = {album: slots.link};
        const parts = ['artistPage.creditList.album'];

        if (onlyDate) {
          parts.push('withDate');
          options.date = language.formatDate(onlyDate);
        }

        if (slots.duration) {
          parts.push('withDuration');
          options.duration =
            language.formatDuration(slots.duration, {
              approximate: slots.durationApproximate,
            });
        }

        accentedLink = language.formatString(...parts, options);
        break;
      }

      case 'flash': {
        const options = {act: slots.link};
        const parts = ['artistPage.creditList.flashAct'];

        if (onlyDate) {
          parts.push('withDate');
          options.date = language.formatDate(onlyDate);
        } else if (earliestDate && latestDate) {
          parts.push('withDateRange');
          options.dateRange =
            language.formatDateRange(earliestDate, latestDate);
        }

        accentedLink = language.formatString(...parts, options);
        break;
      }
    }

    return html.tags([
      html.tag('dt',
        slots.id && {id: slots.id},
        accentedLink),

      html.tag('dd', slots.list),
    ]);
  },
};