« get me outta code hell

client, write: data.json -> random-link-data.json - 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-11-15 19:16:52 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-11-15 19:24:18 -0400
commit84c367c27ca82938c61b696ef24601d1cff9b2b3 (patch)
treeadb4b91cdca7c1989948341766a82c22b71e09ba
parent28371f6e029fb86ba536bf4c20e787dba44d202b (diff)
client, write: data.json -> random-link-data.json
-rw-r--r--src/static/client3.js120
-rw-r--r--src/write/build-modes/live-dev-server.js11
-rw-r--r--src/write/build-modes/static-build.js18
-rw-r--r--src/write/common-templates.js40
4 files changed, 100 insertions, 89 deletions
diff --git a/src/static/client3.js b/src/static/client3.js
index d2f2bd17..8372a268 100644
--- a/src/static/client3.js
+++ b/src/static/client3.js
@@ -7,11 +7,7 @@
 
 import {getColors} from '../util/colors.js';
 import {empty, stitchArrays} from '../util/sugar.js';
-
-import {
-  filterMultipleArrays,
-  getArtistNumContributions,
-} from '../util/wiki-data.js';
+import {filterMultipleArrays} from '../util/wiki-data.js';
 
 const clientInfo = window.hsmusicClientInfo = Object.create(null);
 
@@ -71,10 +67,6 @@ function cssProp(el, key) {
   return getComputedStyle(el).getPropertyValue(key).trim();
 }
 
-function getRefDirectory(ref) {
-  return ref.split(':')[1];
-}
-
 // TODO: These should pro8a8ly access some shared urlSpec path. We'd need to
 // separ8te the tooling around that into common-shared code too.
 const getLinkHref = (type, directory) => rebase(`${type}/${directory}`);
@@ -100,8 +92,10 @@ const scriptedLinkInfo = clientInfo.scriptedLinkInfo = {
   randomLink: null,
 
   state: {
-    albumData: null,
-    artistData: null,
+    albumDirectories: null,
+    albumTrackDirectories: null,
+    artistDirectories: null,
+    artistNumContributions: null,
   },
 };
 
@@ -147,21 +141,31 @@ function handleRandomLinkClicked(a, domEvent) {
 
 function determineRandomLinkHref(a) {
   const {state} = scriptedLinkInfo;
-  const {albumData, artistData} = state;
 
-  const tracksFromAlbums = albums =>
-    albums
-      .map(album => album.tracks)
-      .reduce((acc, tracks) => acc.concat(tracks), []);
+  const trackDirectoriesFromAlbumDirectories = albumDirectories =>
+    albumDirectories
+      .map(directory => state.albumDirectories.indexOf(directory))
+      .map(index => state.albumTrackDirectories[index])
+      .reduce((acc, trackDirectories) => acc.concat(trackDirectories, []));
 
   switch (a.dataset.random) {
-    case 'album':
-      if (!albumData) return null;
-      return openAlbum(pick(albumData).directory);
+    case 'album': {
+      const {albumDirectories} = state;
+      if (!albumDirectories) return null;
 
-    case 'track':
-      if (!albumData) return null;
-      return openTrack(getRefDirectory(pick(tracksFromAlbums(albumData))));
+      return openAlbum(pick(albumDirectories));
+    }
+
+    case 'track': {
+      const {albumDirectories} = state;
+      if (!albumDirectories) return null;
+
+      const trackDirectories =
+        trackDirectoriesFromAlbumDirectories(
+          albumDirectories);
+
+      return openTrack(pick(trackDirectories));
+    }
 
     case 'album-in-group-dl': {
       const albumLinks =
@@ -170,15 +174,16 @@ function determineRandomLinkHref(a) {
           .nextElementSibling
           .querySelectorAll('li a'))
 
-      const albumDirectories =
+      const listAlbumDirectories =
         albumLinks
           .map(a => cssProp(a, '--album-directory'));
 
-      return openAlbum(pick(albumDirectories));
+      return openAlbum(pick(listAlbumDirectories));
     }
 
     case 'track-in-group-dl': {
-      if (!albumData) return null;
+      const {albumDirectories} = state;
+      if (!albumDirectories) return null;
 
       const albumLinks =
         Array.from(a
@@ -186,16 +191,15 @@ function determineRandomLinkHref(a) {
           .nextElementSibling
           .querySelectorAll('li a'))
 
-      const albumDirectories =
+      const listAlbumDirectories =
         albumLinks
           .map(a => cssProp(a, '--album-directory'));
 
-      const filteredAlbumData =
-        albumData
-          .filter(album =>
-            albumDirectories.includes(album.directory));
+      const trackDirectories =
+        trackDirectoriesFromAlbumDirectories(
+          listAlbumDirectories);
 
-      return openTrack(getRefDirectory(pick(tracksFromAlbums(filteredAlbumData))));
+      return openTrack(pick(trackDirectories));
     }
 
     case 'track-in-sidebar': {
@@ -211,27 +215,32 @@ function determineRandomLinkHref(a) {
     }
 
     case 'track-in-album': {
-      if (!albumData) return null;
+      const {albumDirectories, albumTrackDirectories} = state;
+      if (!albumDirectories || !albumTrackDirectories) return null;
 
-      const directory = cssProp(a, '--album-directory');
-      const {tracks} = albumData.find(album => album.directory === directory);
+      const albumDirectory = cssProp(a, '--album-directory');
+      const albumIndex = albumDirectories.indexOf(albumDirectory);
+      const trackDirectories = albumTrackDirectories[albumIndex];
 
-      return openTrack(getRefDirectory(pick(tracks)));
+      return openTrack(pick(trackDirectories));
     }
 
     case 'artist': {
-      if (!artistData) return null;
-      return openArtist(pick(artistData).directory);
+      const {artistDirectories} = state;
+      if (!artistDirectories) return null;
+
+      return openArtist(pick(artistDirectories));
     }
 
     case 'artist-more-than-one-contrib': {
-      if (!artistData) return null;
+      const {artistDirectories, artistNumContributions} = state;
+      if (!artistDirectories || !artistNumContributions) return null;
 
-      const artists =
-        artistData
-          .filter(artist => getArtistNumContributions(artist) > 1);
+      const filteredArtistDirectories =
+        artistDirectories
+          .filter((_artist, index) => artistNumContributions[index] > 1);
 
-      return openArtist(pick(artists).directory);
+      return openArtist(pick(filteredArtistDirectories));
     }
   }
 }
@@ -291,29 +300,32 @@ if (
 
   dataLoadingLine.style.display = 'block';
 
-  fetch(rebase('data.json', 'rebaseShared'))
-    .then((data) => data.json())
-    .then((data) => {
+  fetch(rebase('random-link-data.json', 'rebaseShared'))
+    .then(data => data.json())
+    .then(data => {
       const {state} = scriptedLinkInfo;
 
-      state.albumData = data.albumData;
-      state.artistData = data.artistData;
+      Object.assign(state, {
+        albumDirectories: data.albumDirectories,
+        albumTrackDirectories: data.albumTrackDirectories,
+        artistDirectories: data.artistDirectories,
+        artistNumContributions: data.artistNumContributions,
+      });
 
       dataLoadingLine.style.display = 'none';
       dataLoadedLine.style.display = 'block';
+    }, () => {
+      dataLoadingLine.style.display = 'none';
+      dataErrorLine.style.display = 'block';
     })
-    .catch(() => {
-      const info = scriptedLinkInfo;
-
-      for (const a of info.randomLinks) {
+    .then(() => {
+      const {randomLinks} = scriptedLinkInfo;
+      for (const a of randomLinks) {
         const href = determineRandomLinkHref(a);
         if (!href) {
           a.removeAttribute('href');
         }
       }
-
-      dataLoadingLine.style.display = 'none';
-      dataErrorLine.style.display = 'block';
     });
 }
 
diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js
index ab6ceecb..8828a5bd 100644
--- a/src/write/build-modes/live-dev-server.js
+++ b/src/write/build-modes/live-dev-server.js
@@ -16,7 +16,7 @@ import {
 } from '#urls';
 
 import {bindUtilities} from '../bind-utilities.js';
-import {generateGlobalWikiDataJSON, generateRedirectHTML} from '../common-templates.js';
+import {generateRandomLinkDataJSON, generateRedirectHTML} from '../common-templates.js';
 
 const defaultHost = '0.0.0.0';
 const defaultPort = 8002;
@@ -157,19 +157,20 @@ export async function go({
 
     // Specialized routes
 
-    if (pathname === '/data.json') {
+    if (pathname === '/random-link-data.json') {
       try {
-        const json = generateGlobalWikiDataJSON({
+        const json = generateRandomLinkDataJSON({
           serializeThings,
           wikiData,
         });
+
         response.writeHead(200, contentTypeJSON);
         response.end(json);
-        if (loudResponses) console.log(`${requestHead} [200] /data.json`);
+        if (loudResponses) console.log(`${requestHead} [200] ${pathname}`);
       } catch (error) {
         response.writeHead(500, contentTypeJSON);
         response.end(`Internal error serializing wiki JSON`);
-        console.error(`${requestHead} [500] /data.json`);
+        console.error(`${requestHead} [500] ${pathname}`);
         showError(error);
       }
       return;
diff --git a/src/write/build-modes/static-build.js b/src/write/build-modes/static-build.js
index b6dc9643..a8e0eb23 100644
--- a/src/write/build-modes/static-build.js
+++ b/src/write/build-modes/static-build.js
@@ -31,7 +31,7 @@ import {
 } from '#urls';
 
 import {bindUtilities} from '../bind-utilities.js';
-import {generateRedirectHTML, generateGlobalWikiDataJSON} from '../common-templates.js';
+import {generateRedirectHTML, generateRandomLinkDataJSON} from '../common-templates.js';
 
 const pageFlags = Object.keys(pageSpecs);
 
@@ -145,14 +145,8 @@ export async function go({
   });
 
   await writeSharedFilesAndPages({
-    language: defaultLanguage,
     outputPath,
-    urls,
-    wikiData,
-    wikiDataJSON: generateGlobalWikiDataJSON({
-      serializeThings,
-      wikiData,
-    }),
+    randomLinkDataJSON: generateRandomLinkDataJSON({wikiData}),
   });
 
   const buildSteps = writeAll
@@ -477,12 +471,12 @@ async function writeFavicon({
 
 async function writeSharedFilesAndPages({
   outputPath,
-  wikiDataJSON,
+  randomLinkDataJSON,
 }) {
   return progressPromiseAll(`Writing files & pages shared across languages.`, [
-    wikiDataJSON &&
+    randomLinkDataJSON &&
       writeFile(
-        path.join(outputPath, 'data.json'),
-        wikiDataJSON),
+        path.join(outputPath, 'random-link-data.json'),
+        randomLinkDataJSON),
   ].filter(Boolean));
 }
diff --git a/src/write/common-templates.js b/src/write/common-templates.js
index 2dd4c924..d897a73b 100644
--- a/src/write/common-templates.js
+++ b/src/write/common-templates.js
@@ -1,4 +1,5 @@
 import * as html from '#html';
+import {getArtistNumContributions} from '#wiki-data';
 
 export function generateRedirectHTML(title, target, {language}) {
   return `<!DOCTYPE html>\n` + html.tag('html', [
@@ -30,22 +31,25 @@ export function generateRedirectHTML(title, target, {language}) {
   ]);
 }
 
-export function generateGlobalWikiDataJSON({
-  serializeThings,
-  wikiData,
-}) {
-  const stringifyThings = thingData =>
-    JSON.stringify(serializeThings(thingData));
-
-  return '{\n' +
-    ([
-      `"albumData": ${stringifyThings(wikiData.albumData)},`,
-      wikiData.wikiInfo.enableFlashesAndGames &&
-        `"flashData": ${stringifyThings(wikiData.flashData)},`,
-      `"artistData": ${stringifyThings(wikiData.artistData)}`,
-    ]
-      .filter(Boolean)
-      .map(line => '  ' + line)
-      .join('\n')) +
-    '\n}';
+export function generateRandomLinkDataJSON({wikiData}) {
+  const {albumData, artistData} = wikiData;
+
+  return JSON.stringify({
+    albumDirectories:
+      albumData
+        .map(album => album.directory),
+
+    albumTrackDirectories:
+      albumData
+        .map(album => album.tracks
+          .map(track => track.directory)),
+
+    artistDirectories:
+      artistData
+        .map(artist => artist.directory),
+
+    artistNumContributions:
+      artistData
+        .map(artist => getArtistNumContributions(artist)),
+  });
 }