Files: 8d7dc1a22c947417b8c5b3b0c73f1d9c7e6cefe9 / app / page / blogSearch.js
3157 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Value, computed, map, when, resolve } = require('mutant') |
3 | const pull = require('pull-stream') |
4 | |
5 | exports.gives = nest('app.page.blogSearch') |
6 | |
7 | exports.needs = nest({ |
8 | 'app.html.sideNav': 'first', |
9 | 'app.html.blogCard': 'first', |
10 | 'app.html.topNav': 'first', |
11 | 'app.html.scroller': 'first', |
12 | 'channel.obs.recent': 'first', |
13 | 'feed.pull.channel': 'first', |
14 | 'feed.pull.public': 'first', |
15 | 'history.sync.push': 'first', |
16 | 'keys.sync.id': 'first', |
17 | 'message.html.channel': 'first', |
18 | 'translations.sync.strings': 'first', |
19 | 'unread.sync.isUnread': 'first' |
20 | }) |
21 | |
22 | exports.create = (api) => { |
23 | return nest('app.page.blogSearch', blogSearch) |
24 | |
25 | function blogSearch (location) { |
26 | // location here can expected to be: { page: 'blogSearch'} |
27 | // OR { page: 'blogSearch', channel: 'scuttlebutt', searchVal: 'scutt'} |
28 | |
29 | var strings = api.translations.sync.strings() |
30 | |
31 | var searchVal = Value(resolve(location.searchVal) || resolve(location.channel) || '') |
32 | var searchResults = computed([api.channel.obs.recent(), searchVal], (channels, val) => { |
33 | if (val.length < 2) return [] |
34 | |
35 | return channels.filter(c => c.toLowerCase().indexOf(val.toLowerCase()) > -1) |
36 | }) |
37 | var searchField = h('div.search', [ |
38 | h('div.input', [ |
39 | h('i.fa.fa-search'), |
40 | h('input', { |
41 | 'ev-input': e => searchVal.set(e.target.value), |
42 | value: searchVal |
43 | }) |
44 | ]), |
45 | when(searchResults, |
46 | h('div.results', map(searchResults, channel => { |
47 | const classList = channel === location.channel |
48 | ? ['-channelActive'] |
49 | : '' |
50 | const newLocation = { |
51 | page: 'blogSearch', |
52 | channel, |
53 | searchVal |
54 | } |
55 | return api.message.html.channel(channel, { classList, location: newLocation }) |
56 | })) |
57 | ) |
58 | ]) |
59 | |
60 | var createStream = api.feed.pull.public |
61 | if (location.channel) { |
62 | createStream = api.feed.pull.channel(location.channel) |
63 | } |
64 | |
65 | var blogs = api.app.html.scroller({ |
66 | classList: ['content'], |
67 | prepend: [ |
68 | api.app.html.topNav(location), |
69 | searchField |
70 | ], |
71 | stream: createStream, |
72 | filter: () => pull( |
73 | pull.filter(msg => { |
74 | const type = msg.value.content.type |
75 | return type === 'post' || type === 'blog' |
76 | }), |
77 | pull.filter(msg => !msg.value.content.root) // show only root messages |
78 | ), |
79 | // FUTURE : if we need better perf, we can add a persistent cache. At the moment this page is fast enough though. |
80 | // See implementation of app.html.sideNav for example |
81 | // store: recentMsgCache, |
82 | // updateTop: updateRecentMsgCache, |
83 | // updateBottom: updateRecentMsgCache, |
84 | render |
85 | }) |
86 | |
87 | return h('Page -blogSearch', {title: strings.home}, [ |
88 | api.app.html.sideNav(location), |
89 | blogs |
90 | ]) |
91 | } |
92 | |
93 | |
94 | function render (blog) { |
95 | const { recps, channel } = blog.value.content |
96 | var onClick |
97 | if (channel && !recps) |
98 | onClick = (ev) => api.history.sync.push(Object.assign({}, blog, { page: 'blogShow' })) |
99 | |
100 | return api.app.html.blogCard(blog, { onClick }) |
101 | } |
102 | } |
103 | |
104 | |
105 |
Built with git-ssb-web