Files: 119e5a731640d722b01b4aa15a4ec30a09b17285 / app / html / thread.js
2221 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 (id) { |
18 | // location here can expected to be: { page: 'home' } |
19 | |
20 | var myId = api.keys.sync.id() |
21 | |
22 | const thread = api.feed.obs.thread(id) |
23 | const chunkedMessages = buildChunkedMessages(thread.messages) |
24 | |
25 | const threadView = h('Thread', |
26 | map(chunkedMessages, chunk => { |
27 | const author = computed([chunk], chunk => get(chunk, '[0].value.author')) |
28 | |
29 | return author() === myId |
30 | ? h('div.my-chunk', [ |
31 | h('div.avatar'), |
32 | h('div.msgs', map(chunk, msg => { |
33 | return h('div.msg-row', [ |
34 | h('div.spacer'), |
35 | message(msg) |
36 | ]) |
37 | })) |
38 | ]) |
39 | : h('div.other-chunk', [ |
40 | h('div.avatar', when(author, api.about.html.image(author()))), |
41 | h('div.msgs', map(chunk, msg => { |
42 | return h('div.msg-row', [ |
43 | message(msg), |
44 | h('div.spacer') |
45 | ]) |
46 | })) |
47 | ]) |
48 | }) |
49 | ) |
50 | |
51 | function message (msg) { |
52 | const raw = get(msg, 'value.content.text') |
53 | |
54 | return h('div.msg', api.message.html.markdown(raw)) |
55 | } |
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