Files: c2515fc07d5df937b0ab574f42b22ed788831215 / message / html / backlinks.js
1420 bytesRaw
1 | const h = require('mutant/h') |
2 | const map = require('mutant/map') |
3 | const computed = require('mutant/computed') |
4 | const when = require('mutant/when') |
5 | const nest = require('depnest') |
6 | const ref = require('ssb-ref') |
7 | |
8 | exports.needs = nest({ |
9 | 'message.obs.backlinks': 'first', |
10 | 'message.obs.name': 'first', |
11 | 'message.async.name': 'first', |
12 | 'sbot.sync.cache': 'first' |
13 | }) |
14 | |
15 | exports.gives = nest('message.html.backlinks') |
16 | |
17 | exports.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 | |
37 | function onlyReferences (backlinks, msg) { |
38 | return backlinks.filter(link => link.root !== msg.key && !includeOrEqual(link.branch, msg.key)) |
39 | } |
40 | |
41 | function hasItems (items, msg) { |
42 | return (items && items.length) |
43 | } |
44 | |
45 | function 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