Files: adca29179dee2177af6fefa725c91581ffef5395 / app / page / blogIndex.js
2808 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Value, resolve } = require('mutant') |
3 | const pull = require('pull-stream') |
4 | |
5 | exports.gives = nest('app.page.blogIndex') |
6 | |
7 | exports.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 | |
21 | exports.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 | function update (soFar, newBlog) { |
55 | soFar.transaction(() => { |
56 | const { timestamp } = newBlog.value |
57 | |
58 | var object = newBlog // Value(newBlog) |
59 | |
60 | // Orders by: time received |
61 | const justOlderPosition = indexOf(soFar, (msg) => newBlog.timestamp > resolve(msg).timestamp) |
62 | |
63 | // Orders by: time published BUT the messagesByType stream streams _by time received_ |
64 | // TODO - we need an index of all blogs otherwise the scroller doesn't work... |
65 | // const justOlderPosition = indexOf(soFar, (msg) => timestamp > resolve(msg).value.timestamp) |
66 | |
67 | if (justOlderPosition > -1) { |
68 | soFar.insert(object, justOlderPosition) |
69 | } else { |
70 | soFar.push(object) |
71 | } |
72 | }) |
73 | } |
74 | |
75 | function render (blog) { |
76 | const { recps, channel } = blog.value.content |
77 | var onClick |
78 | if (channel && !recps) { onClick = (ev) => api.history.sync.push(Object.assign({}, blog, { page: 'blogShow' })) } |
79 | return api.app.html.blogCard(blog, { onClick }) |
80 | } |
81 | } |
82 | |
83 | function indexOf (array, fn) { |
84 | for (var i = 0; i < array.getLength(); i++) { |
85 | if (fn(array.get(i))) { |
86 | return i |
87 | } |
88 | } |
89 | return -1 |
90 | } |
91 |
Built with git-ssb-web