Files: 8a51c32a71a79eaf5aacc86fda73831f1a6356dc / app / page / query.js
2824 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Value, computed, when } = require('mutant') |
3 | const Scroller = require('mutant-scroll') |
4 | const next = require('pull-next-query') |
5 | const json5 = require('json5') |
6 | |
7 | exports.gives = nest({ |
8 | 'app.html.menuItem': true, |
9 | 'app.page.query': true |
10 | }) |
11 | |
12 | exports.needs = nest({ |
13 | 'app.sync.goTo': 'first', |
14 | 'message.html.render': 'first', |
15 | 'sbot.pull.stream': 'first' |
16 | }) |
17 | |
18 | // TODO ?? extract a module patchbay-devtools ? |
19 | exports.create = function (api) { |
20 | return nest({ |
21 | 'app.html.menuItem': menuItem, |
22 | 'app.page.query': queryPage |
23 | }) |
24 | |
25 | function menuItem () { |
26 | return h('a', { |
27 | style: { order: 1 }, |
28 | 'ev-click': () => api.app.sync.goTo({ page: 'query' }) |
29 | }, '/query') |
30 | } |
31 | |
32 | function queryPage (location) { |
33 | const input = Value() |
34 | const error = computed(input, i => { |
35 | try { |
36 | var query = json5.parse(i) |
37 | } catch (err) { |
38 | // console.error(err) |
39 | return err |
40 | } |
41 | if (!Array.isArray(query)) return |
42 | if (!query.map(q => Object.keys(q)[0]).every(q => ['$filter', '$map', '$reduce'].includes(q))) return |
43 | activateQuery() |
44 | }) |
45 | const query = Value(json5.parse(initial())) |
46 | |
47 | const activateQuery = () => query.set(json5.parse(input())) |
48 | |
49 | return h('Query', { title: '/query' }, [ |
50 | h('section.query', [ |
51 | h('textarea', { 'ev-input': ev => input.set(ev.target.value), value: initial() }), |
52 | h('button', { |
53 | className: when(error, '', '-primary'), |
54 | disabled: when(error, 'disabled'), |
55 | 'ev-click': activateQuery |
56 | }, 'Go!') |
57 | ]), |
58 | h('section.output', [ |
59 | computed(query, query => { |
60 | return Scroller({ |
61 | streamToBottom: source(query), |
62 | render: msg => h('pre', JSON.stringify(msg, null, 2)), |
63 | comparer: (a, b) => { |
64 | if (a && b && a.key && b.key) return a.key === b.key |
65 | return a === b |
66 | } |
67 | }) |
68 | }) |
69 | ]) |
70 | ]) |
71 | } |
72 | |
73 | function source (query) { |
74 | const opts = { |
75 | query, |
76 | reverse: true, |
77 | limit: 50 |
78 | } |
79 | |
80 | return api.sbot.pull.stream(server => { |
81 | return next(server.query.read, opts, ['value', 'timestamp']) |
82 | }) |
83 | } |
84 | } |
85 | |
86 | function initial () { |
87 | return `[{ |
88 | $filter: { |
89 | value: { |
90 | timestamp: {$gt: 0}, |
91 | content: { |
92 | type: 'post' |
93 | } |
94 | } |
95 | } |
96 | }, { |
97 | $map: { |
98 | author: ['value', 'author'], |
99 | text: ['value', 'content', 'text'], |
100 | ts: { |
101 | received: ['timestamp'], |
102 | asserted: ['value', 'timestamp'] |
103 | } |
104 | } |
105 | }] |
106 | |
107 | // $filter - used to prune down results. This must be the first entry, as ssb-query uses it to determine the most optimal index for fast lookup. |
108 | |
109 | // $map - optional, can be used to pluck data you want out. Doing this reduces the amount of data sent over muxrpc, which speeds up loading |
110 | ` |
111 | } |
112 |
Built with git-ssb-web