git ssb

2+

mixmix / ticktack



Tree: e39763f409d19a3fb6f6c9052dccd36946f8be25

Files: e39763f409d19a3fb6f6c9052dccd36946f8be25 / app / html / thread.js

2616 bytesRaw
1const nest = require('depnest')
2const { h, Array: MutantArray, map, computed, when } = require('mutant')
3const get = require('lodash/get')
4
5exports.gives = nest('app.html.thread')
6
7exports.needs = nest({
8 'about.html.avatar': 'first',
9 'feed.obs.thread': 'first',
10 'keys.sync.id': 'first',
11 'message.html.markdown': 'first',
12 'unread.sync.markRead': 'first',
13 'unread.sync.isUnread': 'first'
14})
15
16exports.create = (api) => {
17 return nest('app.html.thread', thread)
18
19 function thread (root) {
20 const myId = api.keys.sync.id()
21 const thread = api.feed.obs.thread(root)
22 const chunkedMessages = buildChunkedMessages(thread.messages)
23
24 const threadView = h('Thread',
25 map(chunkedMessages, chunk => {
26 const author = computed([chunk], chunk => get(chunk, '[0].value.author'))
27
28 return author() === myId
29 ? h('div.my-chunk', [
30 h('div.avatar'),
31 h('div.msgs', map(chunk, msg => {
32 return h('div.msg-row', [
33 h('div.spacer'),
34 message(msg)
35 ])
36 }))
37 ])
38 : h('div.other-chunk', [
39 h('div.avatar', when(author, api.about.html.avatar(author()))),
40 h('div.msgs', map(chunk, msg => {
41 return h('div.msg-row', [
42 message(msg),
43 h('div.spacer')
44 ])
45 }))
46 ])
47 })
48 )
49
50 function message (msg) {
51 const raw = get(msg, 'value.content.text')
52 var unread = api.unread.sync.isUnread(msg) ? ' -unread' : ''
53 console.log('unread?', unread, msg)
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