Files: 67583b0e9605f9e2c2a95ff125a23e8a1cca8953 / app / page / blogIndex.js
2940 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Value, Array: MutantArray, 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 | 'blog.sync.isBlog': 'first', |
13 | 'feed.pull.public': 'first', |
14 | 'feed.pull.type': 'first', |
15 | 'history.sync.push': 'first', |
16 | 'keys.sync.id': 'first', |
17 | 'message.sync.isBlocked': 'first', |
18 | 'translations.sync.strings': 'first', |
19 | 'unread.sync.isUnread': 'first' |
20 | }) |
21 | |
22 | exports.create = (api) => { |
23 | var blogsCache = MutantArray() |
24 | |
25 | return nest('app.page.blogIndex', function (location) { |
26 | // location here can expected to be: { page: 'blogIndex'} |
27 | |
28 | var strings = api.translations.sync.strings() |
29 | |
30 | var blogs = api.app.html.scroller({ |
31 | classList: ['content'], |
32 | prepend: api.app.html.topNav(location), |
33 | // stream: api.feed.pull.public, |
34 | stream: api.feed.pull.type('blog'), |
35 | filter: () => pull( |
36 | // pull.filter(api.blog.sync.isBlog), |
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: blogsCache, |
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 | const index = indexOf(soFar, (blog) => newBlog.key === resolve(blog).key) |
61 | // if blog already in cache, not needed again |
62 | if (index >= 0) return |
63 | |
64 | // Orders by: time received |
65 | const justOlderPosition = indexOf(soFar, (msg) => newBlog.timestamp > resolve(msg).timestamp) |
66 | |
67 | // Orders by: time published BUT the messagesByType stream streams _by time received_ |
68 | // TODO - we need an index of all blogs otherwise the scroller doesn't work... |
69 | // const justOlderPosition = indexOf(soFar, (msg) => timestamp > resolve(msg).value.timestamp) |
70 | |
71 | if (justOlderPosition > -1) { |
72 | soFar.insert(object, justOlderPosition) |
73 | } else { |
74 | soFar.push(object) |
75 | } |
76 | }) |
77 | } |
78 | |
79 | function render (blog) { |
80 | const { recps, channel } = blog.value.content |
81 | var onClick |
82 | if (channel && !recps) { onClick = (ev) => api.history.sync.push(Object.assign({}, blog, { page: 'blogShow' })) } |
83 | return api.app.html.blogCard(blog, { onClick }) |
84 | } |
85 | } |
86 | |
87 | function indexOf (array, fn) { |
88 | for (var i = 0; i < array.getLength(); i++) { |
89 | if (fn(array.get(i))) { |
90 | return i |
91 | } |
92 | } |
93 | return -1 |
94 | } |
95 |
Built with git-ssb-web