git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: a732bc2a78bd9ce1b2bff429591d2b85f9a86306

Files: a732bc2a78bd9ce1b2bff429591d2b85f9a86306 / message / html / backlinks.js

1420 bytesRaw
1const h = require('mutant/h')
2const map = require('mutant/map')
3const computed = require('mutant/computed')
4const when = require('mutant/when')
5const nest = require('depnest')
6const ref = require('ssb-ref')
7
8exports.needs = nest({
9 'message.obs.backlinks': 'first',
10 'message.obs.name': 'first',
11 'message.async.name': 'first',
12 'sbot.sync.cache': 'first'
13})
14
15exports.gives = nest('message.html.backlinks')
16
17exports.create = function (api) {
18 return nest('message.html.backlinks', function (msg) {
19 if (!ref.isMsg(msg.key)) return []
20 var backlinks = api.message.obs.backlinks(msg.key)
21 var references = computed([backlinks, msg], onlyReferences)
22 return when(computed(references, hasItems),
23 h('MessageBacklinks', [
24 h('header', 'backlinks:'),
25 h('ul', [
26 map(references, (backlink) => {
27 return h('li', [
28 h('a -backlink', { href: backlink.id, title: backlink.id }, api.message.obs.name(backlink.id))
29 ])
30 })
31 ])
32 ])
33 )
34 })
35}
36
37function onlyReferences (backlinks, msg) {
38 return backlinks.filter(link => link.root !== msg.key && !includeOrEqual(link.branch, msg.key))
39}
40
41function hasItems (items, msg) {
42 return (items && items.length)
43}
44
45function includeOrEqual (valueOrArray, item) {
46 if (Array.isArray(valueOrArray)) {
47 return valueOrArray.includes(item)
48 } else {
49 return valueOrArray === item
50 }
51}
52

Built with git-ssb-web