<commit_before>import * as yo from 'yo-yo'
import { niceDate } from '../../lib/time'
import { ucfirst } from '../../lib/strings'
import { renderArchives, entriesListToTree } from '../com/files-list'
import prettyBytes from 'pretty-bytes'
import emitStream from 'emit-stream'
import multicb from 'multicb'

// globals
// =

// list of archives
var archives = []

// event emitter
var archivesEvents


// exported API
// =

export function setup () {
}

export function show () {
  document.title = 'My Dat Sites'
  // fetch archives
  beaker.dat.ownedArchives((err, subscribed) => {
    archives = subscribed
    archives.sort(archiveSortFn)
    console.log(archives)
    render()
  })

  // start event stream and register events
  if (!archivesEvents) {
    archivesEvents = emitStream(beaker.dat.archivesEventStream())
    archivesEvents.on('update-archive', onUpdateArchive)
    archivesEvents.on('update-peers', onUpdatePeers)
  }
}

export function hide () {
  archives = null
}

// internal methods
// =

function archiveSortFn (a, b) {
  return b.mtime - a.mtime
}

// rendering
// =

function render () {
  // render view
  yo.update(document.querySelector('#el-content'), yo`<div class="pane" id="el-content">
    <div class="dats">
      <div class="sf-actions">
        <button class="btn btn-default" onclick=${onClickNewDat}>New Dat Site</button>
      </div>
      ${renderArchives(archives, { showHead: true, onToggleSharing, renderEmpty })}
    </div>
  </div>`)
}

function renderEmpty () {
  return yo`<div class="fl-row archive"><div class="fl-name">You have not created any sites yet!</div></div>`
}

// event handlers
// =

function onClickNewDat (e) {
  beaker.dat.createNewArchive((err, key) => {
    window.location = 'view-dat://'+key
  })
}

function onToggleSharing (archive) {
  if (archive.isSharing) {
    beaker.dat.unswarm(archive.key)
    render()
  } else {
    beaker.dat.swarm(archive.key)
    render()
  }
}

function onUpdateArchive (update) {
  console.log('update', update)
  if (archives) {
    // find the archive being updated
    var archive = archives.find(a => a.key == update.key)
    if (archive) {
      // patch the archive
      for (var k in update)
        archive[k] = update[k]
    } else {
      // add to list
      archives.push(update)
    }
    render()
  }
}

function onUpdatePeers ({ key, peers }) {
  if (archives) {
    // find the archive being updated
    var archive = archives.find(a => a.key == key)
    if (archive)
      archive.peers = peers // update
    render()
  }
}<commit_msg>fix: only render dats in beaker:dats that were originally requested<commit_after>import * as yo from 'yo-yo'
import { niceDate } from '../../lib/time'
import { ucfirst } from '../../lib/strings'
import { renderArchives, entriesListToTree } from '../com/files-list'
import prettyBytes from 'pretty-bytes'
import emitStream from 'emit-stream'
import multicb from 'multicb'

// globals
// =

// list of archives
var archives = []

// event emitter
var archivesEvents


// exported API
// =

export function setup () {
}

export function show () {
  document.title = 'My Dat Sites'
  // fetch archives
  beaker.dat.ownedArchives((err, subscribed) => {
    archives = subscribed
    archives.sort(archiveSortFn)
    console.log(archives)
    render()
  })

  // start event stream and register events
  if (!archivesEvents) {
    archivesEvents = emitStream(beaker.dat.archivesEventStream())
    archivesEvents.on('update-archive', onUpdateArchive)
    archivesEvents.on('update-peers', onUpdatePeers)
  }
}

export function hide () {
  archives = null
}

// internal methods
// =

function archiveSortFn (a, b) {
  return b.mtime - a.mtime
}

// rendering
// =

function render () {
  // render view
  yo.update(document.querySelector('#el-content'), yo`<div class="pane" id="el-content">
    <div class="dats">
      <div class="sf-actions">
        <button class="btn btn-default" onclick=${onClickNewDat}>New Dat Site</button>
      </div>
      ${renderArchives(archives, { showHead: true, onToggleSharing, renderEmpty })}
    </div>
  </div>`)
}

function renderEmpty () {
  return yo`<div class="fl-row archive"><div class="fl-name">You have not created any sites yet!</div></div>`
}

// event handlers
// =

function onClickNewDat (e) {
  beaker.dat.createNewArchive((err, key) => {
    window.location = 'view-dat://'+key
  })
}

function onToggleSharing (archive) {
  if (archive.isSharing) {
    beaker.dat.unswarm(archive.key)
    render()
  } else {
    beaker.dat.swarm(archive.key)
    render()
  }
}

function onUpdateArchive (update) {
  console.log('update', update)
  if (archives) {
    // find the archive being updated
    var archive = archives.find(a => a.key == update.key)
    if (archive) {
      // patch the archive
      for (var k in update)
        archive[k] = update[k]
      render()
    }
  }
}

function onUpdatePeers ({ key, peers }) {
  if (archives) {
    // find the archive being updated
    var archive = archives.find(a => a.key == key)
    if (archive)
      archive.peers = peers // update
    render()
  }
}
{'fraction_non_alphanumeric': 0.14713949843260188, 'fraction_numerical': 0.0, 'mean_word_length': 3.054805401111994, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1760279', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1652, 'n_words': 414}