git ssb

2+

mixmix / ticktack



Tree: 314000baff3b2045fe13f8edf6519b9bdd1779d5

Files: 314000baff3b2045fe13f8edf6519b9bdd1779d5 / app / html / thread.js

2186 bytesRaw
1const nest = require('depnest')
2const { h, Array: MutantArray, map, computed, when } = require('mutant')
3const pull = require('pull-stream')
4const last = require('lodash/last')
5const get = require('lodash/get')
6
7exports.gives = nest('app.html.thread')
8
9exports.needs = nest({
10 'app.sync.goTo': 'first'
11})
12
13exports.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
59function 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
87function 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