forked from mui/mui-toolpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryStateEditor.tsx
268 lines (248 loc) · 7.91 KB
/
QueryStateEditor.tsx
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import {
Stack,
Button,
Dialog,
DialogTitle,
DialogContent,
FormControl,
InputLabel,
MenuItem,
Select,
List,
ListItem,
SelectChangeEvent,
DialogActions,
Box,
LinearProgress,
Alert,
Checkbox,
FormControlLabel,
TextField,
InputAdornment,
} from '@mui/material';
import * as React from 'react';
import { ArgTypeDefinitions, UseDataQuery } from '@mui/toolpad-core';
import useLatest from '../../../utils/useLatest';
import { useDom, useDomApi } from '../../DomLoader';
import { usePageEditorState } from './PageEditorProvider';
import * as appDom from '../../../appDom';
import { NodeId } from '../../../types';
import { ExactEntriesOf } from '../../../utils/types';
import { getQueryNodeArgTypes } from '../../../toolpadDataSources/client';
import NodeAttributeEditor from './NodeAttributeEditor';
import NodeNameEditor from '../NodeNameEditor';
import JsonView from '../../JsonView';
interface ParamsEditorProps<Q> {
node: appDom.AppDomNode;
argTypes: ArgTypeDefinitions<Q>;
}
function ParamsEditor<Q>({ node, argTypes }: ParamsEditorProps<Q>) {
return (
<Stack spacing={1}>
{(Object.entries(argTypes) as ExactEntriesOf<ArgTypeDefinitions<Q>>).map(
([propName, propTypeDef]) =>
propTypeDef ? (
<div key={propName}>
<NodeAttributeEditor
node={node}
namespace="params"
name={propName}
argType={propTypeDef}
/>
</div>
) : null,
)}
</Stack>
);
}
interface PreviewQueryStateResultProps {
node: appDom.QueryStateNode;
}
function PreviewQueryStateResult({ node }: PreviewQueryStateResultProps) {
const { pageState } = usePageEditorState();
const actualNodeState: UseDataQuery | undefined = pageState[node.name] as any;
if (!node.attributes.api.value) {
return null;
}
return (
<Box sx={{ maxHeight: 150, overflow: 'auto' }}>
{actualNodeState?.isLoading ? <LinearProgress /> : null}
{actualNodeState?.error ? (
<Alert severity="error">
{actualNodeState?.error.message || actualNodeState?.error || 'Something went wrong'}
</Alert>
) : null}
{actualNodeState?.data ? <JsonView src={actualNodeState.data} /> : null}
</Box>
);
}
function refetchIntervalInSeconds(maybeInterval?: number) {
if (typeof maybeInterval !== 'number') {
return undefined;
}
const seconds = Math.floor(maybeInterval / 1000);
return seconds > 0 ? seconds : undefined;
}
interface QueryStateNodeEditorProps<P> {
node: appDom.QueryStateNode<P>;
}
function QueryStateNodeEditor<P>({ node }: QueryStateNodeEditorProps<P>) {
const dom = useDom();
const domApi = useDomApi();
const app = appDom.getApp(dom);
const { apis = [] } = appDom.getChildNodes(dom, app);
const handleSelectionChange = React.useCallback(
(event: SelectChangeEvent<'' | NodeId>) => {
const apiNodeId = event.target.value ? (event.target.value as NodeId) : null;
domApi.setNodeNamespacedProp(node, 'attributes', 'api', appDom.createConst(apiNodeId));
},
[domApi, node],
);
const handleRefetchOnWindowFocusChange = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
domApi.setNodeNamespacedProp(
node,
'attributes',
'refetchOnWindowFocus',
appDom.createConst(event.target.checked),
);
},
[domApi, node],
);
const handleRefetchOnReconnectChange = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
domApi.setNodeNamespacedProp(
node,
'attributes',
'refetchOnReconnect',
appDom.createConst(event.target.checked),
);
},
[domApi, node],
);
const handleRefetchIntervalChange = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
const interval = Number(event.target.value);
if (Number.isNaN(interval) || interval <= 0) {
domApi.setNodeNamespacedProp(node, 'attributes', 'refetchInterval', undefined);
} else {
domApi.setNodeNamespacedProp(
node,
'attributes',
'refetchInterval',
appDom.createConst(interval * 1000),
);
}
},
[domApi, node],
);
const argTypes = getQueryNodeArgTypes(dom, node);
return (
<React.Fragment>
<Stack spacing={1} py={1}>
<NodeNameEditor node={node} />
<FormControl fullWidth size="small">
<InputLabel id={`select-data-query`}>Query</InputLabel>
<Select
value={node.attributes.api.value || ''}
labelId="select-data-query"
label="Query"
onChange={handleSelectionChange}
size="small"
>
<MenuItem value="">---</MenuItem>
{apis.map(({ id, name }) => (
<MenuItem key={id} value={id}>
{name}
</MenuItem>
))}
</Select>
</FormControl>
<ParamsEditor node={node} argTypes={argTypes} />
<FormControlLabel
control={
<Checkbox
size="small"
checked={node.attributes.refetchOnWindowFocus?.value ?? true}
onChange={handleRefetchOnWindowFocusChange}
/>
}
label="Refetch on window focus"
/>
<FormControlLabel
control={
<Checkbox
size="small"
checked={node.attributes.refetchOnReconnect?.value ?? true}
onChange={handleRefetchOnReconnectChange}
/>
}
label="Refetch on network reconnect"
/>
<TextField
InputProps={{
startAdornment: <InputAdornment position="start">s</InputAdornment>,
}}
sx={{ maxWidth: 300 }}
size="small"
type="number"
label="Refetch interval"
value={refetchIntervalInSeconds(node.attributes.refetchInterval?.value) ?? ''}
onChange={handleRefetchIntervalChange}
/>
<PreviewQueryStateResult node={node} />
</Stack>
</React.Fragment>
);
}
export default function QueryStateEditor() {
const dom = useDom();
const state = usePageEditorState();
const domApi = useDomApi();
const [editedState, setEditedState] = React.useState<NodeId | null>(null);
const editedStateNode = editedState ? appDom.getNode(dom, editedState, 'queryState') : null;
const handleEditStateDialogClose = React.useCallback(() => setEditedState(null), []);
const page = appDom.getNode(dom, state.nodeId, 'page');
const { queryStates = [] } = appDom.getChildNodes(dom, page) ?? [];
// To keep it around during closing animation
const lastEditedStateNode = useLatest(editedStateNode);
const handleRemove = React.useCallback(() => {
if (editedStateNode) {
domApi.removeNode(editedStateNode.id);
}
handleEditStateDialogClose();
}, [editedStateNode, handleEditStateDialogClose, domApi]);
return queryStates.length > 0 ? (
<Stack spacing={1} alignItems="start">
<List>
{queryStates.map((stateNode) => {
return (
<ListItem key={stateNode.id} button onClick={() => setEditedState(stateNode.id)}>
{stateNode.name}
</ListItem>
);
})}
</List>
{lastEditedStateNode ? (
<Dialog
fullWidth
maxWidth="lg"
open={!!editedStateNode}
onClose={handleEditStateDialogClose}
scroll="body"
>
<DialogTitle>Edit Query State ({lastEditedStateNode.id})</DialogTitle>
<DialogContent>
<QueryStateNodeEditor node={lastEditedStateNode} />
</DialogContent>
<DialogActions>
<Button color="inherit" variant="text" onClick={handleEditStateDialogClose}>
Close
</Button>
<Button onClick={handleRemove}>Remove</Button>
</DialogActions>
</Dialog>
) : null}
</Stack>
) : null;
}