Projekt

Obecné

Profil

Stáhnout (4.87 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Container, Row, Stack } from 'react-bootstrap';
2
import { TagInfo } from '../../api';
3
import { Button, Tooltip } from 'antd';
4
import { DownOutlined, QuestionCircleOutlined, TagOutlined } from '@ant-design/icons';
5
import React, { useContext, useState } from 'react';
6
import { SubTagItem } from './SubTagItem';
7
import { TagCategoryContext } from '../../contexts/TagCategoryContext';
8
import { AnnotationContext } from '../../contexts/AnnotationContext';
9

    
10
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
11
import { faAngleDown, faAngleUp, faTag } from '@fortawesome/free-solid-svg-icons';
12
import { faCircleQuestion } from '@fortawesome/free-regular-svg-icons';
13
import styles from '/styles/Icon.module.scss';
14

    
15
/**
16
 * Creates a single tag item in a tag panel.
17
 * @param props Properties should contain the tag.
18
 * @returns The item that represents the tag.
19
 */
20
export function TagItem(props: { tag: TagInfo }) {
21
    /**
22
     * Should sub tags be visible?
23
     */
24
    const [visibleSubTags, setVisibleSubTags] = useState(false);
25

    
26
    /**
27
     * Tag context.
28
     */
29
    const { selectTag } = useContext(TagCategoryContext);
30

    
31
    /**
32
     * Annotation context
33
     */
34
    const { submitting } = useContext(AnnotationContext);
35

    
36
    /**
37
     * Button icon.
38
     */
39
    const [buttonIcon, setButtonIcon] = useState<React.ReactNode>(<DownOutlined />);
40

    
41
    /**
42
     * Changes visibility of sub tags.
43
     */
44
    const changeSubTagsVisibility = () => {
45
        setButtonIcon(
46
            visibleSubTags ? (
47
                <FontAwesomeIcon icon={faAngleDown} className={styles.iconLeft} />
48
            ) : (
49
                <FontAwesomeIcon icon={faAngleUp} className={styles.iconLeft} />
50
            )
51
        );
52
        setVisibleSubTags(!visibleSubTags);
53
    };
54

    
55
    /**
56
     * Selects the tag.
57
     */
58
    const onSelectTag = () => {
59
        selectTag(props.tag, null);
60
    };
61

    
62
    /**
63
     * Does tag have sub tags?
64
     */
65
    const hasSubTags = (props.tag.subTags?.length ?? 0) > 0;
66

    
67
    return (
68
        <Container>
69
            <Row>
70
                {hasSubTags && (
71
                    <Button
72
                        onClick={changeSubTagsVisibility}
73
                        className="w-100 text-start rounded"
74
                        disabled={submitting}
75
                    >
76
                        <div className=" align-middle">
77
                            {buttonIcon}
78
                            <FontAwesomeIcon
79
                                icon={faTag}
80
                                style={{ color: props.tag.color ?? 'black' }}
81
                                className={styles.iconMiddle}
82
                            />
83
                            {props.tag.name}
84
                            {props.tag.description && props.tag.description !== '' && (
85
                                <Tooltip
86
                                    title={props.tag.description}
87
                                    key={props.tag.description}
88
                                >
89
                                    <FontAwesomeIcon
90
                                        icon={faCircleQuestion}
91
                                        className={styles.iconRight}
92
                                    />
93
                                </Tooltip>
94
                            )}
95
                        </div>
96
                    </Button>
97
                )}
98
                {!hasSubTags && (
99
                    <Button
100
                        onClick={onSelectTag}
101
                        className="w-100 text-start rounded"
102
                        disabled={submitting}
103
                    >
104
                        <div className=" align-middle">
105
                            <FontAwesomeIcon
106
                                icon={faTag}
107
                                style={{ color: props.tag.color ?? 'black' }}
108
                                className={styles.iconLeft}
109
                            />
110
                            {props.tag.name}
111
                            {props.tag.description && props.tag.description !== '' && (
112
                                <Tooltip
113
                                    title={props.tag.description}
114
                                    key={props.tag.description}
115
                                >
116
                                    <FontAwesomeIcon
117
                                        icon={faCircleQuestion}
118
                                        className={styles.iconRight}
119
                                    />
120
                                </Tooltip>
121
                            )}
122
                        </div>
123
                    </Button>
124
                )}
125
            </Row>
126
            {visibleSubTags && (
127
                <Stack gap={2} className="mt-2">
128
                    {props.tag.subTags?.map((subTag, index) => (
129
                        <SubTagItem tag={props.tag} subTag={subTag} key={index} />
130
                    ))}
131
                </Stack>
132
            )}
133
        </Container>
134
    );
135
}
(7-7/8)