Projekt

Obecné

Profil

Stáhnout (7.67 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Col, Container, Row, Stack } from 'react-bootstrap';
2
import { Tag } from '../types/tag';
3
import { useContext, useState } from 'react';
4
import 'antd/dist/antd.css';
5
import { Button, Select } from 'antd';
6
import { DownOutlined, PlusOutlined, TagOutlined } from '@ant-design/icons';
7
import { AnnotationContext } from '../../contexts/AnnotationContext';
8
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

    
14
const { Option } = Select;
15

    
16
/**
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
export function AnnotationItem(props: { tag: Tag }) {
22
    /**
23
     * Should properties of this annotation be visible?
24
     */
25
    const [detailsVisible, setDetailsVisible] = useState(false);
26

    
27
    /**
28
     * Context that manages annotations.
29
     */
30
    const {
31
        addOccurrence,
32
        selectedInstanceId,
33
        setSelectedOccurrenceId,
34
        setSelectedInstanceId,
35
        isFinal,
36
        makeOccurrenceFinal,
37
    } = useContext(AnnotationContext);
38

    
39
    /**
40
     * Adds new occurrence of this annotation to the context.
41
     */
42
    const onAddOccurrence = () => {
43
        addOccurrence(props.tag);
44
    };
45

    
46
    /**
47
     * Changes visibility of properties of this annotation.
48
     */
49
    const changePropertiesVisibility = () => {
50
        setDetailsVisible(!detailsVisible);
51
    };
52

    
53
    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
    return (
73
        <Container
74
            className="border rounded"
75
            style={{
76
                backgroundColor: getBackgroundColor(),
77
            }}
78
        >
79
            <Row className="">
80
                <Col sm="auto" className="d-flex align-items-center">
81
                    <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
                            backgroundColor: getBackgroundColor(),
91
                        }}
92
                        title={'Zvýraznit značky'}
93
                    />
94
                </Col>
95
                <Col className="d-flex align-items-center">
96
                    {props.tag.tagName}
97
                    {props.tag.subtagName ? ' (' + props.tag.subtagName + ')' : ''}
98
                </Col>
99
                <Col sm="auto">
100
                    {props.tag.occurrences.length > 1 && (
101
                        <ABadge style={BadgeStyle.GENERAL}>
102
                            {props.tag.occurrences.length}
103
                        </ABadge>
104
                    )}
105

    
106
                    <Button
107
                        type="text"
108
                        shape="circle"
109
                        icon={<PlusOutlined />}
110
                        onClick={onAddOccurrence}
111
                    />
112
                    <Button
113
                        type="text"
114
                        shape="circle"
115
                        icon={<DownOutlined />}
116
                        onClick={changePropertiesVisibility}
117
                    />
118
                </Col>
119
            </Row>
120
            {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
                                            >
156
                                                {getNameTruncated(u)}
157
                                            </span>
158
                                        ))}
159
                                    </div>
160
                                    <div>
161
                                        <Button
162
                                            onClick={() => {
163
                                                ShowConfirm(() => {
164
                                                    makeOccurrenceFinal(
165
                                                        props.tag.occurrences[0]
166
                                                    );
167
                                                }, 'označit toto řešení jako správné');
168
                                            }}
169
                                        >
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
                        );
192
                    })}
193
                </Stack>
194
            )}
195
        </Container>
196
    );
197
}
(1-1/8)