-
Notifications
You must be signed in to change notification settings - Fork 368
/
AddTilesToDashboardModal.tsx
350 lines (337 loc) · 13.5 KB
/
AddTilesToDashboardModal.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import {
DashboardTileTypes,
getDefaultChartTileSize,
type DashboardChartTile,
} from '@lightdash/common';
import {
Anchor,
Button,
Group,
Modal,
Select,
Stack,
Textarea,
TextInput,
Title,
} from '@mantine/core';
import { useForm } from '@mantine/form';
import {
IconArrowLeft,
IconLayoutDashboard,
IconPlus,
} from '@tabler/icons-react';
import { useState, type FC } from 'react';
import { v4 as uuid4 } from 'uuid';
import {
appendNewTilesToBottom,
useCreateMutation,
useDashboardQuery,
useUpdateDashboard,
} from '../../hooks/dashboard/useDashboard';
import { useDashboards } from '../../hooks/dashboard/useDashboards';
import { useSavedQuery } from '../../hooks/useSavedQuery';
import {
useCreateMutation as useSpaceCreateMutation,
useSpaceSummaries,
} from '../../hooks/useSpaces';
import MantineIcon from '../common/MantineIcon';
interface AddTilesToDashboardModalProps {
isOpen: boolean;
projectUuid: string;
savedChartUuid: string;
onClose?: () => void;
}
const AddTilesToDashboardModal: FC<AddTilesToDashboardModalProps> = ({
isOpen,
projectUuid,
savedChartUuid,
onClose,
}) => {
const [isCreatingNewDashboard, setIsCreatingNewDashboard] = useState(false);
const [isCreatingNewSpace, setIsCreatingNewSpace] =
useState<boolean>(false);
const [isLoading, setIsLoading] = useState(false);
const { data: savedChart } = useSavedQuery({ id: savedChartUuid });
const { data: dashboards, isInitialLoading: isLoadingDashboards } =
useDashboards(
projectUuid,
{
staleTime: 0,
onSuccess: (data) => {
if (data.length === 0) {
setIsCreatingNewDashboard(true);
}
},
},
true, // includePrivateSpaces
);
const { data: spaces, isInitialLoading: isLoadingSpaces } =
useSpaceSummaries(projectUuid, true, {
staleTime: 0,
onSuccess: (data) => {
if (data.length === 0) {
setIsCreatingNewSpace(true);
}
},
});
const currentSpace = spaces?.find((s) => s.uuid === savedChart?.spaceUuid);
const form = useForm({
initialValues: {
dashboardUuid:
dashboards?.find((d) => d.spaceUuid === currentSpace?.uuid)
?.uuid ?? '',
dashboardName: '',
dashboardDescription: '',
spaceUuid: currentSpace?.uuid ?? '',
spaceName: '',
},
});
const { data: selectedDashboard } = useDashboardQuery(
form.getInputProps('dashboardUuid').value,
);
const { mutateAsync: createDashboard } = useCreateMutation(
projectUuid,
true,
);
const { mutateAsync: updateDashboard } = useUpdateDashboard(
form.getInputProps('dashboardUuid').value,
true,
);
const { mutateAsync: createSpace } = useSpaceCreateMutation(projectUuid);
const handleSubmit = form.onSubmit(
async ({
dashboardName,
dashboardDescription,
spaceUuid,
spaceName,
}) => {
if (!savedChart) return;
setIsLoading(true);
try {
if (isCreatingNewSpace) {
const newSpace = await createSpace({
name: spaceName,
isPrivate: false,
access: [],
});
spaceUuid = newSpace.uuid;
}
const newTile: DashboardChartTile = {
uuid: uuid4(),
type: DashboardTileTypes.SAVED_CHART,
properties: {
savedChartUuid: savedChart.uuid,
},
tabUuid: undefined,
...getDefaultChartTileSize(savedChart.chartConfig?.type),
};
if (isCreatingNewDashboard) {
await createDashboard({
name: dashboardName,
description: dashboardDescription,
spaceUuid: spaceUuid,
tiles: [
{
uuid: uuid4(),
type: DashboardTileTypes.SAVED_CHART,
tabUuid: undefined,
properties: {
savedChartUuid: savedChart.uuid,
},
...getDefaultChartTileSize(
savedChart?.chartConfig.type,
),
},
],
tabs: [],
});
onClose?.();
} else {
if (!selectedDashboard) {
throw new Error('Expected dashboard');
}
const firstTab = selectedDashboard.tabs?.[0];
await updateDashboard({
name: selectedDashboard.name,
filters: selectedDashboard.filters,
tiles: appendNewTilesToBottom(selectedDashboard.tiles, [
firstTab
? {
...newTile,
tabUuid: firstTab.uuid,
}
: newTile, // TODO: add to first tab by default, need ux to allow user select tab
]),
tabs: selectedDashboard.tabs,
});
onClose?.();
}
} catch (e) {
console.error(e);
} finally {
setIsLoading(false);
}
},
);
if (isLoadingDashboards || !dashboards || isLoadingSpaces || !spaces) {
return null;
}
const showNewDashboardInput =
isCreatingNewDashboard || dashboards.length === 0;
const showNewSpaceInput = isCreatingNewSpace || spaces.length === 0;
return (
<Modal
opened={isOpen}
onClose={() => onClose?.()}
title={
<Group spacing="xs">
<MantineIcon
icon={IconLayoutDashboard}
size="lg"
color="green.8"
/>
<Title order={4}> Add chart to dashboard</Title>
</Group>
}
withCloseButton
>
<Stack spacing="md" mih="100%">
<form onSubmit={handleSubmit}>
{!showNewDashboardInput ? (
<Stack spacing="md">
<Select
id="select-dashboard"
label="Select a dashboard"
data={dashboards.map((d) => ({
value: d.uuid,
label: d.name,
group: spaces.find(
(s) => s.uuid === d.spaceUuid,
)?.name,
}))}
defaultValue={
dashboards.find(
(d) =>
d.spaceUuid === currentSpace?.uuid,
)?.uuid
}
searchable
nothingFound="No matching dashboards found"
filter={(value, dashboard) =>
!!dashboard.label
?.toLowerCase()
.includes(value.toLowerCase().trim())
}
withinPortal
required
{...form.getInputProps('dashboardUuid')}
/>
<Anchor
component="span"
onClick={() => setIsCreatingNewDashboard(true)}
>
<Group spacing="two">
<MantineIcon icon={IconPlus} />
Create new dashboard
</Group>
</Anchor>
</Stack>
) : (
<Stack spacing="md">
<TextInput
id="dashboard-name"
label="Name your dashboard"
placeholder="eg. KPI dashboard"
required
{...form.getInputProps('dashboardName')}
/>
<Textarea
id="dashboard-description"
label="Dashboard description"
placeholder="A few words to give your team some context"
autosize
maxRows={3}
style={{ overflowY: 'auto' }}
{...form.getInputProps('dashboardDescription')}
/>
{!isLoadingSpaces && !showNewSpaceInput ? (
<>
<Select
id="select-space"
label="Select a space"
data={spaces.map((space) => ({
value: space.uuid,
label: space.name,
}))}
defaultValue={currentSpace?.uuid}
required
withinPortal
{...form.getInputProps('spaceUuid')}
/>
<Anchor
component="span"
onClick={() =>
setIsCreatingNewSpace(true)
}
>
<Group spacing="two">
<MantineIcon icon={IconPlus} />
Create new space
</Group>
</Anchor>
</>
) : (
<>
<TextInput
id="new-space"
label="Name your new space"
placeholder="eg. KPIs"
required
{...form.getInputProps('spaceName')}
/>
<Anchor
component="span"
onClick={() =>
setIsCreatingNewSpace(false)
}
>
<Group spacing="two">
<MantineIcon icon={IconArrowLeft} />
Save to existing space
</Group>
</Anchor>
</>
)}
</Stack>
)}
<Group spacing="xs" position="right" mt="md">
<Button
onClick={() => {
if (onClose) onClose();
setIsCreatingNewDashboard(false);
}}
variant="outline"
>
Cancel
</Button>
<Button
type="submit"
loading={isLoading}
disabled={
(isCreatingNewDashboard &&
form.getInputProps('dashboardName')
.value === '') ||
(isCreatingNewSpace &&
form.getInputProps('spaceName').value ===
'')
}
>
Add to dashboard
</Button>
</Group>
</form>
</Stack>
</Modal>
);
};
export default AddTilesToDashboardModal;