« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/content/dependencies/generateAdditionalFilesList.js56
1 files changed, 42 insertions, 14 deletions
diff --git a/src/content/dependencies/generateAdditionalFilesList.js b/src/content/dependencies/generateAdditionalFilesList.js
index 7f257e44..c51435a4 100644
--- a/src/content/dependencies/generateAdditionalFilesList.js
+++ b/src/content/dependencies/generateAdditionalFilesList.js
@@ -1,3 +1,5 @@
+import {empty} from '../../util/sugar.js';
+
 export default {
   extraDependencies: [
     'html',
@@ -17,18 +19,43 @@ export default {
     language,
   }) {
     return html.template(slot =>
-      html.tag('dl',
-        data.additionalFiles.flatMap(({title, description, files}) => [
-          html.tag('dt',
-            (description
-              ? language.$('releaseInfo.additionalFiles.entry.withDescription', {
-                  title,
-                  description,
-                })
-              : language.$('releaseInfo.additionalFiles.entry', {title}))),
-
-          slot('additionalFileLinks', ([fileLinks]) =>
-          slot('additionalFileSizes', ([fileSizes]) =>
+      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 =>
@@ -40,7 +67,8 @@ export default {
                         })
                       : language.$('releaseInfo.additionalFiles.file', {
                           file: fileLinks[file],
-                        })))))))),
-        ])));
+                        })))))),
+          ]));
+      })));
   },
 };