Files: e73bff8390f085ab1944c5d9ac4c514cbbacf355 / app / html / thread.js
2351 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Array: MutantArray, map, computed, when } = require('mutant') |
3 | const pull = require('pull-stream') |
4 | const last = require('lodash/last') |
5 | const get = require('lodash/get') |
6 | |
7 | exports.gives = nest('app.html.thread') |
8 | |
9 | exports.needs = nest({ |
10 | 'about.html.image': 'first', |
11 | 'feed.obs.thread': 'first', |
12 | 'keys.sync.id': 'first', |
13 | 'message.html.markdown': 'first' |
14 | }) |
15 | |
16 | exports.create = (api) => { |
17 | return nest('app.html.thread', thread) |
18 | |
19 | function thread (id) { |
20 | // location here can expected to be: { page: 'home' } |
21 | |
22 | |
23 | var myId = api.keys.sync.id() |
24 | |
25 | const thread = api.feed.obs.thread(id) |
26 | const chunkedMessages = buildChunkedMessages(thread.messages) |
27 | |
28 | const threadView = h('Thread', |
29 | map(chunkedMessages, chunk => { |
30 | const author = computed([chunk], chunk => get(chunk, '[0].value.author')) |
31 | |
32 | return author() === myId |
33 | ? h('div.my-chunk', [ |
34 | h('div.avatar'), |
35 | h('div.msgs', map(chunk, msg => { |
36 | return h('div.msg-row', [ |
37 | h('div.spacer'), |
38 | message(msg) |
39 | ]) |
40 | })) |
41 | ]) |
42 | : h('div.other-chunk', [ |
43 | h('div.avatar', when(author, api.about.html.image(author()))), |
44 | h('div.msgs', map(chunk, msg => { |
45 | return h('div.msg-row', [ |
46 | message(msg), |
47 | h('div.spacer') |
48 | ]) |
49 | })) |
50 | ]) |
51 | }) |
52 | ) |
53 | |
54 | function message (msg) { |
55 | const raw = get(msg, 'value.content.text') |
56 | |
57 | return h('div.msg', api.message.html.markdown(raw)) |
58 | } |
59 | |
60 | return threadView |
61 | } |
62 | } |
63 | |
64 | function buildChunkedMessages (messagesObs) { |
65 | return computed(messagesObs, msgs => { |
66 | var chunkedMessages = MutantArray() |
67 | |
68 | var _chunk = null |
69 | var _lastMsg = null |
70 | |
71 | msgs.forEach(msg => { |
72 | if (!_lastMsg || !isSameAuthor(_lastMsg, msg)) |
73 | createNewChunk(msg) |
74 | else |
75 | _chunk.push(msg) |
76 | |
77 | _lastMsg = msg |
78 | }) |
79 | |
80 | function createNewChunk (msg) { |
81 | const newChunk = MutantArray() |
82 | newChunk.push(msg) |
83 | chunkedMessages.push(newChunk) |
84 | _chunk = newChunk |
85 | } |
86 | |
87 | return chunkedMessages |
88 | }) |
89 | } |
90 | |
91 | function isSameAuthor (msgA, msgB) { |
92 | // TODO (mix) use lodash/get |
93 | return msgA.value.author === msgB.value.author |
94 | } |
95 | |
96 | |
97 |
Built with git-ssb-web