import React, { memo, useMemo } from 'react'; import type { SyntheticEvent } from 'react'; import Card from '@mui/material/Card'; import Typography from '@mui/material/Typography'; import Tooltip from '@mui/material/Tooltip'; import Grid from '@mui/material/Grid'; import Box from '@mui/material/Box'; import Button from '@mui/material/Button'; import type { ButtonProps } from '@mui/material/Button'; import Divider from '@mui/material/Divider'; import { ASSETS_URL } from '../../../consts/common'; import { HorizontalStackedBars } from '../../horizontal-stacked-bar'; import type { HorizontalStackedBarsProps } from '../../horizontal-stacked-bar'; import clsx from 'clsx'; import { ReactSVG } from 'react-svg'; import createClasses from './styles'; import { Link } from '../../@navigation/link'; type ArrayLengthMutationKeys = 'splice' | 'push' | 'pop' | 'shift' | 'unshift'; type FixedLengthArray]> = Pick< TObj, Exclude > & { readonly length: L; [I: number]: T; [Symbol.iterator]: () => IterableIterator; }; export interface InfoCardWithBarsProps { /** * Callback on clicking the top right button. */ handleClick?: (e: SyntheticEvent) => void; /** * Url for top right link. */ href?: string; /** * Text for the top right button. */ buttonText?: string; /** * The width of the component. */ rootWidth?: string; /** * Title of the component. */ title?: string; /** * Subtitle of the component. */ subtitle?: string; /** * The data to display inside the component. */ data: { textual: FixedLengthArray<{ text: string; value: string | number }, 3>; chart: HorizontalStackedBarsProps['data']; }; /** * The error message in case of no data. */ errorMessage?: string; /** * Text for the tooltip on the right side of the title. */ titleTooltipText?: string; /** * Text for the button tooltip. */ buttonTooltipText?: string; } const InfoCardWithBars = (props: InfoCardWithBarsProps) => { const { subtitle, title, data, rootWidth, buttonText, handleClick, errorMessage, titleTooltipText, buttonTooltipText, href } = props; const classes = createClasses({ rootWidth: rootWidth || '' }); const dataWithClasses = useMemo( () => data.chart.map(singleData => { const { progressBarProps } = singleData; const { classes: progressBarClasses } = progressBarProps; return { ...singleData, progressBarProps: { ...singleData.progressBarProps, classes: { marginTop: clsx(classes.marginTop, progressBarClasses?.marginTop), marginBottom: clsx(classes.marginBottom, progressBarClasses?.Bottom), ...progressBarClasses } } }; }), [classes.marginBottom, classes.marginTop, data.chart] ); const buttonProps: ButtonProps = useMemo( () => ({ color: 'primary', variant: 'text', onClick: handleClick }), [handleClick] ); return ( {title} {titleTooltipText && (
)}
{(typeof handleClick === 'function' || typeof href === 'string') && ( {buttonTooltipText ? (
) : ( {buttonText} )}
)}
{subtitle}
{data.chart.length > 0 ? ( <> {data.textual.map((singleData, index) => { const { text, value } = singleData; if (index === 1) { return ( {text} {value} ); } return ( {text} {value} ); })} ) : ( {errorMessage} )}
); }; InfoCardWithBars.defaultProps = { title: 'Active sessions', subtitle: '', rootWidth: '537px', buttonText: 'Expand', errorMessage: '' }; const m = memo(InfoCardWithBars); export { m as InfoCardWithBars };