Files: 567e4d92c4e33c4df6f3dbaee5e7a29e2e93eebb / app / html / thread.js
2167 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 | return threadView |
55 | } |
56 | } |
57 | |
58 | function buildChunkedMessages (messagesObs) { |
59 | return computed(messagesObs, msgs => { |
60 | var chunkedMessages = MutantArray() |
61 | |
62 | var _chunk = null |
63 | var _lastMsg = null |
64 | |
65 | msgs.forEach(msg => { |
66 | if (!_lastMsg || !isSameAuthor(_lastMsg, msg)) { createNewChunk(msg) } else { _chunk.push(msg) } |
67 | |
68 | _lastMsg = msg |
69 | }) |
70 | |
71 | function createNewChunk (msg) { |
72 | const newChunk = MutantArray() |
73 | newChunk.push(msg) |
74 | chunkedMessages.push(newChunk) |
75 | _chunk = newChunk |
76 | } |
77 | |
78 | return chunkedMessages |
79 | }) |
80 | } |
81 | |
82 | function isSameAuthor (msgA, msgB) { |
83 | // TODO (mix) use lodash/get |
84 | return msgA.value.author === msgB.value.author |
85 | } |
86 |
Built with git-ssb-web