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