git ssb

2+

mixmix / ticktack



Tree: 7cf45e7c039b145503518e3560ac8943fa1e3708

Files: 7cf45e7c039b145503518e3560ac8943fa1e3708 / app / page / blogSearch.js

3145 bytesRaw
1const nest = require('depnest')
2const { h, Value, computed, map, when, resolve } = require('mutant')
3const pull = require('pull-stream')
4
5exports.gives = nest('app.page.blogSearch')
6
7exports.needs = nest({
8 'app.html.context': 'first',
9 'app.html.blogCard': 'first',
10 'app.html.blogNav': '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
22exports.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) createStream = api.feed.pull.channel(location.channel)
62
63 var blogs = api.app.html.scroller({
64 classList: ['content'],
65 prepend: [
66 api.app.html.blogNav(location),
67 searchField
68 ],
69 stream: createStream,
70 filter: () => pull(
71 pull.filter(msg => {
72 const type = msg.value.content.type
73 return type === 'post' || type === 'blog'
74 }),
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.context 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.context(location),
87 blogs
88 ])
89 }
90
91
92 function render (blog) {
93 const { recps, channel } = blog.value.content
94 var onClick
95 if (channel && !recps)
96 onClick = (ev) => api.history.sync.push(Object.assign({}, blog, { page: 'blogShow' }))
97
98 return api.app.html.blogCard(blog, { onClick })
99 }
100}
101
102
103

Built with git-ssb-web