umami/components/metrics/MetricsBar.js

30 lines
898 B
JavaScript
Raw Normal View History

2023-07-11 10:16:17 +02:00
import { useState } from 'react';
import { Loading, cloneChildren } from 'react-basics';
import ErrorMessage from 'components/common/ErrorMessage';
2023-04-10 05:22:28 +02:00
import styles from './MetricsBar.module.css';
2023-07-11 10:16:17 +02:00
import { formatLongNumber, formatNumber } from 'lib/format';
export function MetricsBar({ children, isLoading, isFetched, error }) {
const [format, setFormat] = useState(true);
const formatFunc = format
? n => (n >= 0 ? formatLongNumber(n) : `-${formatLongNumber(Math.abs(n))}`)
: formatNumber;
const handleSetFormat = () => {
setFormat(state => !state);
};
return (
2023-07-11 10:16:17 +02:00
<div className={styles.bar} onClick={handleSetFormat}>
2023-02-04 17:59:52 +01:00
{isLoading && !isFetched && <Loading icon="dots" />}
{error && <ErrorMessage />}
2023-07-11 10:16:17 +02:00
{cloneChildren(children, child => {
return { format: child.props.format || formatFunc };
})}
</div>
);
}
2023-04-21 17:00:42 +02:00
export default MetricsBar;