Projekt

Obecné

Profil

Stáhnout (7.67 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 b80a6919 Lukáš Vlček
import { useContext, useState } from 'react';
4 967f45fc Dominik Poch
import 'antd/dist/antd.css';
5 b80a6919 Lukáš Vlček
import { Button, Select } from 'antd';
6
import { DownOutlined, PlusOutlined, TagOutlined } from '@ant-design/icons';
7 c73aecde Dominik Poch
import { AnnotationContext } from '../../contexts/AnnotationContext';
8 b80a6919 Lukáš Vlček
import { AnnotationOccurrenceItem } from './AnnotationOccurrenceItem';
9
import { COLOR_ALL_OCCURRENCES_ACCEPTED, COLOR_HIGHLIGHTED_TAG } from '../../constants';
10
import { ABadge, BadgeStyle } from '../common/ABadge';
11
import { getNameTruncated } from '../../utils/strings';
12
import { ShowConfirm } from '../../utils/alerts';
13 bcbaa7d3 Dominik Poch
14
const { Option } = Select;
15 967f45fc Dominik Poch
16 acb8a961 Dominik Poch
/**
17
 * Creates a single item in an annotation panel.
18
 * @param props Properties should contain a tag that will be shown in this annotation.
19
 * @returns The item that represents an annotation.
20
 */
21 c73aecde Dominik Poch
export function AnnotationItem(props: { tag: Tag }) {
22 acb8a961 Dominik Poch
    /**
23
     * Should properties of this annotation be visible?
24
     */
25 b80a6919 Lukáš Vlček
    const [detailsVisible, setDetailsVisible] = useState(false);
26 acb8a961 Dominik Poch
27
    /**
28
     * Context that manages annotations.
29
     */
30 c73aecde Dominik Poch
    const {
31
        addOccurrence,
32 6973d036 Lukáš Vlček
        selectedInstanceId,
33
        setSelectedOccurrenceId,
34
        setSelectedInstanceId,
35 b80a6919 Lukáš Vlček
        isFinal,
36
        makeOccurrenceFinal,
37 c73aecde Dominik Poch
    } = useContext(AnnotationContext);
38 967f45fc Dominik Poch
39 acb8a961 Dominik Poch
    /**
40
     * Adds new occurrence of this annotation to the context.
41
     */
42 c73aecde Dominik Poch
    const onAddOccurrence = () => {
43
        addOccurrence(props.tag);
44 4e91ed40 Dominik Poch
    };
45 967f45fc Dominik Poch
46 acb8a961 Dominik Poch
    /**
47
     * Changes visibility of properties of this annotation.
48
     */
49 c73aecde Dominik Poch
    const changePropertiesVisibility = () => {
50 b80a6919 Lukáš Vlček
        setDetailsVisible(!detailsVisible);
51 c73aecde Dominik Poch
    };
52
53 b80a6919 Lukáš Vlček
    function isAllAccepted() {
54
        return props.tag.occurrences.filter((o) => !o.isFinal).length === 0;
55
    }
56
    function getBackgroundColor(): string {
57
        if (selectedInstanceId === props.tag.instanceId) {
58
            // highlighted tag
59
            return COLOR_HIGHLIGHTED_TAG;
60
        }
61
62
        if (isFinal) {
63
            if (isAllAccepted()) {
64
                return COLOR_ALL_OCCURRENCES_ACCEPTED;
65
            } else {
66
                return '#E4C8CC';
67
            }
68
        }
69
70
        return 'white';
71
    }
72 967f45fc Dominik Poch
    return (
73 b80a6919 Lukáš Vlček
        <Container
74
            className="border rounded"
75
            style={{
76
                backgroundColor: getBackgroundColor(),
77
            }}
78
        >
79
            <Row className="">
80 76242338 Dominik Poch
                <Col sm="auto" className="d-flex align-items-center">
81 6973d036 Lukáš Vlček
                    <Button
82
                        icon={<TagOutlined style={{ marginLeft: 0 }} />}
83
                        onClick={() => {
84
                            setSelectedInstanceId(props.tag.instanceId);
85
                            setSelectedOccurrenceId(null);
86
                        }}
87
                        style={{
88
                            border: 'none',
89
                            paddingLeft: 0,
90 b80a6919 Lukáš Vlček
                            backgroundColor: getBackgroundColor(),
91 6973d036 Lukáš Vlček
                        }}
92
                        title={'Zvýraznit značky'}
93
                    />
94 76242338 Dominik Poch
                </Col>
95 4bc99591 Lukáš Vlček
                <Col className="d-flex align-items-center">
96
                    {props.tag.tagName}
97
                    {props.tag.subtagName ? ' (' + props.tag.subtagName + ')' : ''}
98
                </Col>
99 4e91ed40 Dominik Poch
                <Col sm="auto">
100 b80a6919 Lukáš Vlček
                    {props.tag.occurrences.length > 1 && (
101
                        <ABadge style={BadgeStyle.GENERAL}>
102
                            {props.tag.occurrences.length}
103
                        </ABadge>
104
                    )}
105
106 4e91ed40 Dominik Poch
                    <Button
107
                        type="text"
108
                        shape="circle"
109
                        icon={<PlusOutlined />}
110 c73aecde Dominik Poch
                        onClick={onAddOccurrence}
111 4e91ed40 Dominik Poch
                    />
112
                    <Button
113
                        type="text"
114
                        shape="circle"
115
                        icon={<DownOutlined />}
116
                        onClick={changePropertiesVisibility}
117
                    />
118
                </Col>
119 967f45fc Dominik Poch
            </Row>
120 b80a6919 Lukáš Vlček
            {isFinal && !isAllAccepted() && (
121
                <>
122
                    <Row
123
                        style={{
124
                            backgroundColor: getBackgroundColor(),
125
                        }}
126
                    >
127
                        <Col sm="12" className="d-flex align-items-center">
128
                            {props.tag.occurrences.length === 1 && (
129
                                <div
130
                                    style={{
131
                                        display: 'flex',
132
                                        flexDirection: 'row',
133
                                        width: '100%',
134
                                        justifyContent: 'space-between',
135
                                    }}
136
                                >
137
                                    <div style={{ width: '60%' }}>
138
                                        Anotovali:{' '}
139
                                        {props.tag.occurrences[0].users?.map((u) => (
140
                                            <span
141
                                                title={
142
                                                    u.name +
143
                                                    ' ' +
144
                                                    u.surname +
145
                                                    ' (' +
146
                                                    u.username +
147
                                                    ')'
148
                                                }
149
                                                key={
150
                                                    props.tag.occurrences[0].occurenceId +
151
                                                    '.' +
152
                                                    u.id
153
                                                }
154
                                                style={{ marginRight: 10 }}
155 bcbaa7d3 Dominik Poch
                                            >
156 b80a6919 Lukáš Vlček
                                                {getNameTruncated(u)}
157
                                            </span>
158
                                        ))}
159
                                    </div>
160
                                    <div>
161 6973d036 Lukáš Vlček
                                        <Button
162
                                            onClick={() => {
163 b80a6919 Lukáš Vlček
                                                ShowConfirm(() => {
164
                                                    makeOccurrenceFinal(
165
                                                        props.tag.occurrences[0]
166
                                                    );
167
                                                }, 'označit toto řešení jako správné');
168 6973d036 Lukáš Vlček
                                            }}
169 b80a6919 Lukáš Vlček
                                        >
170
                                            Přijmout toto řešení
171
                                        </Button>
172
                                    </div>
173
                                </div>
174
                            )}
175
                        </Col>
176
                    </Row>
177
                </>
178
            )}
179
180
            {detailsVisible && (
181
                <Stack gap={1} className="mb-2">
182
                    <div>Kategorie: {props.tag.category}</div>
183
                    <div>Výskyty (části):</div>
184
                    {props.tag.occurrences.map((occurrence, index) => {
185
                        return (
186
                            <AnnotationOccurrenceItem
187
                                occurrence={occurrence}
188
                                tag={props.tag}
189
                                key={'occ-' + occurrence.occurenceId}
190
                            />
191 967f45fc Dominik Poch
                        );
192
                    })}
193 4e91ed40 Dominik Poch
                </Stack>
194 967f45fc Dominik Poch
            )}
195 c73aecde Dominik Poch
        </Container>
196 967f45fc Dominik Poch
    );
197
}