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