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