« get me outta code hell

generateAdditionalFilesList.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/generateAdditionalFilesList.js
blob: c51435a459f0f49c1f6c7720a44238a7077d3a8c (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
import {empty} from '../../util/sugar.js';

export default {
  extraDependencies: [
    'html',
    'language',
  ],

  data(additionalFiles, {fileSize = true} = {}) {
    return {
      // Additional files are already a serializable format.
      additionalFiles,
      showFileSizes: fileSize,
    };
  },

  generate(data, {
    html,
    language,
  }) {
    return html.template(slot =>
      slot('additionalFileLinks', ([fileLinks]) =>
      slot('additionalFileSizes', ([fileSizes]) => {
        if (!fileSizes) {
          return html.blank();
        }

        const filesWithLinks = new Set(
          Object.entries(fileLinks)
            .filter(([key, value]) => value)
            .map(([key]) => key));

        if (filesWithLinks.size === 0) {
          return html.blank();
        }

        const filteredFileGroups = data.additionalFiles
          .map(({title, description, files}) => ({
            title,
            description,
            files: files.filter(f => filesWithLinks.has(f)),
          }))
          .filter(({files}) => !empty(files));

        if (empty(filteredFileGroups)) {
          return html.blank();
        }

        return html.tag('dl',
          filteredFileGroups.flatMap(({title, description, files}) => [
            html.tag('dt',
              (description
                ? language.$('releaseInfo.additionalFiles.entry.withDescription', {
                    title,
                    description,
                  })
                : language.$('releaseInfo.additionalFiles.entry', {title}))),

            html.tag('dd',
              html.tag('ul',
                files.map(file =>
                  html.tag('li',
                    (fileSizes[file]
                      ? language.$('releaseInfo.additionalFiles.file.withSize', {
                          file: fileLinks[file],
                          size: language.formatFileSize(fileSizes[file]),
                        })
                      : language.$('releaseInfo.additionalFiles.file', {
                          file: fileLinks[file],
                        })))))),
          ]));
      })));
  },
};