|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {Button, Flex, Tooltip} from '@gravity-ui/uikit'; |
| 4 | +import {debounce} from 'lodash'; |
| 5 | + |
| 6 | +import {cn} from '../../utils/cn'; |
| 7 | +import type {PreparedVersion} from '../../utils/versions/types'; |
| 8 | + |
| 9 | +import i18n from './i18n'; |
| 10 | + |
| 11 | +import './VersionsBar.scss'; |
| 12 | + |
| 13 | +const b = cn('ydb-versions-bar'); |
| 14 | + |
| 15 | +const TRUNCATION_THRESHOLD = 4; |
| 16 | +// One more line for Show more / Hide button |
| 17 | +const MAX_DISPLAYED_VERSIONS = TRUNCATION_THRESHOLD - 1; |
| 18 | + |
| 19 | +const HOVER_DELAY = 200; |
| 20 | +const TOOLTIP_OPEN_DELAY = 200; |
| 21 | + |
| 22 | +interface VersionsBarProps { |
| 23 | + preparedVersions: PreparedVersion[]; |
| 24 | +} |
| 25 | + |
| 26 | +export function VersionsBar({preparedVersions}: VersionsBarProps) { |
| 27 | + const shouldTruncateVersions = preparedVersions.length > TRUNCATION_THRESHOLD; |
| 28 | + |
| 29 | + const [hoveredVersion, setHoveredVersion] = React.useState<string | undefined>(); |
| 30 | + const [allVersionsDisplayed, setAllVersionsDisplayed] = React.useState<boolean>(false); |
| 31 | + |
| 32 | + const displayedVersions = React.useMemo(() => { |
| 33 | + const total = preparedVersions.reduce((acc, item) => acc + (item.count || 0), 0); |
| 34 | + |
| 35 | + return preparedVersions.map((item) => { |
| 36 | + return { |
| 37 | + value: ((item.count || 0) / total) * 100, |
| 38 | + color: item.color, |
| 39 | + version: item.version, |
| 40 | + count: item.count, |
| 41 | + }; |
| 42 | + }); |
| 43 | + }, [preparedVersions]); |
| 44 | + |
| 45 | + const truncatedDisplayedVersions = React.useMemo(() => { |
| 46 | + if (allVersionsDisplayed) { |
| 47 | + return preparedVersions; |
| 48 | + } |
| 49 | + |
| 50 | + return shouldTruncateVersions |
| 51 | + ? preparedVersions.slice(0, MAX_DISPLAYED_VERSIONS) |
| 52 | + : preparedVersions; |
| 53 | + }, [allVersionsDisplayed, preparedVersions, shouldTruncateVersions]); |
| 54 | + |
| 55 | + const handleShowAllVersions = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => { |
| 56 | + event.preventDefault(); |
| 57 | + event.stopPropagation(); |
| 58 | + setAllVersionsDisplayed(true); |
| 59 | + }; |
| 60 | + const handleHideAllVersions = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => { |
| 61 | + event.preventDefault(); |
| 62 | + event.stopPropagation(); |
| 63 | + setAllVersionsDisplayed(false); |
| 64 | + }; |
| 65 | + |
| 66 | + const renderButton = () => { |
| 67 | + if (!shouldTruncateVersions) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + const truncatedVersionsCount = preparedVersions.length - MAX_DISPLAYED_VERSIONS; |
| 72 | + |
| 73 | + if (allVersionsDisplayed) { |
| 74 | + return ( |
| 75 | + <Button view="flat-secondary" size={'s'} onClick={handleHideAllVersions}> |
| 76 | + {i18n('action_hide', { |
| 77 | + count: truncatedVersionsCount, |
| 78 | + })} |
| 79 | + </Button> |
| 80 | + ); |
| 81 | + } else { |
| 82 | + return ( |
| 83 | + <Button view="flat-secondary" size={'s'} onClick={handleShowAllVersions}> |
| 84 | + {i18n('action_show_more', { |
| 85 | + count: truncatedVersionsCount, |
| 86 | + })} |
| 87 | + </Button> |
| 88 | + ); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + const handleMouseEnter = React.useMemo(() => { |
| 93 | + return debounce((version: string) => { |
| 94 | + setHoveredVersion(version); |
| 95 | + }, HOVER_DELAY); |
| 96 | + }, []); |
| 97 | + |
| 98 | + const handleMouseLeave = () => { |
| 99 | + handleMouseEnter.cancel(); |
| 100 | + setHoveredVersion(undefined); |
| 101 | + }; |
| 102 | + |
| 103 | + const isDimmed = (version: string) => { |
| 104 | + return hoveredVersion && hoveredVersion !== version; |
| 105 | + }; |
| 106 | + |
| 107 | + return ( |
| 108 | + <Flex gap={2} direction={'column'} className={b(null)} wrap> |
| 109 | + <Flex className={b('bar')} grow={1} gap={0.5}> |
| 110 | + {displayedVersions.map((item) => ( |
| 111 | + <Tooltip |
| 112 | + key={item.version} |
| 113 | + content={ |
| 114 | + <React.Fragment> |
| 115 | + {i18n('tooltip_nodes', {count: item.count})} |
| 116 | + <br /> |
| 117 | + {item.version} |
| 118 | + </React.Fragment> |
| 119 | + } |
| 120 | + placement={'top-start'} |
| 121 | + openDelay={TOOLTIP_OPEN_DELAY} |
| 122 | + > |
| 123 | + <span |
| 124 | + onMouseEnter={() => { |
| 125 | + handleMouseEnter(item.version); |
| 126 | + }} |
| 127 | + onMouseLeave={handleMouseLeave} |
| 128 | + className={b('version', {dimmed: isDimmed(item.version)})} |
| 129 | + style={{backgroundColor: item.color, width: `${item.value}%`}} |
| 130 | + /> |
| 131 | + </Tooltip> |
| 132 | + ))} |
| 133 | + </Flex> |
| 134 | + |
| 135 | + <Flex gap={0.5} direction={'column'}> |
| 136 | + {truncatedDisplayedVersions.map((item) => ( |
| 137 | + <Tooltip |
| 138 | + key={item.version} |
| 139 | + content={i18n('tooltip_nodes', {count: item.count})} |
| 140 | + placement={'bottom-end'} |
| 141 | + openDelay={TOOLTIP_OPEN_DELAY} |
| 142 | + > |
| 143 | + <Flex gap={1} alignItems={'center'} className={b('titles-wrapper')}> |
| 144 | + <svg |
| 145 | + xmlns="http://www.w3.org/2000/svg" |
| 146 | + width="6" |
| 147 | + height="6" |
| 148 | + viewBox="0 0 6 6" |
| 149 | + fill="none" |
| 150 | + className={b('version-icon', {dimmed: isDimmed(item.version)})} |
| 151 | + > |
| 152 | + <circle cx="3" cy="3" r="3" fill={item.color} /> |
| 153 | + </svg> |
| 154 | + <div |
| 155 | + className={b('title', {dimmed: isDimmed(item.version)})} |
| 156 | + onMouseEnter={() => { |
| 157 | + handleMouseEnter(item.version); |
| 158 | + }} |
| 159 | + onMouseLeave={handleMouseLeave} |
| 160 | + > |
| 161 | + {item.version} |
| 162 | + </div> |
| 163 | + </Flex> |
| 164 | + </Tooltip> |
| 165 | + ))} |
| 166 | + <Flex>{renderButton()}</Flex> |
| 167 | + </Flex> |
| 168 | + </Flex> |
| 169 | + ); |
| 170 | +} |
0 commit comments