Files: 7e24f761d4772ef1ac78ab33da6743bb7e1d821d / app / html / thread.js
2585 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Array: MutantArray, map, computed, when } = require('mutant') |
3 | const get = require('lodash/get') |
4 | |
5 | exports.gives = nest('app.html.thread') |
6 | |
7 | exports.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 | |
16 | exports.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' : ' -read' |
53 | api.unread.sync.markRead(msg) |
54 | return h('div.msg'+unread, api.message.html.markdown(raw)) |
55 | } |
56 | |
57 | threadView.subject = computed(thread.messages, msgs => { |
58 | return get(msgs, '[0].value.content.subject') |
59 | }) |
60 | return threadView |
61 | } |
62 | } |
63 | |
64 | function buildChunkedMessages (messagesObs) { |
65 | return computed(messagesObs, msgs => { |
66 | var chunkedMessages = MutantArray() |
67 | |
68 | var _chunk = null |
69 | var _lastMsg = null |
70 | |
71 | msgs.forEach(msg => { |
72 | const text = get(msg, 'value.content.text') |
73 | if (!text) return |
74 | |
75 | if (!_lastMsg || !isSameAuthor(_lastMsg, msg)) { |
76 | createNewChunk(msg) |
77 | } else { |
78 | _chunk.push(msg) |
79 | } |
80 | |
81 | _lastMsg = msg |
82 | }) |
83 | |
84 | function createNewChunk (msg) { |
85 | const newChunk = MutantArray() |
86 | newChunk.push(msg) |
87 | chunkedMessages.push(newChunk) |
88 | _chunk = newChunk |
89 | } |
90 | |
91 | return chunkedMessages |
92 | }) |
93 | } |
94 | |
95 | function isSameAuthor (msgA, msgB) { |
96 | // TODO (mix) use lodash/get |
97 | return msgA.value.author === msgB.value.author |
98 | } |
99 | |
100 | |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | |
107 |
Built with git-ssb-web