git ssb

2+

mixmix / ticktack



Tree: 162122b46e57bbd1cb6ff6774e6b40502586e89b

Files: 162122b46e57bbd1cb6ff6774e6b40502586e89b / app / html / thread.js

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

Built with git-ssb-web