Projekt

Obecné

Profil

Stáhnout (11.1 KB) Statistiky
| Větev: | Tag: | Revize:
1 4e91ed40 Dominik Poch
import { Col, Container, Row, Stack } from 'react-bootstrap';
2 76242338 Dominik Poch
import { Tag } from '../types/tag';
3 c73aecde Dominik Poch
import { ChangeEvent, useContext, useState } from 'react';
4 967f45fc Dominik Poch
import 'antd/dist/antd.css';
5 bcbaa7d3 Dominik Poch
import { Button, Input, Select } from 'antd';
6 967f45fc Dominik Poch
import {
7
    PlusOutlined,
8
    DownOutlined,
9
    DeleteOutlined,
10 76242338 Dominik Poch
    TagOutlined,
11 6973d036 Lukáš Vlček
    EyeOutlined,
12 967f45fc Dominik Poch
} from '@ant-design/icons';
13 c73aecde Dominik Poch
import { AnnotationContext } from '../../contexts/AnnotationContext';
14 bcbaa7d3 Dominik Poch
import { ETagSentiment, TagInstanceInfo } from '../../api';
15 80244bde Lukáš Vlček
import { getTextMaxLength } from '../../utils/strings';
16 bcbaa7d3 Dominik Poch
17
const { Option } = Select;
18 967f45fc Dominik Poch
19 acb8a961 Dominik Poch
/**
20
 * Creates a single item in an annotation panel.
21
 * @param props Properties should contain a tag that will be shown in this annotation.
22
 * @returns The item that represents an annotation.
23
 */
24 c73aecde Dominik Poch
export function AnnotationItem(props: { tag: Tag }) {
25 acb8a961 Dominik Poch
    /**
26
     * Should properties of this annotation be visible?
27
     */
28 967f45fc Dominik Poch
    const [visibleProperties, setVisibleProperties] = useState(false);
29 acb8a961 Dominik Poch
30
    /**
31
     * Context that manages annotations.
32
     */
33 c73aecde Dominik Poch
    const {
34
        addOccurrence,
35
        deleteOccurrence,
36
        changePosition,
37 812ee966 Dominik Poch
        changeNote,
38 bcbaa7d3 Dominik Poch
        changeSentiment,
39 c73aecde Dominik Poch
        changeLength,
40 6973d036 Lukáš Vlček
        selectedOccurrenceId,
41
        selectedInstanceId,
42
        setSelectedOccurrenceId,
43
        setSelectedInstanceId,
44 c73aecde Dominik Poch
    } = useContext(AnnotationContext);
45 967f45fc Dominik Poch
46 acb8a961 Dominik Poch
    /**
47
     * Adds new occurrence of this annotation to the context.
48
     */
49 c73aecde Dominik Poch
    const onAddOccurrence = () => {
50
        addOccurrence(props.tag);
51 4e91ed40 Dominik Poch
    };
52 967f45fc Dominik Poch
53 acb8a961 Dominik Poch
    /**
54
     * Removes an occurrence of this annotation from the context.
55
     * @param occurrence The occurrence that should be removed.
56
     */
57 76242338 Dominik Poch
    const onDeleteOccurrence = (occurrence: TagInstanceInfo) => (e: any) => {
58 c73aecde Dominik Poch
        deleteOccurrence(occurrence);
59 4e91ed40 Dominik Poch
    };
60 967f45fc Dominik Poch
61 acb8a961 Dominik Poch
    /**
62
     * Changes a position of an occurrence of this annotation in the context.
63
     * @param occurrence The occurrence that should be changed.
64
     */
65 c73aecde Dominik Poch
    const onChangePosition =
66 76242338 Dominik Poch
        (occurrence: TagInstanceInfo) => (e: ChangeEvent<HTMLInputElement>) => {
67 c73aecde Dominik Poch
            changePosition(occurrence, Number(e.currentTarget.value));
68 4e91ed40 Dominik Poch
        };
69
70 acb8a961 Dominik Poch
    /**
71
     * Changes a length of an occurrence of this annotation in the context.
72
     * @param occurrence The occurrence that should be changed.
73
     */
74 c73aecde Dominik Poch
    const onChangeLength =
75 76242338 Dominik Poch
        (occurrence: TagInstanceInfo) => (e: ChangeEvent<HTMLInputElement>) => {
76 c73aecde Dominik Poch
            changeLength(occurrence, Number(e.currentTarget.value));
77 4e91ed40 Dominik Poch
        };
78
79 bcbaa7d3 Dominik Poch
    const onChangeSentiment = (occurrence: TagInstanceInfo) => (val: ETagSentiment) => {
80
        changeSentiment(occurrence, val);
81
    };
82
83 812ee966 Dominik Poch
    const onChangeNote =
84
        (occurrence: TagInstanceInfo) => (e: ChangeEvent<HTMLTextAreaElement>) => {
85
            changeNote(occurrence, e.currentTarget.value);
86
        };
87
88 acb8a961 Dominik Poch
    /**
89
     * Changes visibility of properties of this annotation.
90
     */
91 c73aecde Dominik Poch
    const changePropertiesVisibility = () => {
92
        setVisibleProperties(!visibleProperties);
93
    };
94
95 967f45fc Dominik Poch
    return (
96 c73aecde Dominik Poch
        <Container>
97 6973d036 Lukáš Vlček
            <Row
98
                className="border rounded"
99
                style={{
100
                    backgroundColor:
101
                        selectedInstanceId === props.tag.instanceId ? '#FCF3CF' : 'white',
102
                }}
103
            >
104 76242338 Dominik Poch
                <Col sm="auto" className="d-flex align-items-center">
105 6973d036 Lukáš Vlček
                    <Button
106
                        icon={<TagOutlined style={{ marginLeft: 0 }} />}
107
                        onClick={() => {
108
                            setSelectedInstanceId(props.tag.instanceId);
109
                            setSelectedOccurrenceId(null);
110
                        }}
111
                        style={{
112
                            border: 'none',
113
                            paddingLeft: 0,
114
                            backgroundColor:
115
                                selectedInstanceId === props.tag.instanceId
116
                                    ? '#FCF3CF'
117
                                    : 'white',
118
                        }}
119
                        title={'Zvýraznit značky'}
120
                    />
121 76242338 Dominik Poch
                </Col>
122 4bc99591 Lukáš Vlček
                <Col className="d-flex align-items-center">
123
                    {props.tag.tagName}
124
                    {props.tag.subtagName ? ' (' + props.tag.subtagName + ')' : ''}
125
                </Col>
126 4e91ed40 Dominik Poch
                <Col sm="auto">
127
                    <Button
128
                        type="text"
129
                        shape="circle"
130
                        icon={<PlusOutlined />}
131 c73aecde Dominik Poch
                        onClick={onAddOccurrence}
132 4e91ed40 Dominik Poch
                    />
133
                    <Button
134
                        type="text"
135
                        shape="circle"
136
                        icon={<DownOutlined />}
137
                        onClick={changePropertiesVisibility}
138
                    />
139
                </Col>
140 967f45fc Dominik Poch
            </Row>
141
            {visibleProperties && (
142 76242338 Dominik Poch
                <Stack gap={1} className="mb-2">
143 4e91ed40 Dominik Poch
                    <div>Kategorie: {props.tag.category}</div>
144
                    <div>Výskyty:</div>
145
                    {props.tag.occurrences.map((occurrence, index) => {
146 967f45fc Dominik Poch
                        return (
147 6973d036 Lukáš Vlček
                            <Container
148
                                key={index}
149
                                className="shadow-sm"
150
                                style={{
151
                                    backgroundColor:
152
                                        selectedOccurrenceId === occurrence.occurenceId
153
                                            ? '#D8E1E9'
154
                                            : 'white',
155
                                }}
156
                            >
157
                                <Row className="mb-1 mt-1">
158 76242338 Dominik Poch
                                    <Col>
159
                                        <Row>
160 812ee966 Dominik Poch
                                            <Col>Pozice: {occurrence.position}</Col>
161 80244bde Lukáš Vlček
                                            <Col>Délka: {occurrence.length}</Col>
162 812ee966 Dominik Poch
                                        </Row>
163
                                        <Row>
164 80244bde Lukáš Vlček
                                            <i title={occurrence.selectedText ?? ''}>
165
                                                {getTextMaxLength(
166
                                                    occurrence.selectedText ?? '',
167
                                                    35
168
                                                )}
169
                                            </i>
170 76242338 Dominik Poch
                                        </Row>
171
                                        <Row>
172 bcbaa7d3 Dominik Poch
                                            <Col
173
                                                className="d-flex align-items-center"
174
                                                sm="4"
175
                                            >
176 812ee966 Dominik Poch
                                                Poznámka:
177 76242338 Dominik Poch
                                            </Col>
178 bcbaa7d3 Dominik Poch
                                            <Col>
179 812ee966 Dominik Poch
                                                <Input.TextArea
180
                                                    defaultValue={occurrence.note ?? ''}
181 89154cb1 Dominik Poch
                                                    onBlur={onChangeNote(occurrence)}
182 80244bde Lukáš Vlček
                                                    rows={1}
183 76242338 Dominik Poch
                                                />
184
                                            </Col>
185
                                        </Row>
186 bcbaa7d3 Dominik Poch
                                        {occurrence.sentiment && (
187
                                            <Row>
188
                                                <Col
189
                                                    className="d-flex align-items-center"
190
                                                    sm="4"
191
                                                >
192
                                                    Sentiment:
193
                                                </Col>
194
                                                <Col>
195
                                                    <Select
196 812ee966 Dominik Poch
                                                        defaultValue={
197
                                                            occurrence.sentiment
198
                                                        }
199 bcbaa7d3 Dominik Poch
                                                        style={{ width: '100%' }}
200
                                                        onChange={onChangeSentiment(
201
                                                            occurrence
202
                                                        )}
203
                                                    >
204
                                                        <Option
205
                                                            value={ETagSentiment.Positive}
206
                                                        >
207
                                                            Pozitivní
208
                                                        </Option>
209
                                                        <Option
210
                                                            value={ETagSentiment.Neutral}
211
                                                        >
212
                                                            Neutrální
213
                                                        </Option>
214
                                                        <Option
215
                                                            value={ETagSentiment.Negative}
216
                                                        >
217
                                                            Negativní
218
                                                        </Option>
219
                                                    </Select>
220
                                                </Col>
221
                                            </Row>
222
                                        )}
223 76242338 Dominik Poch
                                    </Col>
224 6973d036 Lukáš Vlček
                                    <Col
225
                                        sm="auto"
226
                                        className="d-flex align-items-center flex-column justify-content-sm-evenly"
227
                                    >
228
                                        <Button
229
                                            icon={<EyeOutlined />}
230
                                            onClick={() => {
231
                                                setSelectedOccurrenceId(
232
                                                    occurrence.occurenceId ?? null
233
                                                );
234
                                                setSelectedInstanceId(
235
                                                    props.tag.instanceId
236
                                                );
237
                                            }}
238
                                            title={'Zvýraznit výskyt'}
239
                                        />
240 76242338 Dominik Poch
                                        <Button
241
                                            icon={<DeleteOutlined />}
242
                                            onClick={onDeleteOccurrence(occurrence)}
243
                                            danger
244 6973d036 Lukáš Vlček
                                        />
245 76242338 Dominik Poch
                                    </Col>
246
                                </Row>
247
                            </Container>
248 967f45fc Dominik Poch
                        );
249
                    })}
250 4e91ed40 Dominik Poch
                </Stack>
251 967f45fc Dominik Poch
            )}
252 c73aecde Dominik Poch
        </Container>
253 967f45fc Dominik Poch
    );
254
}