This repository was archived by the owner on Apr 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.tsx
More file actions
80 lines (72 loc) · 2.45 KB
/
index.tsx
File metadata and controls
80 lines (72 loc) · 2.45 KB
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
import React from 'react';
import * as t from 'io-ts';
import { isRight } from 'fp-ts/Either';
import JsonFileInput, { Props as JsonFileInputProps } from '#components/JsonFileInput';
const Image = t.type({
id: t.number,
// width: t.number,
// height: t.number,
file_name: t.string,
// license: t.union([t.number, t.undefined]),
flickr_url: t.union([t.string, t.undefined]),
coco_url: t.union([t.string, t.undefined]),
// date_captured: DateFromISOString,
});
const CocoDataset = t.type({
// info: Info,
// licenses: t.array(License),
images: t.array(Image),
// annotations: t.array(Annotation),
// categories: t.array(Category)
});
export type CocoDatasetType = t.TypeOf<typeof CocoDataset>
interface Props<N> extends Omit<JsonFileInputProps<N, object>, 'onChange' | 'value'> {
value: CocoDatasetType | undefined;
maxLength: number;
onChange: (newValue: CocoDatasetType | undefined, name: N) => void;
}
function CocoFileInput<N>(props: Props<N>) {
const {
name,
onChange,
error,
maxLength,
...otherProps
} = props;
const [
internalErrorMessage,
setInternalErrorMessage,
] = React.useState<string>();
const handleChange = React.useCallback(
(val) => {
const result = CocoDataset.decode(val);
if (!isRight(result)) {
// eslint-disable-next-line no-console
console.error('Invalid COCO format', result.left);
setInternalErrorMessage('Invalid COCO format');
return;
}
if (result.right.images.length > maxLength) {
setInternalErrorMessage(`Too many images ${result.right.images.length} uploaded. Please do not exceed ${maxLength} images.`);
return;
}
const uniqueIdentifiers = new Set(result.right.images.map((item) => item.id));
if (uniqueIdentifiers.size < result.right.images.length) {
setInternalErrorMessage('Each image should have a unique id.');
return;
}
setInternalErrorMessage(undefined);
onChange(result.right, name);
},
[onChange, maxLength, name],
);
return (
<JsonFileInput
name={name}
onChange={handleChange}
error={internalErrorMessage ?? error}
{...otherProps}
/>
);
}
export default CocoFileInput;