This repository was archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathDebug.js
101 lines (85 loc) · 2.89 KB
/
Debug.js
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
import React, { useCallback, useRef } from 'react';
import { Button, Form } from 'antd';
import { observer } from 'mobx-react';
const toJSON = async (annotation) => {
const id = annotation.pk || annotation.id;
const result = await annotation.serializeAnnotation();
const draft = annotation.versions.draft;
const json = { id, result };
if (draft) json.draft = draft;
return json;
};
const DebugComponent = ({ store }) => {
const refConfig = useRef();
const refData = useRef();
const refAnnotations = useRef();
const loadTask = useCallback(() => {
const config = refConfig.current?.value;
const annotations = JSON.parse(refAnnotations.current?.value || '[{ "result": [] }]');
const data = JSON.parse(refData.current?.value);
store.resetState();
store.assignConfig(config);
store.assignTask({ data });
store.initializeStore({ annotations, predictions: [] });
const cs = store.annotationStore;
if (cs.annotations.length) cs.selectAnnotation(cs.annotations[0].id);
}, []);
const serializeCurrent = useCallback(async () => {
const input = refAnnotations.current;
if (!input) return;
const annotation = store.annotationStore.selected;
const json = await Promise.all([toJSON(annotation)]);
input.value = JSON.stringify(json, null, 2);
}, []);
const serializeAll = useCallback(async () => {
const input = refAnnotations.current;
if (!input) return;
const { annotations, predictions } = store.annotationStore;
const json = await Promise.all([...annotations, ...predictions].map(toJSON));
input.value = JSON.stringify(json, null, 2);
}, []);
return (
<div style={{ width: '100%' }}>
<br />
<h2>Debug</h2>
<div>
<Button onClick={serializeAll}>↓ Serialize All Annotations</Button>
<Button onClick={serializeCurrent}>↓ Serialize Current Annotation</Button>
<Button onClick={loadTask}>↑ Simulate Loading Task</Button>
</div>
<Form>
<div style={{ display: 'flex' }}>
<div style={{ flexBasis: '50%' }}>
<p>Data</p>
<textarea
style={{ width: '100%' }}
ref={refData}
rows={4}
defaultValue={store.task.data}
className="is-search"
/>
<p>Config</p>
<textarea
style={{ width: '100%' }}
ref={refConfig}
rows={16}
defaultValue={store.config}
className="is-search"
/>
</div>
<div style={{ flexBasis: '50%' }}>
<p>Annotations</p>
<textarea
style={{ width: '100%' }}
ref={refAnnotations}
rows={22}
// defaultValue={}
className="is-search"
/>
</div>
</div>
</Form>
</div>
);
};
export default observer(DebugComponent);