fix html template
[websub-hub] / src / template / template-helper.js
index b48a90ae6d02046dd7244788ec11d150e0c40ad8..0ff90772ec7ebd2479344979b08e7d4c075fdb01 100644 (file)
@@ -61,6 +61,11 @@ const secondsToPeriod = (seconds) => {
  * @returns {String}
  */
 function renderTopicRow(topic, subscribers, detailsLink = true) {
+  if (!topic) {
+    return `<tr>
+    <th colspan="15">(topic not found)</th>
+</tr>`;
+  }
   return `<tr>
   <th scope="row">${detailsLink ? '<a href="topic/' + topic.id + '">' : ''}${topic.url}${detailsLink ? '</a>' : ''}</th>
   <td>${subscribers.length}</td>
@@ -112,6 +117,11 @@ function renderTopicRowHeader() {
  * @returns {String}
  */
 function renderSubscriptionRow(subscription) {
+  if (!subscription) {
+    return `<tr>
+    <th colspan="12">(topic not found)</th>
+</tr>`;
+  }
   return `<tr>
   <td scope="row">${subscription.callback}</td>
   <td>${dateOrNot(subscription.created, 'Unknown')}</td>
@@ -162,7 +172,7 @@ function renderSubscriptionRowHeader() {
 function htmlHead(pagePathLevel, pageTitle, headElements = []) {
   const rootPathPfx = '../'.repeat(pagePathLevel);
   return `<!DOCTYPE html>
-<html lang="en>
+<html lang="en">
   <head>
     <meta charset="utf-8">` +
   headElements.map((e) => `${'  '.repeat(2)}${e}`).join('\n') + `
@@ -221,41 +231,50 @@ function htmlHeader(pageTitle, navLinks = []) {
 
 /**
  * Close the main section and finish off with boilerplate.
+ * @param {String[]} footerEntries
  * @returns {String}
  */
-function htmlFooter() {
+function htmlFooter(footerEntries = []) {
   return `    </main>
-    <footer>
-      <ol>
-        <li>
-          <a href="https://git.squeep.com/?p=websub-hub;a=tree">Development Repository</a>
-        </li>
-        <li>
-          <a href="https://squeep.com/">A Squeep Infrastructure Component</a>
-        </li>
-        <li>
-          &copy;<time datetime="2021">&#8559;&#8559;&#8553;&#8553;&#8544;</time>
-        </li>
-      </ol>
+    <footer>` +
+    (footerEntries.length ? `
+      <ol>` + footerEntries.map((f) => `        <li>${f}</li>`).join('\n') + `
+      </ol>`
+      : '') + `
     </footer>`;
 }
 
 
 /**
- * Render all parts of an HTML page.
+ * Render all parts of an HTML page. Adds user logout nav link automatically.
+ * @param {Object} ctx
  * @param {Number} pagePathLevel
  * @param {String} pageTitle
  * @param {String[]} headElements
  * @param {Object[]} navLinks
  * @param {String[]} main
+ * @param {String[]} footerEntries
  * @returns {String}
  */
-function htmlTemplate(pagePathLevel, pageTitle, headElements = [], navLinks = [], main = []) {
+function htmlTemplate(ctx, pagePathLevel, pageTitle, headElements = [], navLinks = [], main = [], footerEntries = []) {
+  const user = (ctx && ctx.session && ctx.session.authenticatedProfile) || (ctx && ctx.session && ctx.session.authenticatedIdentifier);
+  if (user) {
+    let logoutPath;
+    if (pagePathLevel > 0) {
+      logoutPath = `${'../'.repeat(pagePathLevel - 1)}`;
+    } else {
+      logoutPath = 'admin/';
+    }
+    navLinks.push({
+      text: `Logout (${user})`,
+      href: `${logoutPath}logout`,
+    });
+  }
   return [
     htmlHead(pagePathLevel, pageTitle, headElements),
     htmlHeader(pageTitle, navLinks),
     ...main,
-    htmlFooter(),
+    htmlFooter(footerEntries),
     htmlTail(),
   ].join('\n');
 }