Files: 6ceaa46c3d79b1182d2dddc9a765738453b4a190 / app / html / thread.js
2287 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 | }) |
13 | |
14 | exports.create = (api) => { |
15 | return nest('app.html.thread', thread) |
16 | |
17 | function thread (root) { |
18 | const myId = api.keys.sync.id() |
19 | const thread = api.feed.obs.thread(root) |
20 | const chunkedMessages = buildChunkedMessages(thread.messages) |
21 | |
22 | const threadView = h('Thread', |
23 | map(chunkedMessages, chunk => { |
24 | const author = computed([chunk], chunk => get(chunk, '[0].value.author')) |
25 | |
26 | return author() === myId |
27 | ? h('div.my-chunk', [ |
28 | h('div.avatar'), |
29 | h('div.msgs', map(chunk, msg => { |
30 | return h('div.msg-row', [ |
31 | h('div.spacer'), |
32 | message(msg) |
33 | ]) |
34 | })) |
35 | ]) |
36 | : h('div.other-chunk', [ |
37 | h('div.avatar', when(author, api.about.html.image(author()))), |
38 | h('div.msgs', map(chunk, msg => { |
39 | return h('div.msg-row', [ |
40 | message(msg), |
41 | h('div.spacer') |
42 | ]) |
43 | })) |
44 | ]) |
45 | }) |
46 | ) |
47 | |
48 | function message (msg) { |
49 | const raw = get(msg, 'value.content.text') |
50 | |
51 | return h('div.msg', api.message.html.markdown(raw)) |
52 | } |
53 | |
54 | threadView.subject = computed(thread.messages, msgs => { |
55 | return get(msgs, '[0].value.content.subject') |
56 | }) |
57 | return threadView |
58 | } |
59 | } |
60 | |
61 | function buildChunkedMessages (messagesObs) { |
62 | return computed(messagesObs, msgs => { |
63 | var chunkedMessages = MutantArray() |
64 | |
65 | var _chunk = null |
66 | var _lastMsg = null |
67 | |
68 | msgs.forEach(msg => { |
69 | if (!_lastMsg || !isSameAuthor(_lastMsg, msg)) { createNewChunk(msg) } else { _chunk.push(msg) } |
70 | |
71 | _lastMsg = msg |
72 | }) |
73 | |
74 | function createNewChunk (msg) { |
75 | const newChunk = MutantArray() |
76 | newChunk.push(msg) |
77 | chunkedMessages.push(newChunk) |
78 | _chunk = newChunk |
79 | } |
80 | |
81 | return chunkedMessages |
82 | }) |
83 | } |
84 | |
85 | function isSameAuthor (msgA, msgB) { |
86 | // TODO (mix) use lodash/get |
87 | return msgA.value.author === msgB.value.author |
88 | } |
89 |
Built with git-ssb-web