-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathEntityStatus.tsx
More file actions
157 lines (139 loc) · 5.18 KB
/
EntityStatus.tsx
File metadata and controls
157 lines (139 loc) · 5.18 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import React from 'react';
import {CircleInfo} from '@gravity-ui/icons';
import {Button, ClipboardButton, Icon, Popover, Link as UIKitLink} from '@gravity-ui/uikit';
import {EFlag} from '../../types/api/enums';
import {cn} from '../../utils/cn';
import {InternalLink} from '../InternalLink/InternalLink';
import {StatusIcon} from '../StatusIcon/StatusIcon';
import type {StatusIconMode, StatusIconSize} from '../StatusIcon/StatusIcon';
import './EntityStatus.scss';
const b = cn('entity-status');
interface EntityStatusProps {
status?: EFlag;
name?: string;
renderName?: (name?: string) => React.ReactNode;
label?: string;
path?: string;
iconPath?: string;
size?: StatusIconSize;
mode?: StatusIconMode;
showStatus?: boolean;
externalLink?: boolean;
withLeftTrim?: boolean;
hasClipboardButton?: boolean;
infoPopoverContent?: React.ReactNode;
clipboardButtonAlwaysVisible?: boolean;
className?: string;
}
function defaultRenderName(name?: string) {
return name ?? '';
}
export function EntityStatus({
status = EFlag.Grey,
name = '',
renderName = defaultRenderName,
label,
path,
iconPath,
size = 's',
mode = 'color',
showStatus = true,
externalLink = false,
withLeftTrim = false,
hasClipboardButton,
infoPopoverContent,
clipboardButtonAlwaysVisible = false,
className,
}: EntityStatusProps) {
const [infoIconHovered, setInfoIconHovered] = React.useState(false);
const renderIcon = () => {
if (!showStatus) {
return null;
}
return <StatusIcon className={b('icon')} status={status} size={size} mode={mode} />;
};
const renderStatusLink = (href: string) => {
return (
<UIKitLink target="_blank" href={href}>
{renderIcon()}
</UIKitLink>
);
};
const renderLink = () => {
if (path) {
if (externalLink) {
return (
<UIKitLink className={b('name')} href={path}>
{renderName(name)}
</UIKitLink>
);
}
return (
<InternalLink className={b('name')} to={path}>
{renderName(name)}
</InternalLink>
);
}
return name && <span className={b('name')}>{renderName(name)}</span>;
};
return (
<div className={b(null, className)}>
{iconPath ? renderStatusLink(iconPath) : renderIcon()}
{label && (
<span title={label} className={b('label', {size, state: status.toLowerCase()})}>
{label}
</span>
)}
{(path || name) && (
<div
className={b('wrapper', {
'with-clipboard-button': hasClipboardButton,
'with-info-button': Boolean(infoPopoverContent),
})}
>
<span className={b('link', {'with-left-trim': withLeftTrim})} title={name}>
{renderLink()}
</span>
{(hasClipboardButton || infoPopoverContent) && (
<div
className={b('controls-wrapper', {
visible: clipboardButtonAlwaysVisible || infoIconHovered,
})}
>
{infoPopoverContent && (
<Popover
className={b('info-popover')}
content={infoPopoverContent}
tooltipOffset={[-4, 4]}
placement={['top-start', 'bottom-start']}
onOpenChange={(visible) => setInfoIconHovered(visible)}
>
<Button view="normal" size="xs">
<Icon
data={CircleInfo}
size="12"
className={b('info-icon', {
visible:
clipboardButtonAlwaysVisible || infoIconHovered,
})}
/>
</Button>
</Popover>
)}
{hasClipboardButton && (
<ClipboardButton
text={name}
size="xs"
view="normal"
className={b('clipboard-button', {
visible: clipboardButtonAlwaysVisible || infoIconHovered,
})}
/>
)}
</div>
)}
</div>
)}
</div>
);
}