git ssb

2+

mixmix / ticktack



Tree: aa4c25f3c365e275c42b08f8e34ef4e6c2b66d93

Files: aa4c25f3c365e275c42b08f8e34ef4e6c2b66d93 / app / page / blogIndex.js

2821 bytesRaw
1const nest = require('depnest')
2const { h, Value, resolve } = require('mutant')
3const pull = require('pull-stream')
4
5exports.gives = nest('app.page.blogIndex')
6
7exports.needs = nest({
8 'app.html.blogCard': 'first',
9 'app.html.topNav': 'first',
10 'app.html.scroller': 'first',
11 'app.html.sideNav': 'first',
12 // 'feed.pull.public': 'first',
13 'feed.pull.type': 'first',
14 'history.sync.push': 'first',
15 'keys.sync.id': 'first',
16 'message.sync.isBlocked': 'first',
17 'translations.sync.strings': 'first',
18 'unread.sync.isUnread': 'first'
19})
20
21exports.create = (api) => {
22 return nest('app.page.blogIndex', function (location) {
23 // location here can expected to be: { page: 'blogIndex'}
24
25 var strings = api.translations.sync.strings()
26
27 var blogs = api.app.html.scroller({
28 classList: ['content'],
29 prepend: api.app.html.topNav(location),
30 // stream: api.feed.pull.public,
31 stream: api.feed.pull.type('blog'),
32 filter: () => pull(
33 // pull.filter(msg => {
34 // const type = msg.value.content.type
35 // return type === 'post' || type === 'blog'
36 // }),
37 pull.filter(msg => !msg.value.content.root), // show only root messages
38 pull.filter(msg => !api.message.sync.isBlocked(msg))
39 ),
40 // FUTURE : if we need better perf, we can add a persistent cache. At the moment this page is fast enough though.
41 // See implementation of app.html.sideNav for example
42 // store: recentMsgCache,
43 updateTop: update,
44 updateBottom: update,
45 render
46 })
47
48 return h('Page -blogIndex', {title: strings.home}, [
49 api.app.html.sideNav(location),
50 blogs
51 ])
52 })
53
54
55 function update (soFar, newBlog) {
56 soFar.transaction(() => {
57 const { timestamp } = newBlog.value
58
59 var object = newBlog // Value(newBlog)
60
61 // Orders by: time received
62 const justOlderPosition = indexOf(soFar, (msg) => newBlog.timestamp > resolve(msg).timestamp)
63
64 // Orders by: time published BUT the messagesByType stream streams _by time received_
65 // TODO - we need an index of all blogs otherwise the scroller doesn't work...
66 // const justOlderPosition = indexOf(soFar, (msg) => timestamp > resolve(msg).value.timestamp)
67
68 if (justOlderPosition > -1) {
69 soFar.insert(object, justOlderPosition)
70 } else {
71 soFar.push(object)
72 }
73 })
74 }
75
76
77 function render (blog) {
78 const { recps, channel } = blog.value.content
79 var onClick
80 if (channel && !recps)
81 onClick = (ev) => api.history.sync.push(Object.assign({}, blog, { page: 'blogShow' }))
82 return api.app.html.blogCard(blog, { onClick })
83 }
84}
85
86function indexOf (array, fn) {
87 for (var i = 0; i < array.getLength(); i++) {
88 if (fn(array.get(i))) {
89 return i
90 }
91 }
92 return -1
93}
94
95

Built with git-ssb-web