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