git ssb

2+

mixmix / ticktack



Tree: 34736f8234e840203c81e10a3b49e1570aa2a801

Files: 34736f8234e840203c81e10a3b49e1570aa2a801 / app / html / thread.js

2604 bytesRaw
1const nest = require('depnest')
2const { h, Array: MutantArray, map, computed, when } = require('mutant')
3const get = require('lodash/get')
4
5// TODO - rename threadPrivate
6exports.gives = nest('app.html.thread')
7
8exports.needs = nest({
9 'about.html.avatar': 'first',
10 'feed.obs.thread': 'first',
11 'keys.sync.id': 'first',
12 'message.html.markdown': 'first',
13 'unread.sync.markRead': 'first',
14 'unread.sync.isUnread': 'first'
15})
16
17exports.create = (api) => {
18 return nest('app.html.thread', thread)
19
20 function thread (root) {
21 const myId = api.keys.sync.id()
22 const thread = api.feed.obs.thread(root)
23 const chunkedMessages = buildChunkedMessages(thread.messages)
24
25 const threadView = h('Thread',
26 map(chunkedMessages, chunk => {
27 const author = computed([chunk], chunk => get(chunk, '[0].value.author'))
28
29 return author() === myId
30 ? h('div.my-chunk', [
31 h('Avatar -small'),
32 h('div.msgs', map(chunk, msg => {
33 return h('div.msg-row', [
34 h('div.spacer'),
35 message(msg)
36 ])
37 }))
38 ])
39 : h('div.other-chunk', [
40 when(author, api.about.html.avatar(author()), 'small'),
41 h('div.msgs', map(chunk, msg => {
42 return h('div.msg-row', [
43 message(msg),
44 h('div.spacer')
45 ])
46 }))
47 ])
48 })
49 )
50
51 function message (msg) {
52 const raw = get(msg, 'value.content.text')
53 var unread = api.unread.sync.isUnread(msg) ? ' -unread' : ' -read'
54 api.unread.sync.markRead(msg)
55 return h('div.msg'+unread, api.message.html.markdown(raw))
56 }
57
58 threadView.subject = computed(thread.messages, msgs => {
59 return get(msgs, '[0].value.content.subject')
60 })
61 return threadView
62 }
63}
64
65function buildChunkedMessages (messagesObs) {
66 return computed(messagesObs, msgs => {
67 var chunkedMessages = MutantArray()
68
69 var _chunk = null
70 var _lastMsg = null
71
72 msgs.forEach(msg => {
73 const text = get(msg, 'value.content.text')
74 if (!text) return
75
76 if (!_lastMsg || !isSameAuthor(_lastMsg, msg)) {
77 createNewChunk(msg)
78 } else {
79 _chunk.push(msg)
80 }
81
82 _lastMsg = msg
83 })
84
85 function createNewChunk (msg) {
86 const newChunk = MutantArray()
87 newChunk.push(msg)
88 chunkedMessages.push(newChunk)
89 _chunk = newChunk
90 }
91
92 return chunkedMessages
93 })
94}
95
96function isSameAuthor (msgA, msgB) {
97 // TODO (mix) use lodash/get
98 return msgA.value.author === msgB.value.author
99}
100
101

Built with git-ssb-web