2022-12-29 00:43:22 +01:00
|
|
|
import { useState } from 'react';
|
2023-04-01 06:31:58 +02:00
|
|
|
import useMeasure from 'react-use-measure';
|
2020-10-10 09:07:08 +02:00
|
|
|
import { FixedSizeList } from 'react-window';
|
|
|
|
import { useSpring, animated, config } from 'react-spring';
|
|
|
|
import classNames from 'classnames';
|
2023-07-02 07:02:49 +02:00
|
|
|
import Empty from 'components/common/Empty';
|
2020-10-10 09:07:08 +02:00
|
|
|
import { formatNumber, formatLongNumber } from 'lib/format';
|
2023-05-25 06:40:02 +02:00
|
|
|
import useMessages from 'hooks/useMessages';
|
2020-10-10 09:07:08 +02:00
|
|
|
import styles from './DataTable.module.css';
|
|
|
|
|
2023-04-21 17:00:42 +02:00
|
|
|
export function DataTable({
|
2023-02-15 11:27:18 +01:00
|
|
|
data = [],
|
2020-10-10 09:07:08 +02:00
|
|
|
title,
|
|
|
|
metric,
|
|
|
|
className,
|
|
|
|
renderLabel,
|
|
|
|
animate = true,
|
2020-10-11 10:33:26 +02:00
|
|
|
virtualize = false,
|
2022-10-22 06:33:23 +02:00
|
|
|
showPercentage = true,
|
2020-10-10 09:07:08 +02:00
|
|
|
}) {
|
2023-04-08 07:03:52 +02:00
|
|
|
const { formatMessage, labels } = useMessages();
|
2023-04-01 06:31:58 +02:00
|
|
|
const [ref, bounds] = useMeasure();
|
2020-10-10 09:07:08 +02:00
|
|
|
const [format, setFormat] = useState(true);
|
|
|
|
const formatFunc = format ? formatLongNumber : formatNumber;
|
|
|
|
|
|
|
|
const handleSetFormat = () => setFormat(state => !state);
|
|
|
|
|
|
|
|
const getRow = row => {
|
|
|
|
const { x: label, y: value, z: percent } = row;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AnimatedRow
|
|
|
|
key={label}
|
2023-04-08 07:03:52 +02:00
|
|
|
label={renderLabel ? renderLabel(row) : label ?? formatMessage(labels.unknown)}
|
2020-10-10 09:07:08 +02:00
|
|
|
value={value}
|
|
|
|
percent={percent}
|
2020-10-11 10:33:26 +02:00
|
|
|
animate={animate && !virtualize}
|
2020-10-10 09:07:08 +02:00
|
|
|
format={formatFunc}
|
|
|
|
onClick={handleSetFormat}
|
2022-10-22 06:33:23 +02:00
|
|
|
showPercentage={showPercentage}
|
2020-10-10 09:07:08 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const Row = ({ index, style }) => {
|
|
|
|
return <div style={style}>{getRow(data[index])}</div>;
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classNames(styles.table, className)}>
|
|
|
|
<div className={styles.header}>
|
|
|
|
<div className={styles.title}>{title}</div>
|
|
|
|
<div className={styles.metric} onClick={handleSetFormat}>
|
|
|
|
{metric}
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-04-01 06:31:58 +02:00
|
|
|
<div ref={ref} className={styles.body}>
|
2023-07-02 07:02:49 +02:00
|
|
|
{data?.length === 0 && <Empty />}
|
2020-10-11 10:33:26 +02:00
|
|
|
{virtualize && data.length > 0 ? (
|
2023-04-01 06:31:58 +02:00
|
|
|
<FixedSizeList height={bounds.height} itemCount={data.length} itemSize={30}>
|
2020-10-11 10:33:26 +02:00
|
|
|
{Row}
|
|
|
|
</FixedSizeList>
|
|
|
|
) : (
|
|
|
|
data.map(row => getRow(row))
|
|
|
|
)}
|
2020-10-10 09:07:08 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-22 06:33:23 +02:00
|
|
|
const AnimatedRow = ({
|
|
|
|
label,
|
|
|
|
value = 0,
|
|
|
|
percent,
|
|
|
|
animate,
|
|
|
|
format,
|
|
|
|
onClick,
|
|
|
|
showPercentage = true,
|
|
|
|
}) => {
|
2020-10-10 09:07:08 +02:00
|
|
|
const props = useSpring({
|
|
|
|
width: percent,
|
|
|
|
y: value,
|
|
|
|
from: { width: 0, y: 0 },
|
|
|
|
config: animate ? config.default : { duration: 0 },
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.row}>
|
|
|
|
<div className={styles.label}>{label}</div>
|
|
|
|
<div className={styles.value} onClick={onClick}>
|
2023-03-10 00:18:54 +01:00
|
|
|
<animated.div className={styles.value}>{props.y?.to(format)}</animated.div>
|
2020-10-10 09:07:08 +02:00
|
|
|
</div>
|
2022-10-22 06:33:23 +02:00
|
|
|
{showPercentage && (
|
|
|
|
<div className={styles.percent}>
|
2023-03-10 00:18:54 +01:00
|
|
|
<animated.div className={styles.bar} style={{ width: props.width.to(n => `${n}%`) }} />
|
2022-10-22 06:33:23 +02:00
|
|
|
<animated.span className={styles.percentValue}>
|
2023-03-10 00:18:54 +01:00
|
|
|
{props.width.to(n => `${n.toFixed(0)}%`)}
|
2022-10-22 06:33:23 +02:00
|
|
|
</animated.span>
|
|
|
|
</div>
|
|
|
|
)}
|
2020-10-10 09:07:08 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2023-04-21 17:00:42 +02:00
|
|
|
|
|
|
|
export default DataTable;
|