« get me outta code hell

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

import groupTracksByGroup from '../util/groupTracksByGroup.js';

export default {
  contentDependencies: ['generateTrackList', 'linkGroup'],
  extraDependencies: ['html', 'language'],

  relations(relation, tracks, groups) {
    if (empty(tracks)) {
      return {};
    }

    if (empty(groups)) {
      return {
        flatList:
          relation('generateTrackList', tracks),
      };
    }

    const lists = groupTracksByGroup(tracks, groups);

    return {
      groupedLists:
        Array.from(lists.entries()).map(([groupOrOther, tracks]) => ({
          ...(groupOrOther === 'other'
                ? {other: true}
                : {groupLink: relation('linkGroup', groupOrOther)}),

          list:
            relation('generateTrackList', tracks),
        })),
    };
  },

  generate(relations, {html, language}) {
    if (relations.flatList) {
      return relations.flatList;
    }

    return html.tag('dl',
      relations.groupedLists.map(({other, groupLink, list}) => [
        html.tag('dt',
          (other
            ? language.$('trackList.group.fromOther')
            : language.$('trackList.group', {
                group: groupLink
              }))),

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