-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathfiles-to-upload.tsx
369 lines (351 loc) · 12.6 KB
/
files-to-upload.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import React, { useEffect, useRef } from "react"
import styled from "styled-components"
import { Heading, Table, Select, Button } from "@questdb/react-components"
import type { Props as TableProps } from "@questdb/react-components/dist/components/Table"
import { PopperHover, Text, Tooltip } from "../../../components"
import { Box } from "../../../components/Box"
import { bytesWithSuffix } from "../../../utils/bytesWithSuffix"
import { FileStatus } from "./file-status"
import { Grid, Information } from "@styled-icons/remix-line"
import { FiletypeCsv } from "@styled-icons/bootstrap/FiletypeCsv"
import { ProcessedFile } from "./types"
import { UploadActions } from "./upload-actions"
import { RenameTableDialog } from "./rename-table-dialog"
import { Dialog as TableSchemaDialog } from "../../../components/TableSchemaDialog/dialog"
import { UploadResultDialog } from "./upload-result-dialog"
import { shortenText, UploadResult } from "../../../utils"
import { DropBox } from "./dropbox"
const Root = styled(Box).attrs({ flexDirection: "column", gap: "2rem" })`
padding: 2rem;
`
const StyledTable = styled(Table)`
width: 100%;
table-layout: fixed;
border-collapse: separate;
border-spacing: 0 2rem;
th {
padding: 0 1.5rem;
}
td {
padding: 1.5rem;
}
tbody td {
background: #242531;
&:first-child {
border-top-left-radius: ${({ theme }) => theme.borderRadius};
border-bottom-left-radius: ${({ theme }) => theme.borderRadius};
}
&:last-child {
border-top-right-radius: ${({ theme }) => theme.borderRadius};
border-bottom-right-radius: ${({ theme }) => theme.borderRadius};
}
}
`
const EmptyState = styled(Box).attrs({ justifyContent: "center" })`
width: 100%;
background: #242531;
border-radius: ${({ theme }) => theme.borderRadius};
padding: 1rem;
`
const FileTextBox = styled(Box)`
padding: 0 1.1rem;
`
const BrowseTextLink = styled.span`
text-decoration: underline;
cursor: pointer;
&:hover {
text-decoration: none;
}
`
type Props = {
files: ProcessedFile[]
onDialogToggle: (open: boolean) => void
onFileRemove: (id: string) => void
onFileUpload: (id: string) => void
onFilePropertyChange: (id: string, file: Partial<ProcessedFile>) => void
onFilesDropped: (files: File[]) => void
onViewData: (result: UploadResult) => void
dialogOpen: boolean
}
export const FilesToUpload = ({
files,
onDialogToggle,
onFileRemove,
onFilePropertyChange,
onFileUpload,
onFilesDropped,
onViewData,
dialogOpen,
}: Props) => {
const uploadInputRef = useRef<HTMLInputElement | null>(null)
const [renameDialogOpen, setRenameDialogOpen] = React.useState<
string | undefined
>()
const [schemaDialogOpen, setSchemaDialogOpen] = React.useState<
string | undefined
>()
useEffect(() => {
onDialogToggle(
renameDialogOpen !== undefined || schemaDialogOpen !== undefined,
)
}, [renameDialogOpen, schemaDialogOpen])
return (
<DropBox
files={files}
onFilesDropped={onFilesDropped}
dialogOpen={dialogOpen}
render={({ duplicates, addToQueue }) => (
<Root>
<Heading level={3}>Upload queue</Heading>
<input
type="file"
id="file"
onChange={(e) => {
if (e.target.files === null) return
addToQueue(e.target.files)
}}
multiple={true}
ref={uploadInputRef}
style={{ display: "none" }}
value=""
/>
<Text color="foreground">
You can drag and drop more files or{" "}
<BrowseTextLink
onClick={() => {
uploadInputRef.current?.click()
}}
>
browse from disk
</BrowseTextLink>
</Text>
{duplicates.length > 0 && (
<Text color="red">
File{duplicates.length > 1 ? "s" : ""} already added to queue:{" "}
{duplicates.map((f) => f.name).join(", ")}. Change target table
name and try again.
</Text>
)}
<StyledTable<React.FunctionComponent<TableProps<ProcessedFile>>>
columns={[
{
header: "File",
align: "flex-start",
...(files.length > 0 && { width: "400px" }),
render: ({ data }) => {
const file = (
<FileTextBox align="center" gap="1rem">
<Text color="foreground">
{shortenText(data.fileObject.name, 20)}
</Text>
<Text color="gray2" size="sm">
{bytesWithSuffix(data.fileObject.size)}
</Text>
</FileTextBox>
)
return (
<Box align="center" gap="1rem">
<FiletypeCsv size="46px" />
<Box gap="1rem" align="flex-4tart" flexDirection="column">
{data.fileObject.name.length > 20 && (
<PopperHover placement="top" trigger={file}>
<Tooltip>{data.fileObject.name}</Tooltip>
</PopperHover>
)}
{data.fileObject.name.length <= 20 && file}
<Box gap="1rem" align="center">
<FileStatus file={data} />
{!data.isUploading &&
data.uploadResult !== undefined && (
<React.Fragment>
<UploadResultDialog file={data} />
<Button
skin="secondary"
prefixIcon={<Grid size="18px" />}
onClick={() =>
onViewData(
data.uploadResult as UploadResult,
)
}
>
Result
</Button>
</React.Fragment>
)}
</Box>
{(data.uploadResult &&
data.uploadResult.rowsRejected > 0) ||
(data.error && (
<FileTextBox
flexDirection="column"
gap="1rem"
align="flex-start"
>
{data.uploadResult &&
data.uploadResult.rowsRejected > 0 && (
<Text color="orange" size="sm">
{data.uploadResult.rowsRejected.toLocaleString()}{" "}
row
{data.uploadResult.rowsRejected > 1
? "s"
: ""}{" "}
rejected
</Text>
)}
{data.error && (
<Text color="red" size="sm">
{data.error}
</Text>
)}
</FileTextBox>
))}
</Box>
</Box>
)
},
},
{
header: "Table name",
align: "flex-end",
width: "200px",
render: ({ data }) => {
return (
<RenameTableDialog
open={renameDialogOpen === data.id}
onOpenChange={(f) => setRenameDialogOpen(f?.id)}
onNameChange={(name) => {
onFilePropertyChange(data.id, {
table_name: name,
})
}}
file={data}
/>
)
},
},
{
header: (
<PopperHover
placement="top"
trigger={
<Box align="center" gap="0.5rem">
Schema
<Information size="16px" />
</Box>
}
>
<Tooltip>
Optional. By default, QuestDB will infer schema from the
CSV file structure
</Tooltip>
</PopperHover>
),
align: "center",
width: "150px",
render: ({ data }) => {
const name = data.table_name ?? data.fileObject.name
return (
<TableSchemaDialog
action="import"
open={schemaDialogOpen === data.id}
onOpenChange={(name?: string) =>
setSchemaDialogOpen(name ? data.id : undefined)
}
onSchemaChange={(schema) => {
onFilePropertyChange(data.id, {
schema: schema.schemaColumns,
partitionBy: schema.partitionBy,
timestamp: schema.timestamp,
})
}}
name={name}
schema={data.schema}
partitionBy={data.partitionBy}
timestamp={data.timestamp}
isEditLocked={
data.exists && data.table_name === data.fileObject.name
}
hasWalSetting={false}
ctaText="Save"
/>
)
},
},
{
header: (
<PopperHover
placement="top"
trigger={
<Box align="center" gap="0.5rem">
Write mode
<Information size="16px" />
</Box>
}
>
<Tooltip>
<strong>Append</strong>: data will be appended to the set.
<br />
<strong>Overwrite</strong>: any existing data or structure
will be overwritten. Required for partitioning and
timestamp related changes.
</Tooltip>
</PopperHover>
),
align: "center",
width: "150px",
render: ({ data }) => (
<Select
name="overwrite"
defaultValue={data.settings.overwrite ? "true" : "false"}
onChange={(e: React.ChangeEvent<HTMLSelectElement>) =>
onFilePropertyChange(data.id, {
settings: {
...data.settings,
overwrite: e.target.value === "true",
},
})
}
options={[
{
label: "Append",
value: "false",
},
{
label: "Overwrite",
value: "true",
},
]}
/>
),
},
{
align: "flex-end",
width: "300px",
render: ({ data }) => (
<UploadActions
file={data}
onUpload={onFileUpload}
onRemove={onFileRemove}
onSettingsChange={(settings) => {
onFilePropertyChange(data.id, {
settings,
})
}}
/>
),
},
]}
rows={files}
/>
{files.length === 0 && (
<EmptyState>
<Text color="gray2" align="center">
No files in queue
</Text>
</EmptyState>
)}
</Root>
)}
/>
)
}