import { forwardRef, useId, useMemo } from 'react' import { BREAKPOINTS } from '../../breakpoints/breakPoints' import { useBreakpoints } from '../../hooks/useBreakPoints' import { useResponsiveTokens } from '../../hooks/useResponsiveTokens' import Block from '../Primitives/Block/Block' import { StatCardV2TokensType } from './statcardV2.tokens' import { StatCardV2ArrowDirection, StatCardV2ChangeType, StatCardV2Props, StatCardV2Variant, } from './statcardV2.types' import { filterBlockedProps } from '../../utils/prop-helpers' import { ChartV2 } from '../ChartsV2' import { ProgressBar, ProgressBarSize, ProgressBarVariant, } from '../ProgressBar' import StatCardV2Skeleton from './StatCardV2Skeleton' import StatCardV2Title from './StatCardV2Title' import StatCardV2Change from './StatCardV2Change' import StatCardV2Subtitle from './StatCardV2Subtitle' import StatCardV2Value, { renderVariantFallbackValue } from './StatCardV2Value' import { buildStatCardV2ChartOptions } from './StatCardV2.chartConfig' import StatCardV2NoData from './StatCardV2NoData' import { buildStatCardV2AriaLabel } from './utils' import { SingleSelectV2 } from '../SingleSelectV2' import { SelectV2Size, SelectV2Variant } from '../SelectV2' const StatCardV2 = forwardRef( ( { title, titleIcon, helpIconText, variant = StatCardV2Variant.NUMBER, actionIcon, value, valueTooltip, progressValue, change, subtitle, dropdownProps, maxWidth, minWidth, width = '100%', height, options, skeleton, showBorder = true, ...props }, ref ) => { const tokens = useResponsiveTokens('STATCARDV2') const { breakPointLabel } = useBreakpoints(BREAKPOINTS) const isSmallScreen = breakPointLabel === 'sm' const filteredProps = filterBlockedProps(props) const isChartVariant = variant === StatCardV2Variant.CHART const isProgressBarVariant = variant === StatCardV2Variant.PROGRESS_BAR const isNumberVariant = variant === StatCardV2Variant.NUMBER const numberVariantAlignment = isNumberVariant && !isSmallScreen ? 'center' : 'flex-start' const { label, placeholder, items, selected, onSelect } = dropdownProps || {} const resolvedHeight = !isSmallScreen && height ? height : tokens.height const effectiveChangeType = change?.changeType ?? StatCardV2ChangeType.INCREASE const effectiveArrowDirection = change?.arrowDirection ?? StatCardV2ArrowDirection.UP const baseId = useId() const titleId = `${baseId}-title` const valueId = `${baseId}-value` const changeId = `${baseId}-change` const subtitleId = `${baseId}-subtitle` const chartId = `${baseId}-chart` const cardLabel = useMemo( () => buildStatCardV2AriaLabel({ title, value, subtitle, change, }), [title, value, subtitle, change] ) type ChartOptions = { series?: { data?: unknown[] }[] } const chartOptions = options as ChartOptions | undefined const hasChartData = isChartVariant && Array.isArray(chartOptions?.series) && chartOptions.series?.some( (seriesItem) => Array.isArray(seriesItem?.data) && seriesItem.data.length > 0 ) const hasValue = value !== undefined && value !== null && value !== '' const hasProgressValue = progressValue !== undefined && progressValue !== null if ( !skeleton?.show && !hasValue && !change && !hasProgressValue && !hasChartData ) { return ( ) } return ( {skeleton?.show ? ( ) : ( <> {actionIcon && !isSmallScreen && ( {actionIcon} )} {titleIcon && !isSmallScreen && titleIcon} {!isSmallScreen && ( )} {isSmallScreen && items && items.length > 0 && ( {}) } variant={ SelectV2Variant.NO_CONTAINER } size={SelectV2Size.SM} inline={true} /> )} {isChartVariant && (hasChartData ? ( ) : ( renderVariantFallbackValue( tokens, StatCardV2Variant.CHART ) ))} {isProgressBarVariant && (hasProgressValue ? ( ) : ( renderVariantFallbackValue( tokens, StatCardV2Variant.PROGRESS_BAR ) ))} )} ) } ) export default StatCardV2 StatCardV2.displayName = 'StatCardV2'