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