« 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/generateFooterLocalizationLinks.js53
1 files changed, 34 insertions, 19 deletions
diff --git a/src/content/dependencies/generateFooterLocalizationLinks.js b/src/content/dependencies/generateFooterLocalizationLinks.js
index 5df83566..86e6c61a 100644
--- a/src/content/dependencies/generateFooterLocalizationLinks.js
+++ b/src/content/dependencies/generateFooterLocalizationLinks.js
@@ -1,3 +1,6 @@
+import {stitchArrays} from '#sugar';
+import {sortByName} from '#wiki-data';
+
 export default {
   extraDependencies: [
     'defaultLanguage',
@@ -16,25 +19,37 @@ export default {
     pagePath,
     to,
   }) {
-    const links = Object.entries(languages)
-      .filter(([code, language]) => code !== 'default' && !language.hidden)
-      .map(([code, language]) => language)
-      .sort(({name: a}, {name: b}) => (a < b ? -1 : a > b ? 1 : 0))
-      .map((language) =>
-        html.tag('span',
-          html.tag('a',
-            {
-              href:
-                language === defaultLanguage
-                  ? to(
-                      'localizedDefaultLanguage.' + pagePath[0],
-                      ...pagePath.slice(1))
-                  : to(
-                      'localizedWithBaseDirectory.' + pagePath[0],
-                      language.code,
-                      ...pagePath.slice(1)),
-            },
-            language.name)));
+    const switchableLanguages =
+      Object.entries(languages)
+        .filter(([code, language]) => code !== 'default' && !language.hidden)
+        .map(([code, language]) => language);
+
+    if (switchableLanguages.length <= 1) {
+      return html.blank();
+    }
+
+    sortByName(switchableLanguages);
+
+    const [pagePathSubkey, ...pagePathArgs] = pagePath;
+
+    const linkPaths =
+      switchableLanguages.map(language =>
+        (language === defaultLanguage
+          ? (['localizedDefaultLanguage.' + pagePathSubkey,
+              ...pagePathArgs])
+          : (['localizedWithBaseDirectory.' + pagePathSubkey,
+              language.code,
+              ...pagePathArgs])));
+
+    const links =
+      stitchArrays({
+        language: switchableLanguages,
+        linkPath: linkPaths,
+      }).map(({language, linkPath}) =>
+          html.tag('span',
+            html.tag('a',
+              {href: to(...linkPath)},
+              language.name)));
 
     return html.tag('div', {class: 'footer-localization-links'},
       language.$('misc.uiLanguage', {