git ssb

16+

Dominic / patchbay



Tree: 77ea0a465101140a19943d0f66eabde2b5a04bbf

Files: 77ea0a465101140a19943d0f66eabde2b5a04bbf / app / page / query.js

2502 bytesRaw
1const nest = require('depnest')
2const { h, Value, computed, when } = require('mutant')
3const Scroller = require('mutant-scroll')
4const next = require('pull-next-query')
5const json5 = require('json5')
6
7exports.gives = nest({
8 'app.html.menuItem': true,
9 'app.page.query': true
10})
11
12exports.needs = nest({
13 'message.html.render': 'first',
14 'sbot.pull.stream': 'first'
15})
16
17// TODO ?? extract a module patchbay-devtools ?
18exports.create = function (api) {
19 return nest({
20 'app.html.menuItem': menuItem,
21 'app.page.query': queryPage
22 })
23
24 function menuItem () {
25 return h('a', {
26 style: { order: 1 },
27 'ev-click': () => api.app.sync.goTo({ page: 'query' })
28 }, '/query')
29 }
30
31 function queryPage (location) {
32 const input = Value()
33 const error = computed(input, i => {
34 try {
35 var query = json5.parse(i)
36 } catch (err) {
37 // console.error(err)
38 return err
39 }
40 if (!Array.isArray(query)) return
41 if (!query.map(q => Object.keys(q)[0]).every(q => ['$filter', '$map', '$reduce'].includes(q))) return
42 activateQuery()
43 })
44 const query = Value(json5.parse(initial()))
45
46 const activateQuery = () => query.set(json5.parse(input()))
47
48 return h('Query', { title: '/query' }, [
49 h('section.query', [
50 h('textarea', { 'ev-input': ev => input.set(ev.target.value), value: initial() }),
51 h('button', {
52 className: when(error, '', '-primary'),
53 disabled: when(error, 'disabled'),
54 'ev-click': activateQuery
55 }, 'Go!')
56 ]),
57 h('section.output', [
58 computed(query, query => {
59 return Scroller({
60 streamToBottom: source(query),
61 render: msg => h('pre', JSON.stringify(msg, null, 2)),
62 comparer: (a, b) => {
63 if (a && b && a.key && b.key) return a.key === b.key
64 return a === b
65 }
66 })
67 })
68 ])
69 ])
70 }
71
72 function source (query) {
73 const opts = {
74 query,
75 reverse: true,
76 limit: 50
77 }
78
79 return api.sbot.pull.stream(server => {
80 return next(server.query.read, opts, ['value', 'timestamp'])
81 })
82 }
83}
84
85function initial () {
86 return `[{
87 $filter: {
88 value: {
89 timestamp: {$gt: 0},
90 content: {
91 type: 'post'
92 }
93 }
94 }
95}, {
96 $map: {
97 receivedTimestamp: ['timestamp'],
98 assertedTimestamp: ['value', 'timestamp'],
99 author: ['value', 'author'],
100 content: ['value', 'content']
101 }
102}]
103`
104}
105

Built with git-ssb-web