Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

3.0 KB
Raw Blame
import { type BlockProps } from '@lobehub/ui';
import { Block, Flexbox, Spin, Statistic, Text } from '@lobehub/ui';
import { cssVar } from '@lobehub/ui/stylex/cssVar.stylex';
import { media } from '@lobehub/ui/stylex/media.stylex';
import * as stylex from '@stylexjs/stylex';
import { type CSSProperties, type ReactNode } from 'react';
import { memo } from 'react';

const styles = stylex.create({
  header: {
    alignItems: 'center',
    display: 'flex',
    flexWrap: { default: null, [media.sm]: 'wrap' },
    gap: 8,
    justifyContent: 'space-between',
    marginBlockEnd: { default: null, [media.sm]: 8 },
  },
  statistic: {
    fontWeight: 'bold',
    lineHeight: 1.2,
  },
  title: {
    color: cssVar.colorText,
    flex: '1',
    fontSize: { default: 16, [media.sm]: 14 },
    fontWeight: 500,
    lineHeight: { default: '32px', [media.sm]: '16px' },
    overflow: 'hidden',
  },
});

export interface StatisticConfig {
  description?: ReactNode;
  precision?: number;
  prefix?: ReactNode;
  style?: CSSProperties;
  suffix?: ReactNode;
  value?: number | string;
  valueStyle?: CSSProperties;
}

export interface StatisticCardProps extends Pick<
  BlockProps,
  'variant' | 'padding' | 'paddingBlock' | 'paddingInline'
> {
  className?: string;
  extra?: ReactNode;
  loading?: boolean;
  statistic?: StatisticConfig;
  style?: CSSProperties;
  title?: ReactNode;
}

const StatisticCard = memo<StatisticCardProps>(
  ({
    title,
    className,
    variant = 'borderless',
    loading,
    extra,
    style,
    padding,
    paddingBlock,
    paddingInline,
    statistic,
  }) => {
    return (
      <Block
        className={className}
        flex={1}
        padding={padding}
        paddingBlock={paddingBlock}
        paddingInline={paddingInline}
        style={style}
        variant={variant}
      >
        <div {...stylex.props(styles.header)}>
          <div {...stylex.props(styles.title)}>
            {typeof title === 'string' ? (
              <Text
                as={'h2'}
                ellipsis={{ rows: 1, tooltip: true }}
                style={{
                  fontSize: 'inherit',
                  fontWeight: 'inherit',
                  lineHeight: 'inherit',
                  margin: 0,
                  overflow: 'hidden',
                }}
              >
                {title}
              </Text>
            ) : (
              title
            )}
          </div>
          {loading ? <Spin size={'small'} /> : extra}
        </div>
        {statistic && (
          <Flexbox gap={16} style={statistic.style}>
            <Statistic
              classNames={{ value: stylex.props(styles.statistic).className }}
              precision={statistic.precision}
              prefix={statistic.prefix}
              styles={statistic.valueStyle ? { value: statistic.valueStyle } : undefined}
              suffix={statistic.suffix}
              value={statistic.value}
            />
            {statistic.description}
          </Flexbox>
        )}
      </Block>
    );
  },
);

export default StatisticCard;