OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-note / lib / views / user-ui.js

43 lines · javascript ref:
/**
 * User Public Profile — List of user's public notebooks
 *
 * SSR view showing a user's public-facing profile.
 */

import { renderPage } from '../page-layout.js'

function esc (s) {
  return (s || '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;')
}

/**
 * Render user public profile page
 *
 * @param {Object} user - User object with name, username
 * @param {Array} notebooks - Public notebooks
 * @param {Object} opts - { pathPrefix }
 * @returns {string} Full HTML page
 */
export function renderUserPage (user, notebooks, opts = {}) {
  const pathPrefix = opts.pathPrefix || ''

  let content = ''
  content += `<h1>@${esc(user.username || 'anonymous')}</h1>`
  content += `<p class="meta">${esc(user.name || 'Anonymous')}</p>`

  if (notebooks.length === 0) {
    content += '<p class="meta">No public notebooks.</p>'
  } else {
    content += `<p class="meta">${notebooks.length} public notebook${notebooks.length !== 1 ? 's' : ''}</p>`
    for (const nb of notebooks) {
      content += `<a class="card" href="${pathPrefix}/notebooks/${encodeURIComponent(nb.slug)}">`
      content += `<h3>${esc(nb.name || nb.slug)}</h3>`
      if (nb.description) content += `<div class="desc">${esc(nb.description)}</div>`
      content += `<div class="desc">${nb.noteCount || 0} notes</div>`
      content += '</a>'
    }
  }

  return renderPage('@' + (user.username || 'user'), content, { pathPrefix, activePage: '' })
}