|
| 1 | +import {Link} from 'react-router-dom'; |
| 2 | +import cn from 'bem-cn-lite'; |
| 3 | + |
| 4 | +import {Icon, Link as UIKitLink} from '@gravity-ui/uikit'; |
| 5 | + |
| 6 | +import {EFlag} from '../../types/api/enums'; |
| 7 | +import circleExclamationIcon from '../../assets/icons/circle-exclamation.svg'; |
| 8 | +import circleInfoIcon from '../../assets/icons/circle-info.svg'; |
| 9 | +import circleTimesIcon from '../../assets/icons/circle-xmark.svg'; |
| 10 | +import triangleExclamationIcon from '../../assets/icons/triangle-exclamation.svg'; |
| 11 | +import {ClipboardButton} from '../ClipboardButton'; |
| 12 | +import './EntityStatus.scss'; |
| 13 | + |
| 14 | +const icons = { |
| 15 | + [EFlag.Blue]: circleInfoIcon, |
| 16 | + [EFlag.Yellow]: circleExclamationIcon, |
| 17 | + [EFlag.Orange]: triangleExclamationIcon, |
| 18 | + [EFlag.Red]: circleTimesIcon, |
| 19 | +}; |
| 20 | + |
| 21 | +const b = cn('entity-status'); |
| 22 | + |
| 23 | +interface EntityStatusProps { |
| 24 | + status?: EFlag; |
| 25 | + name?: string; |
| 26 | + label?: string; |
| 27 | + path?: string; |
| 28 | + iconPath?: string; |
| 29 | + |
| 30 | + size?: 'xs' | 's' | 'm' | 'l'; |
| 31 | + mode?: 'color' | 'icons'; |
| 32 | + |
| 33 | + showStatus?: boolean; |
| 34 | + externalLink?: boolean; |
| 35 | + withLeftTrim?: boolean; |
| 36 | + |
| 37 | + hasClipboardButton?: boolean; |
| 38 | + clipboardButtonAlwaysVisible?: boolean; |
| 39 | + |
| 40 | + className?: string; |
| 41 | +} |
| 42 | + |
| 43 | +export function EntityStatus({ |
| 44 | + status = EFlag.Grey, |
| 45 | + name = '', |
| 46 | + label, |
| 47 | + path, |
| 48 | + iconPath, |
| 49 | + |
| 50 | + size = 's', |
| 51 | + mode = 'color', |
| 52 | + |
| 53 | + showStatus = true, |
| 54 | + externalLink = false, |
| 55 | + withLeftTrim = false, |
| 56 | + |
| 57 | + hasClipboardButton, |
| 58 | + clipboardButtonAlwaysVisible = false, |
| 59 | + |
| 60 | + className, |
| 61 | +}: EntityStatusProps) { |
| 62 | + const renderIcon = () => { |
| 63 | + if (!showStatus) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + const modifiers = {state: status.toLowerCase(), size}; |
| 68 | + |
| 69 | + if (mode === 'icons' && status in icons) { |
| 70 | + return ( |
| 71 | + <Icon |
| 72 | + className={b('status-icon', modifiers)} |
| 73 | + data={icons[status as keyof typeof icons]} |
| 74 | + /> |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + return <div className={b('status-color', modifiers)} />; |
| 79 | + }; |
| 80 | + const renderStatusLink = () => { |
| 81 | + return ( |
| 82 | + <UIKitLink target="_blank" href={iconPath}> |
| 83 | + {renderIcon()} |
| 84 | + </UIKitLink> |
| 85 | + ); |
| 86 | + }; |
| 87 | + const renderLink = () => { |
| 88 | + if (externalLink) { |
| 89 | + return ( |
| 90 | + <UIKitLink className={b('name')} href={path}> |
| 91 | + {name} |
| 92 | + </UIKitLink> |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + return path ? ( |
| 97 | + <Link className={b('name')} to={path}> |
| 98 | + {name} |
| 99 | + </Link> |
| 100 | + ) : ( |
| 101 | + name && <span className={b('name')}>{name}</span> |
| 102 | + ); |
| 103 | + }; |
| 104 | + return ( |
| 105 | + <div className={b(null, className)} title={name}> |
| 106 | + {iconPath ? renderStatusLink() : renderIcon()} |
| 107 | + {label && ( |
| 108 | + <span title={label} className={b('label', {size, state: status.toLowerCase()})}> |
| 109 | + {label} |
| 110 | + </span> |
| 111 | + )} |
| 112 | + <span className={b('link', {'with-left-trim': withLeftTrim})}>{renderLink()}</span> |
| 113 | + {hasClipboardButton && ( |
| 114 | + <ClipboardButton |
| 115 | + text={name} |
| 116 | + size="s" |
| 117 | + className={b('clipboard-button', { |
| 118 | + visible: clipboardButtonAlwaysVisible, |
| 119 | + })} |
| 120 | + /> |
| 121 | + )} |
| 122 | + </div> |
| 123 | + ); |
| 124 | +} |
0 commit comments