Files: 17df3c936c4c4573ec97072a96be633ed3ae1c2b / app / page / blogIndex.js
3262 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Value, Array: MutantArray, resolve } = require('mutant') |
3 | const Scroller = require('mutant-scroll') |
4 | const pull = require('pull-stream') |
5 | const Next = require('pull-next') |
6 | |
7 | exports.gives = nest('app.page.blogIndex') |
8 | |
9 | exports.needs = nest({ |
10 | 'app.html.blogCard': 'first', |
11 | 'app.html.topNav': 'first', |
12 | // 'app.html.scroller': 'first', |
13 | 'app.html.sideNav': 'first', |
14 | 'blog.sync.isBlog': 'first', |
15 | 'feed.pull.public': 'first', |
16 | 'feed.pull.type': 'first', |
17 | 'history.sync.push': 'first', |
18 | 'keys.sync.id': 'first', |
19 | 'message.sync.isBlocked': 'first', |
20 | 'translations.sync.strings': 'first', |
21 | 'unread.sync.isUnread': 'first' |
22 | }) |
23 | |
24 | exports.create = (api) => { |
25 | var blogsCache = MutantArray() |
26 | |
27 | return nest('app.page.blogIndex', function (location) { |
28 | // location here can expected to be: { page: 'blogIndex'} |
29 | |
30 | var strings = api.translations.sync.strings() |
31 | |
32 | // const filter = () => pull( |
33 | // pull.filter(api.blog.sync.isBlog), // isBlog or Plog? |
34 | // pull.filter(msg => !msg.value.content.root), // show only root messages |
35 | // pull.filter(msg => !api.message.sync.isBlocked(msg)) // this is already in feed.pull.type |
36 | // ) |
37 | |
38 | // stream: api.feed.pull.public, // for post + blog type |
39 | |
40 | const streamToTop = pull( |
41 | // next(stream, { live: true, reverse: false, old: false, limit: 100, property: indexProperty }), |
42 | api.feed.pull.type('blog')({ live: true, reverse: false, old: false }), |
43 | // filter() |
44 | ) |
45 | |
46 | |
47 | const streamToBottom = pull( |
48 | Next |
49 | api.feed.pull.type('blog')({ live: false, reverse: true }), |
50 | // filter() |
51 | |
52 | ) |
53 | |
54 | var blogs = Scroller({ |
55 | classList: ['content'], |
56 | prepend: api.app.html.topNav(location), |
57 | streamToTop, |
58 | streamToBottom, |
59 | updateTop: update, |
60 | updateBottom: update, |
61 | store: blogsCache, |
62 | render |
63 | }) |
64 | |
65 | return h('Page -blogIndex', {title: strings.home}, [ |
66 | api.app.html.sideNav(location), |
67 | blogs |
68 | ]) |
69 | }) |
70 | |
71 | function update (soFar, newBlog) { |
72 | soFar.transaction(() => { |
73 | var object = newBlog // Value(newBlog) |
74 | |
75 | const index = indexOf(soFar, (blog) => newBlog.key === resolve(blog).key) |
76 | // if blog already in cache, not needed again |
77 | if (index >= 0) return |
78 | |
79 | // Orders by: time received |
80 | const justOlderPosition = indexOf(soFar, (msg) => newBlog.timestamp > resolve(msg).timestamp) |
81 | |
82 | // Orders by: time published BUT the messagesByType stream streams _by time received_ |
83 | // TODO - we need an index of all blogs otherwise the scroller doesn't work... |
84 | // const justOlderPosition = indexOf(soFar, (msg) => newBlog.value.timestamp > resolve(msg).value.timestamp) |
85 | |
86 | if (justOlderPosition > -1) { |
87 | soFar.insert(object, justOlderPosition) |
88 | } else { |
89 | soFar.push(object) |
90 | } |
91 | }) |
92 | } |
93 | |
94 | function render (blog) { |
95 | const { recps, channel } = blog.value.content |
96 | var onClick |
97 | if (channel && !recps) { onClick = (ev) => api.history.sync.push(Object.assign({}, blog, { page: 'blogShow' })) } |
98 | return api.app.html.blogCard(blog, { onClick }) |
99 | } |
100 | } |
101 | |
102 | function indexOf (array, fn) { |
103 | for (var i = 0; i < array.getLength(); i++) { |
104 | if (fn(array.get(i))) { |
105 | return i |
106 | } |
107 | } |
108 | return -1 |
109 | } |
110 |
Built with git-ssb-web