aboutsummaryrefslogtreecommitdiffstats
path: root/client/js/app/src/app/pages/querybuilder/query-filters/query-filters.jsx
blob: cdc6da0ba169a3f7abab6ddf2eaf289378cb9441 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
import React from 'react';
import {
  Select,
  TextInput,
  ActionIcon,
  Button,
  Box,
  Stack,
  Switch,
  Badge,
  Group,
} from '@mantine/core';
import { Container, Content, Icon } from 'app/components';
import {
  ACTION,
  dispatch,
  useQueryBuilderContext,
} from 'app/pages/querybuilder/context/query-builder-provider';
import { SHADE } from 'app/styles/theme/colors';

function AddProperty(props) {
  return (
    <Button leftIcon={<Icon name="plus" />} {...props}>
      Add property
    </Button>
  );
}

function Property({ id, type, types }) {
  if (types)
    return (
      <Select
        sx={{ flex: 1 }}
        data={Object.values({ [type.name]: type, ...types }).map(
          ({ name }) => name
        )}
        onChange={(type) => dispatch(ACTION.INPUT_UPDATE, { id, type })}
        value={type.name}
        searchable
      />
    );

  return (
    <TextInput
      sx={{ flex: 1 }}
      onChange={(event) =>
        dispatch(ACTION.INPUT_UPDATE, {
          id,
          type: event.currentTarget.value,
        })
      }
      placeholder="String"
      value={type.name}
    />
  );
}

function Value({ id, type, value }) {
  if (type.children) return null;

  if (type.type === 'Boolean')
    return (
      <Switch
        sx={{ flex: 1 }}
        onLabel="true"
        offLabel="false"
        size="xl"
        checked={value === 'true'}
        onChange={(event) =>
          dispatch(ACTION.INPUT_UPDATE, {
            id,
            value: event.currentTarget.checked.toString(),
          })
        }
      />
    );

  const props = { value, placeholder: type.type };
  if (type.type === 'Integer' || type.type === 'Float') {
    props.type = 'number';
    let range;
    if (type.min != null) {
      props.min = type.min;
      range = `[${props.min}, `;
    } else range = '(-∞, ';
    if (type.max != null) {
      props.max = type.max;
      range += props.max + ']';
    } else range += '∞)';
    props.placeholder += ` in ${range}`;

    if (type.type === 'Float' && type.min != null && type.max != null)
      props.step = (type.max - type.min) / 100;

    if (parseFloat(value) < type.min || parseFloat(value) > type.max)
      props.error = `Must be within ${range}`;
  }

  return (
    <TextInput
      sx={{ flex: 1 }}
      onChange={(event) =>
        dispatch(ACTION.INPUT_UPDATE, {
          id,
          value: event.currentTarget.value,
        })
      }
      {...props}
    />
  );
}

function Input({ id, value, types, type }) {
  return (
    <>
      <Box sx={{ display: 'flex', gap: '5px' }}>
        <Property {...{ id, type, types }} />
        <Value {...{ id, type, value }} />
        <ActionIcon
          sx={{ marginTop: 5 }}
          onClick={() => dispatch(ACTION.INPUT_REMOVE, id)}
        >
          <Icon name="circle-minus" />
        </ActionIcon>
      </Box>
      {type.children && (
        <Box
          py={8}
          sx={(theme) => ({
            borderLeft: `1px dashed ${theme.fn.themeColor(
              'gray',
              SHADE.UI_ELEMENT_BORDER_AND_FOCUS
            )}`,
            marginLeft: '13px',
            paddingLeft: '13px',
          })}
        >
          <Inputs id={id} type={type.children} inputs={value} />
        </Box>
      )}
    </>
  );
}

function Inputs({ id, type, inputs }) {
  const usedTypes = inputs.map(({ type }) => type.name);
  const remainingTypes =
    typeof type === 'string'
      ? null
      : Object.fromEntries(
          Object.entries(type).filter(([name]) => !usedTypes.includes(name))
        );
  const firstRemaining = remainingTypes ? Object.keys(remainingTypes)[0] : '';

  return (
    <Container sx={{ rowGap: '5px' }}>
      {inputs.map(({ id, value, type }) => (
        <Input key={id} types={remainingTypes} {...{ id, value, type }} />
      ))}
      {firstRemaining != null && (
        <>
          {id != null ? (
            <Container sx={{ justifyContent: 'start' }}>
              <AddProperty
                onClick={() =>
                  dispatch(ACTION.INPUT_ADD, { id, type: firstRemaining })
                }
                variant="subtle"
                size="xs"
                compact
              />
            </Container>
          ) : (
            <AddProperty
              onClick={() =>
                dispatch(ACTION.INPUT_ADD, { id, type: firstRemaining })
              }
              mt={13}
            />
          )}
        </>
      )}
    </Container>
  );
}

export function QueryFilters() {
  const { value, type } = useQueryBuilderContext('params');
  return (
    <Stack>
      <Group>
        <Badge variant="filled">Parameters</Badge>
      </Group>
      <Container sx={{ alignContent: 'start' }}>
        <Content padding={0}>
          <Inputs type={type.children} inputs={value} />
        </Content>
      </Container>
    </Stack>
  );
}