aboutsummaryrefslogtreecommitdiffstats
path: root/client/js/app/src/app/pages/querybuilder/context/query-builder-provider.jsx
blob: 911f55f1862e3908212d51f577b22bee13c24d25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
import { last, set } from 'lodash';
import React, { useReducer } from 'react';
import { createContext, useContextSelector } from 'use-context-selector';
import parameters from 'app/pages/querybuilder/context/parameters';

let _dispatch;
const root = { type: { children: parameters } };
const context = createContext(null);

export const ACTION = Object.freeze({
  SET_QUERY: 0,
  SET_HTTP: 1,
  SET_METHOD: 2,
  SET_URL: 3,

  INPUT_ADD: 10,
  INPUT_UPDATE: 11,
  INPUT_REMOVE: 12,
});

function cloneParams(params) {
  if (!Array.isArray(params.value)) return { ...params };
  return { ...params, value: params.value.map(cloneParams) };
}

function inputsToQuery(method, inputs) {
  if (method === 'POST') {
    const inputsToJson = (inputs, parent) =>
      Object.fromEntries(
        inputs.map(({ value, type: { name, type, children } }) => [
          name,
          children ? inputsToJson(value) : parseInput(value, type),
        ])
      );
    return JSON.stringify(inputsToJson(inputs), null, 4);
  }

  const inputsToSearchParams = (inputs, parent) =>
    inputs.reduce((acc, { value, type: { name, children } }) => {
      const key = parent ? `${parent}.${name}` : name;
      return Object.assign(
        acc,
        children ? inputsToSearchParams(value, key) : { [key]: value }
      );
    }, {});
  return new URLSearchParams(inputsToSearchParams(inputs)).toString();
}

function queryToInputs(method, query) {
  if (method === 'POST') return jsonToInputs(JSON.parse(query));

  const json = [...new URLSearchParams(query).entries()].reduce(
    (acc, [key, value]) => set(acc, key, value),
    {}
  );
  return jsonToInputs(json);
}

function jsonToInputs(json, parent = root) {
  return Object.entries(json).map(([key, value], i) => {
    const node = {
      id: parent.id ? `${parent.id}.${i}` : i.toString(),
      type:
        typeof parent.type.children === 'string'
          ? { name: key, type: parent.type.children }
          : parent.type.children[key],
    };
    if (!node.type) {
      const location = parent.type.name
        ? `under '${parent.type.name}'`
        : 'on root level';
      throw new Error(`Unknown property '${key}' ${location}`);
    }
    if (value != null && typeof value === 'object') {
      if (!node.type.children)
        throw new Error(`Expected property '${key}' to be ${node.type.type}`);
      node.value = jsonToInputs(value, node);
    } else {
      if (node.type.children)
        throw new Error(
          `Property '${key}' cannot have a value, supported children: ${Object.keys(
            node.type.children
          ).sort()}`
        );
      node.value = value?.toString();
    }
    return node;
  });
}

function parseInput(value, type) {
  if (type === 'Integer') return parseInt(value);
  if (type === 'Float') return parseFloat(value);
  if (type === 'Boolean') return value.toLowerCase() === 'true';
  return value;
}

function inputAdd(params, { id: parentId, type: typeName }) {
  const cloned = cloneParams(params);
  const parent = findInput(cloned, parentId);

  const nextId = parseInt(last(last(parent.value)?.id?.split('.')) ?? -1) + 1;
  const id = parentId ? `${parentId}.${nextId}` : nextId.toString();
  const type =
    typeof parent.type.children === 'string'
      ? { name: typeName, type: parent.type.children }
      : parent.type.children[typeName];

  parent.value.push({
    id,
    value: type.children ? [] : type.default?.toString() ?? '',
    type,
  });

  return cloned;
}

function inputUpdate(params, { id, type, value }) {
  const cloned = cloneParams(params);
  const node = findInput(cloned, id);
  if (type) {
    const parent = findInput(cloned, id.substring(0, id.lastIndexOf('.')));
    const newType =
      typeof parent.type.children === 'string'
        ? { name: type, type: parent.type.children }
        : parent.type.children[type];
    if (node.type.type !== newType.type.type)
      node.value = newType.children ? [] : newType.default?.toString() ?? '';
    node.type = newType;
  }
  if (value != null) node.value = value;

  return cloned;
}

function findInput(params, id, Delete = false) {
  if (!id) return params;
  let end = -1;
  while ((end = id.indexOf('.', end + 1)) > 0)
    params = params.value.find((input) => input.id === id.substring(0, end));
  const index = params.value.findIndex((input) => input.id === id);
  return Delete ? params.value.splice(index, 1)[0] : params.value[index];
}

export function reducer(state, action) {
  if (state == null) {
    state = { http: {}, params: {}, query: {}, request: {} };

    return [
      [ACTION.SET_URL, 'http://localhost:8080/search/'],
      [ACTION.SET_QUERY, 'yql='],
      [ACTION.SET_METHOD, 'POST'],
    ].reduce((s, [action, data]) => reducer(s, { action, data }), state);
  }

  const result = preReducer(state, action);
  const { request: sr, params: sp, query: sq } = state;
  const { request: rr, params: rp, query: rq } = result;

  if ((sp.value !== rp.value && sq === rq) || sr.method !== rr.method)
    result.query = { input: inputsToQuery(rr.method, rp.value) };

  const input = result.query.input;
  if (sr.url !== rr.url || sq.input !== input || sr.method !== rr.method) {
    if (rr.method === 'POST') {
      result.request = { ...result.request, fullUrl: rr.url, body: input };
    } else {
      const url = new URL(rr.url);
      url.search = input;
      result.request = {
        ...result.request,
        fullUrl: url.toString(),
        body: null,
      };
    }
  }

  return result;
}

function preReducer(state, { action, data }) {
  switch (action) {
    case ACTION.SET_QUERY: {
      try {
        const value = queryToInputs(state.request.method, data);
        return {
          ...state,
          params: { ...root, value },
          query: { input: data },
        };
      } catch (error) {
        return { ...state, query: { input: data, error: error.message } };
      }
    }
    case ACTION.SET_HTTP:
      return { ...state, http: data };
    case ACTION.SET_METHOD:
      return { ...state, request: { ...state.request, method: data } };
    case ACTION.SET_URL:
      return { ...state, request: { ...state.request, url: data } };

    case ACTION.INPUT_ADD:
      return { ...state, params: inputAdd(state.params, data) };
    case ACTION.INPUT_UPDATE:
      return { ...state, params: inputUpdate(state.params, data) };
    case ACTION.INPUT_REMOVE: {
      const cloned = cloneParams(state.params);
      findInput(cloned, data, true);
      return { ...state, params: cloned };
    }

    default:
      throw new Error(`Unknown action ${action}`);
  }
}

export function QueryBuilderProvider({ children }) {
  const [value, dispatch] = useReducer(reducer, null, reducer);
  _dispatch = dispatch;
  return <context.Provider value={value}>{children}</context.Provider>;
}

export function useQueryBuilderContext(selector) {
  const func = typeof selector === 'string' ? (c) => c[selector] : selector;
  return useContextSelector(context, func);
}

export function dispatch(action, data) {
  _dispatch({ action, data });
}