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