git ssb

16+

Dominic / patchbay



Commit 9753714d647474529a117b3536bc687a81f8aa08

/query : allow initialQuery to be passed in!

mixmix committed on 7/17/2018, 7:53:33 AM
Parent: 9b9ae32751d87f8c75ee8da7d23f22f5b9250a74

Files changed

app/page/query.jschanged
app/page/query.jsView
@@ -37,19 +37,19 @@
3737 } catch (err) {
3838 // console.error(err)
3939 return err
4040 }
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()
41 + if (isValidQuery(query)) activateQuery()
4442 })
45- const query = Value(json5.parse(initial()))
4643
44 + const { initialQuery, initialValue } = getInitialState(location)
45 + const query = Value(initialQuery)
46 +
4747 const activateQuery = () => query.set(json5.parse(input()))
4848
4949 return h('Query', { title: '/query' }, [
5050 h('section.query', [
51- h('textarea', { 'ev-input': ev => input.set(ev.target.value), value: initial() }),
51 + h('textarea', { 'ev-input': ev => input.set(ev.target.value), value: initialValue }),
5252 h('button', {
5353 className: when(error, '', '-primary'),
5454 disabled: when(error, 'disabled'),
5555 'ev-click': activateQuery
@@ -82,10 +82,18 @@
8282 })
8383 }
8484 }
8585
86-function initial () {
87- return `[{
86 +function getInitialState (location) {
87 + const { initialQuery, initialValue } = location
88 + if (isValidQuery(initialQuery)) {
89 + return {
90 + initialQuery,
91 + initialValue: initialValue || json5.stringify(initialQuery, null, 2)
92 + }
93 + }
94 +
95 + const defaultValue = `[{
8896 $filter: {
8997 value: {
9098 timestamp: {$gt: 0},
9199 content: {
@@ -107,5 +115,16 @@
107115 // $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.
108116
109117 // $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
110118 `
119 + return {
120 + initialQuery: json5.parse(defaultValue),
121 + initialValue: defaultValue
122 + }
111123 }
124 +
125 +function isValidQuery (query) {
126 + if (!Array.isArray(query)) return false
127 + if (!query.map(q => Object.keys(q)[0]).every(q => ['$filter', '$map', '$reduce'].includes(q))) return false
128 +
129 + return true
130 +}

Built with git-ssb-web