|
| 1 | +import {useState} from 'react'; |
| 2 | +import cn from 'bem-cn-lite'; |
| 3 | +import {Popover} from '@gravity-ui/uikit'; |
| 4 | + |
| 5 | +import {formatBytesCustom, IBytesSizes, IProcessSpeedStats} from '../../utils/bytesParsers'; |
| 6 | + |
| 7 | +import './SpeedMultiMeter.scss'; |
| 8 | + |
| 9 | +import i18n from './i18n'; |
| 10 | + |
| 11 | +const b = cn('speed-multimeter'); |
| 12 | + |
| 13 | +interface SpeedMultiMeterProps { |
| 14 | + data?: IProcessSpeedStats; |
| 15 | + speedSize?: IBytesSizes; |
| 16 | + withValue?: boolean; |
| 17 | + withPopover?: boolean; |
| 18 | +} |
| 19 | + |
| 20 | +export const SpeedMultiMeter = ({ |
| 21 | + data, |
| 22 | + speedSize = 'kb', |
| 23 | + withValue = true, |
| 24 | + withPopover = true, |
| 25 | +}: SpeedMultiMeterProps) => { |
| 26 | + const {perMinute = 0, perHour = 0, perDay = 0} = data || {}; |
| 27 | + const rawValues = [perMinute, perHour, perDay]; |
| 28 | + |
| 29 | + const formatValue = (value: number) => |
| 30 | + formatBytesCustom({value: value, size: speedSize, isSpeed: true}); |
| 31 | + |
| 32 | + const formattedValues = [ |
| 33 | + {value: formatValue(perMinute), label: i18n('perMinute')}, |
| 34 | + {value: formatValue(perHour), label: i18n('perHour')}, |
| 35 | + {value: formatValue(perDay), label: i18n('perDay')}, |
| 36 | + ]; |
| 37 | + |
| 38 | + const [valueToDisplay, setValueToDisplay] = useState(perMinute); |
| 39 | + const [highlightedValueIndex, setHighlightedValueIndex] = useState(withValue ? 0 : undefined); |
| 40 | + const [highlightedContainerIndex, setHighlightedContainerIndex] = useState< |
| 41 | + number | undefined |
| 42 | + >(); |
| 43 | + |
| 44 | + const onEnterDiagram = (values: number[], index: number) => { |
| 45 | + setValueToDisplay(values[index]); |
| 46 | + setHighlightedValueIndex(index); |
| 47 | + setHighlightedContainerIndex(index); |
| 48 | + }; |
| 49 | + |
| 50 | + const onLeaveDiagram = () => { |
| 51 | + setValueToDisplay(perMinute); |
| 52 | + setHighlightedValueIndex(withValue ? 0 : undefined); |
| 53 | + setHighlightedContainerIndex(undefined); |
| 54 | + }; |
| 55 | + |
| 56 | + const isValueHighlighted = (index: number) => highlightedValueIndex === index; |
| 57 | + const isContainerHighlighted = (index: number) => highlightedContainerIndex === index; |
| 58 | + |
| 59 | + const getModifier = (flag: boolean) => (flag ? {color: 'primary'} : {color: 'secondary'}); |
| 60 | + |
| 61 | + const renderValues = () => { |
| 62 | + const max = Math.max(...rawValues, 0) || 1; |
| 63 | + |
| 64 | + return rawValues.map((value, index) => ( |
| 65 | + <div |
| 66 | + key={index} |
| 67 | + className={b('bar-container', { |
| 68 | + highlighted: isContainerHighlighted(index), |
| 69 | + })} |
| 70 | + onMouseEnter={onEnterDiagram.bind(null, rawValues, index)} |
| 71 | + > |
| 72 | + <div |
| 73 | + className={b('bar', { |
| 74 | + color: isValueHighlighted(index) ? 'dark' : 'light', |
| 75 | + })} |
| 76 | + style={{width: `${(100 * value) / max}%`}} |
| 77 | + /> |
| 78 | + </div> |
| 79 | + )); |
| 80 | + }; |
| 81 | + |
| 82 | + const renderPopoverContent = () => { |
| 83 | + return ( |
| 84 | + <div className={b('popover-content')}> |
| 85 | + <span className={b('popover-header')}>{i18n('averageSpeed')}</span> |
| 86 | + {formattedValues.map((formattedValue, index) => ( |
| 87 | + <span |
| 88 | + key={index} |
| 89 | + className={b('popover-row', getModifier(isValueHighlighted(index)))} |
| 90 | + > |
| 91 | + {`${formattedValue.label}: ${formattedValue.value}`} |
| 92 | + </span> |
| 93 | + ))} |
| 94 | + </div> |
| 95 | + ); |
| 96 | + }; |
| 97 | + |
| 98 | + return ( |
| 99 | + <div className={b()}> |
| 100 | + <div className={b('content')}> |
| 101 | + {withValue && ( |
| 102 | + <div className={b('displayed-value')}>{formatValue(valueToDisplay)}</div> |
| 103 | + )} |
| 104 | + <div className={b('popover-container')}> |
| 105 | + <Popover |
| 106 | + content={renderPopoverContent()} |
| 107 | + placement={'bottom'} |
| 108 | + disabled={!withPopover} |
| 109 | + hasArrow={true} |
| 110 | + size="s" |
| 111 | + > |
| 112 | + <div className={b('bars')} onMouseLeave={onLeaveDiagram}> |
| 113 | + {renderValues()} |
| 114 | + </div> |
| 115 | + </Popover> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + ); |
| 120 | +}; |
0 commit comments