OverviewFilesCommitsIssuesDocsReleasesPeers

keet-tui / lib / components / member-list.js

45 lines · javascript ref:
import * as style from '../style.js'

export function memberListView (model, width, height) {
  const lines = []
  const members = model.members || []

  lines.push(style.bold(style.center(' Members ', width)))
  lines.push(style.hline(width, 'rounded'))

  if (members.length === 0) {
    lines.push(style.dim(style.center('---', width)))
    while (lines.length < height) lines.push('')
    return lines
  }

  const listHeight = height - 3

  // Online first, then by name
  const sorted = [...members].sort((a, b) => {
    if (a.online && !b.online) return -1
    if (!a.online && b.online) return 1
    return (a.name || '').localeCompare(b.name || '')
  })

  for (let i = 0; i < listHeight; i++) {
    if (i >= sorted.length) {
      lines.push('')
      continue
    }
    const m = sorted[i]
    const name = m.name || (m.memberId ? m.memberId.slice(0, 8) : '???')
    const dot = m.online ? style.green('\u25cf') : style.brightBlack('\u25cb')
    const role = m.role === 'admin' ? style.yellow(' \u2605') : ''
    const line = ' ' + dot + ' ' + style.truncate(name, width - 6) + role
    lines.push(style.padRight(line, width))
  }

  // Count
  const online = members.filter(m => m.online).length
  lines.push(style.dim(style.center(online + '/' + members.length + ' online', width)))

  while (lines.length < height) lines.push('')
  return lines
}