git ssb

16+

Dominic / patchbay



Tree: 1c2926f12ff8e365e1d469f23180b50d50b9c749

Files: 1c2926f12ff8e365e1d469f23180b50d50b9c749 / app / page / search.js

2037 bytesRaw
1const nest = require('depnest')
2const { h, Struct, Value } = require('mutant')
3const pull = require('pull-stream')
4const Scroller = require('pull-scroll')
5const TextNodeSearcher = require('text-node-searcher')
6
7exports.gives = nest('app.page.search')
8
9exports.needs = nest({
10 'app.html.filter': 'first',
11 'app.html.scroller': 'first',
12 'message.html.render': 'first',
13 'sbot.pull.log': 'first',
14 'sbot.pull.stream': 'first'
15})
16
17var whitespace = /\s+/
18
19exports.create = function (api) {
20 return nest('app.page.search', searchPage)
21
22 function searchPage (location) {
23 const query = location.query.trim()
24
25 const search = Struct({
26 fulltext: Struct({
27 isDone: Value(false)
28 }),
29 matches: Value(0)
30 })
31
32 const searchHeader = h('Search', [
33 h('header', h('h1', query))
34 ])
35 const { filterMenu, filterDownThrough, resetFeed } = api.app.html.filter(draw)
36 const { container, content } = api.app.html.scroller({ prepend: [searchHeader, filterMenu] })
37
38 function renderMsg (msg) {
39 var el = api.message.html.render(msg)
40 var queryTerms = query.split(whitespace)
41
42 highlight(el, createOrRegExp(queryTerms))
43 return el
44 }
45
46 function draw () {
47 resetFeed({ container, content })
48
49 // TODO figure out how to step on kinda orderless search results
50 pull(
51 // api.sbot.pull.stream(sbot => next(sbot.search.query, { query, limit: 500 })),
52 api.sbot.pull.stream(sbot => sbot.search.query({ query, limit: 500 })),
53 filterDownThrough(),
54 pull.through(() => search.matches.set(search.matches() + 1)),
55 Scroller(container, content, renderMsg, false, false)
56 )
57 }
58
59 draw()
60
61 container.title = '?' + query
62 return container
63 }
64}
65
66function createOrRegExp (ary) {
67 return new RegExp(ary.map(function (e) {
68 return '\\b' + e + '\\b'
69 }).join('|'), 'i')
70}
71
72function highlight (el, query) {
73 var searcher = new TextNodeSearcher({container: el})
74 searcher.query = query
75 searcher.highlight()
76 return el
77}
78

Built with git-ssb-web