Files: 00db2720562a9536989a440889c17cb7fdad9c30 / app / html / thread.js
2480 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.image': '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.image(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 | if (!_lastMsg || !isSameAuthor(_lastMsg, msg)) { createNewChunk(msg) } else { _chunk.push(msg) } |
73 | |
74 | _lastMsg = msg |
75 | }) |
76 | |
77 | function createNewChunk (msg) { |
78 | const newChunk = MutantArray() |
79 | newChunk.push(msg) |
80 | chunkedMessages.push(newChunk) |
81 | _chunk = newChunk |
82 | } |
83 | |
84 | return chunkedMessages |
85 | }) |
86 | } |
87 | |
88 | function isSameAuthor (msgA, msgB) { |
89 | // TODO (mix) use lodash/get |
90 | return msgA.value.author === msgB.value.author |
91 | } |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | |
100 |
Built with git-ssb-web