Files: f736acde0d02447833cfd0c3a021d2d560e5c9a6 / app / html / thread.js
2222 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 | const myId = api.keys.sync.id() |
21 | const thread = api.feed.obs.thread(id) |
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 | |
53 | return h('div.msg', api.message.html.markdown(raw)) |
54 | } |
55 | |
56 | return threadView |
57 | } |
58 | } |
59 | |
60 | function buildChunkedMessages (messagesObs) { |
61 | return computed(messagesObs, msgs => { |
62 | var chunkedMessages = MutantArray() |
63 | |
64 | var _chunk = null |
65 | var _lastMsg = null |
66 | |
67 | msgs.forEach(msg => { |
68 | if (!_lastMsg || !isSameAuthor(_lastMsg, msg)) { createNewChunk(msg) } else { _chunk.push(msg) } |
69 | |
70 | _lastMsg = msg |
71 | }) |
72 | |
73 | function createNewChunk (msg) { |
74 | const newChunk = MutantArray() |
75 | newChunk.push(msg) |
76 | chunkedMessages.push(newChunk) |
77 | _chunk = newChunk |
78 | } |
79 | |
80 | return chunkedMessages |
81 | }) |
82 | } |
83 | |
84 | function isSameAuthor (msgA, msgB) { |
85 | // TODO (mix) use lodash/get |
86 | return msgA.value.author === msgB.value.author |
87 | } |
88 |
Built with git-ssb-web