git ssb

2+

mixmix / ticktack



Tree: fea2881ae175c88bac926fcff65d32598742e9a1

Files: fea2881ae175c88bac926fcff65d32598742e9a1 / app / html / comments.js

2183 bytesRaw
1const nest = require('depnest')
2const { h, map, Struct, computed, throttle } = require('mutant')
3const get = require('lodash/get')
4
5exports.gives = nest('app.html.comments')
6
7exports.needs = nest({
8 'message.html.comment': 'first',
9 'message.html.compose': 'first',
10 'translations.sync.strings': 'first'
11})
12
13exports.create = (api) => {
14 return nest('app.html.comments', comments)
15
16 function comments (thread) {
17 const strings = api.translations.sync.strings()
18 const { messages, channel, lastId: branch } = thread
19
20 // TODO - move this up into Patchcore
21 const messagesTree = computed(throttle(messages, 200), msgs => {
22 return msgs
23 .filter(msg => forkOf(msg) === undefined) // exclude nested replies / forks
24 .filter(msg => msg.value.content.root) // exclude root message / blog
25 .map(comment => {
26 const nestedReplies = msgs.filter(msg => forkOf(msg) === comment.key)
27
28 return Struct({
29 comment: comment,
30 replies: nestedReplies
31 })
32 // return Object.assign({}, comment, { replies: nestedReplies })
33 })
34 })
35
36 const root = computed(messages, ary => ary[0].key)
37
38 const meta = {
39 type: 'post',
40 root,
41 branch,
42 channel
43 }
44 // const twoComposers = computed(messages, messages => {
45 // return messages.length > 5
46 // })
47 const { compose } = api.message.html
48
49 const feedIdsInThread = computed(thread.messages, msgs => {
50 return msgs.map(m => m.value.author)
51 })
52
53 return h('Comments', [
54 // when(twoComposers, compose({ meta, shrink: true, canAttach: false })),
55 map(
56 messagesTree,
57 msg => api.message.html.comment({ comment: msg.comment, replies: msg.replies, branch }),
58 {
59 comparer: (a, b) => {
60 if (a === undefined || b === undefined) return false
61
62 return a.comment() === b.comment() && a.replies().length === b.replies().length
63 }
64 }
65 ),
66 compose({ meta, feedIdsInThread, shrink: false, canAttach: true, placeholder: strings.writeComment })
67 ])
68 }
69}
70
71function forkOf (msg) {
72 return get(msg, 'value.content.fork')
73}
74

Built with git-ssb-web