|
| 1 | +/* eslint-disable sonarjs/no-nested-template-literals */ |
1 | 2 | import React, { useContext } from 'react'
|
2 |
| -import { EuiButton, EuiButtonIcon, EuiToolTip } from '@elastic/eui' |
| 3 | +import { EuiButtonIcon, EuiIcon, EuiTextColor, EuiToolTip } from '@elastic/eui' |
3 | 4 | import cx from 'classnames'
|
4 | 5 |
|
5 | 6 | import {
|
@@ -31,16 +32,14 @@ import { RedisModuleDto } from 'apiSrc/modules/instances/dto/database-instance.d
|
31 | 32 | import styles from './styles.module.scss'
|
32 | 33 |
|
33 | 34 | export interface Props {
|
34 |
| - modules: RedisModuleDto[]; |
35 |
| - inCircle?: boolean; |
36 |
| - dark?: boolean; |
| 35 | + modules: RedisModuleDto[] |
| 36 | + inCircle?: boolean |
| 37 | + dark?: boolean |
| 38 | + highlight?: boolean |
| 39 | + maxViewModules?: number |
| 40 | + tooltipTitle?: React.ReactNode |
37 | 41 | }
|
38 | 42 |
|
39 |
| -interface ITooltipProps { |
40 |
| - icon: any; |
41 |
| - content: any; |
42 |
| - abbreviation?: string; |
43 |
| -} |
44 | 43 | export const modulesDefaultInit = {
|
45 | 44 | [RedisDefaultModules.AI]: {
|
46 | 45 | iconDark: RedisAIDark,
|
@@ -79,68 +78,104 @@ export const modulesDefaultInit = {
|
79 | 78 | },
|
80 | 79 | }
|
81 | 80 |
|
82 |
| -const DatabaseListModules = React.memo(({ modules, inCircle }: Props) => { |
| 81 | +const DatabaseListModules = React.memo(({ modules, inCircle, highlight, tooltipTitle, maxViewModules }: Props) => { |
83 | 82 | const { theme } = useContext(ThemeContext)
|
84 | 83 |
|
| 84 | + const mainContent = [] |
| 85 | + |
85 | 86 | const handleCopy = (text = '') => {
|
86 | 87 | navigator?.clipboard?.writeText(text)
|
87 | 88 | }
|
88 | 89 |
|
89 |
| - const Tooltip = ({ icon, content, abbreviation }: ITooltipProps) => ( |
90 |
| - <> |
91 |
| - <EuiToolTip |
92 |
| - content={content} |
93 |
| - position="top" |
94 |
| - display="inlineBlock" |
95 |
| - anchorClassName={cx({ [styles.anchorCircleIcon]: inCircle })} |
96 |
| - > |
97 |
| - {icon ? ( |
98 |
| - <EuiButtonIcon |
99 |
| - iconType={icon} |
100 |
| - className={cx(styles.icon, { [styles.circle]: inCircle })} |
101 |
| - onClick={() => handleCopy(content)} |
102 |
| - data-testid={`${content}_module`} |
103 |
| - aria-labelledby={`${content}_module`} |
104 |
| - /> |
105 |
| - ) : ( |
106 |
| - <EuiButton |
107 |
| - className={cx(styles.icon, { [styles.circle]: inCircle })} |
108 |
| - onClick={() => handleCopy(content)} |
109 |
| - data-testid={`${content}_module`} |
110 |
| - aria-labelledby={`${content}_module`} |
111 |
| - > |
112 |
| - {abbreviation} |
113 |
| - </EuiButton> |
114 |
| - )} |
115 |
| - </EuiToolTip> |
116 |
| - </> |
117 |
| - ) |
118 |
| - |
119 |
| - const modulesRender = modules?.map(({ name: propName, semanticVersion = '', version = '' }) => { |
| 90 | + const newModules = modules?.map(({ name: propName, semanticVersion = '', version = '' }) => { |
120 | 91 | const moduleName = modulesDefaultInit[propName]?.text || propName
|
121 | 92 |
|
122 | 93 | const { abbreviation = '', name = moduleName } = getModule(moduleName)
|
123 | 94 |
|
124 | 95 | const moduleAlias = truncateText(name, 50)
|
125 | 96 | // eslint-disable-next-line sonarjs/no-nested-template-literals
|
126 |
| - const content = `${moduleAlias}${semanticVersion || version ? ` v. ${semanticVersion || version}` : ''}` |
127 | 97 | let icon = modulesDefaultInit[propName]?.[theme === Theme.Dark ? 'iconDark' : 'iconLight']
|
| 98 | + const content = `${moduleAlias}${semanticVersion || version ? ` v. ${semanticVersion || version}` : ''}` |
128 | 99 |
|
129 | 100 | if (!icon && !abbreviation) {
|
130 | 101 | icon = theme === Theme.Dark ? UnknownDark : UnknownLight
|
131 | 102 | }
|
132 | 103 |
|
133 |
| - return ( |
134 |
| - <Tooltip |
135 |
| - key={moduleName} |
136 |
| - icon={icon} |
137 |
| - abbreviation={abbreviation} |
138 |
| - content={content} |
139 |
| - /> |
140 |
| - ) |
| 104 | + mainContent.push({ icon, content, abbreviation }) |
| 105 | + |
| 106 | + return { |
| 107 | + moduleName, |
| 108 | + icon, |
| 109 | + abbreviation, |
| 110 | + content |
| 111 | + } |
141 | 112 | })
|
142 | 113 |
|
143 |
| - return <>{modulesRender}</> |
| 114 | + // set count of hidden modules |
| 115 | + if (maxViewModules && newModules.length > maxViewModules) { |
| 116 | + newModules.length = maxViewModules |
| 117 | + newModules.push({ |
| 118 | + icon: null, |
| 119 | + content: '', |
| 120 | + moduleName: '', |
| 121 | + abbreviation: `+${modules.length - maxViewModules}` |
| 122 | + }) |
| 123 | + } |
| 124 | + |
| 125 | + const Content = mainContent.map(({ icon, content, abbreviation = '' }) => ( |
| 126 | + <div className={styles.tooltipItem}> |
| 127 | + {!!icon && (<EuiIcon type={icon} style={{ marginRight: 10 }} />)} |
| 128 | + {!icon && ( |
| 129 | + <EuiTextColor |
| 130 | + className={cx(styles.icon, styles.abbr)} |
| 131 | + style={{ marginRight: 10 }} |
| 132 | + > |
| 133 | + {abbreviation} |
| 134 | + </EuiTextColor> |
| 135 | + )} |
| 136 | + {!!content && (<EuiTextColor className={cx(styles.tooltipItemText)}>{content}</EuiTextColor>)} |
| 137 | + <br /> |
| 138 | + </div> |
| 139 | + )) |
| 140 | + |
| 141 | + return ( |
| 142 | + <div className={cx(styles.container, { |
| 143 | + [styles.highlight]: highlight, |
| 144 | + [styles.containerCircle]: inCircle, |
| 145 | + })} |
| 146 | + > |
| 147 | + <EuiToolTip |
| 148 | + position="bottom" |
| 149 | + title={tooltipTitle ?? undefined} |
| 150 | + display="inlineBlock" |
| 151 | + content={Content} |
| 152 | + > |
| 153 | + <> |
| 154 | + {newModules.map(({ icon, content, abbreviation, moduleName }) => ( |
| 155 | + icon ? ( |
| 156 | + <EuiButtonIcon |
| 157 | + iconType={icon} |
| 158 | + className={cx(styles.icon, { [styles.circle]: inCircle })} |
| 159 | + onClick={() => handleCopy(content)} |
| 160 | + data-testid={`${content}_module`} |
| 161 | + aria-labelledby={`${content}_module`} |
| 162 | + key={moduleName} |
| 163 | + /> |
| 164 | + ) : ( |
| 165 | + <EuiTextColor |
| 166 | + className={cx(styles.icon, styles.abbr, { [styles.circle]: inCircle })} |
| 167 | + onClick={() => handleCopy(content)} |
| 168 | + data-testid={`${content}_module`} |
| 169 | + aria-labelledby={`${content}_module`} |
| 170 | + key={moduleName} |
| 171 | + > |
| 172 | + {abbreviation} |
| 173 | + </EuiTextColor> |
| 174 | + )))} |
| 175 | + </> |
| 176 | + </EuiToolTip> |
| 177 | + </div> |
| 178 | + ) |
144 | 179 | })
|
145 | 180 |
|
146 | 181 | export default DatabaseListModules
|
0 commit comments