« get me outta code hell

content: generateAdditionalFilesList: guard against nulls cleanly - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-04-05 16:30:21 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-04-05 16:30:21 -0300
commit6e7d540f2daf84399f34fc923f7f08cc15af3525 (patch)
treeea0a70a1ddca9eb3d70f83bbf479ea101c93bb54
parentcea250920bdbe12e1d87469ee9070211610884a4 (diff)
content: generateAdditionalFilesList: guard against nulls cleanly
-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],
-                        })))))))),
-        ])));
+                        })))))),
+          ]));
+      })));
   },
 };