From 6e7d540f2daf84399f34fc923f7f08cc15af3525 Mon Sep 17 00:00:00 2001
From: "(quasar) nebula" <qznebula@protonmail.com>
Date: Wed, 5 Apr 2023 16:30:21 -0300
Subject: content: generateAdditionalFilesList: guard against nulls cleanly

---
 .../dependencies/generateAdditionalFilesList.js    | 56 ++++++++++++++++------
 1 file changed, 42 insertions(+), 14 deletions(-)

(limited to 'src/content/dependencies')

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],
-                        })))))))),
-        ])));
+                        })))))),
+          ]));
+      })));
   },
 };
-- 
cgit 1.3.0-6-gf8a5