import React, { type ReactNode, useMemo } from 'react'; import cx from 'classnames'; import type { MetricRunInfoDeltaType } from '@bundle-stats/utils'; import { Icon } from '../../ui/icon'; import { HoverCard } from '../../ui/hover-card'; import { Tooltip } from '../../ui/tooltip'; import { Skeleton } from '../../ui/skeleton'; import { FlexStack } from '../../layout/flex-stack'; import { Stack } from '../../layout/stack'; import { Metric } from '../metric'; import { Delta } from '../delta'; import css from './run-info.module.css'; // Separate value and unit const EXTRACT_VALUE_UNIT_PATTERN = /([\d|.|,| ]*)(\w*|%)$/; const getMetricParams = (value: string): [string, string?] => { const matches = value.match(EXTRACT_VALUE_UNIT_PATTERN); if (!matches) { return [value]; } return [matches[1], matches[2]]; }; type RunInfoTitleProps = { className?: string; title?: ReactNode; titleHoverCard?: ReactNode; titleTooltip?: ReactNode; }; const RunInfoTitle = ({ className, title, titleHoverCard, titleTooltip }: RunInfoTitleProps) => ( {title} {titleHoverCard && ( } className={cx(css.titleIcon, css.titleHoverCardIcon)} > {titleHoverCard} )} {titleTooltip && ( )} ); type RunInfoContentProps = { className?: string; loading?: boolean; current?: ReactNode; enhance?: boolean; baseline?: string; delta?: string; deltaPercentage?: string; deltaType?: MetricRunInfoDeltaType; showBaseline?: boolean; showDelta?: boolean; }; const RunInfoContent = ({ className, loading = false, current = '', enhance = false, baseline = '', delta = '', deltaPercentage = '', deltaType = 'NO_CHANGE', showBaseline = true, showDelta = true, }: RunInfoContentProps) => { const currentValueParams: [ReactNode, string?] = useMemo(() => { if (!enhance || typeof current !== 'string') { return [current]; } return getMetricParams(current); }, [current, enhance]); const contentClassName = cx(css.content, className); if (loading) { return ( {showBaseline && } ); } return ( {showDelta && ( {deltaPercentage && ( )} {delta && ( )} )} {showBaseline && ( vs )} ); }; export type RunInfoProps = Pick & Pick< RunInfoContentProps, | 'current' | 'baseline' | 'delta' | 'deltaPercentage' | 'deltaType' | 'showBaseline' | 'showDelta' | 'loading' | 'enhance' > & { as?: React.ElementType; size?: 'small' | 'medium' | 'large' | 'xlarge'; }; export const RunInfo = ({ className = '', title = null, titleHoverCard = null, titleTooltip = null, current = '', baseline = '', delta = '', deltaPercentage = '', deltaType = 'NO_CHANGE', as: Component = 'div', size = 'medium', showBaseline = true, showDelta = true, loading = false, enhance = false, children, ...restProps }: RunInfoProps & Omit, 'title'>) => { const rootClassName = cx(css.root, loading && css.loading, css[size], className); return ( {title && ( )} {children} ); };