umami/components/metrics/RankingsChart.js

139 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-08-01 04:05:14 +02:00
import React, { useState, useEffect, useMemo } from 'react';
2020-08-10 00:13:38 +02:00
import { FixedSizeList } from 'react-window';
2020-08-04 03:12:28 +02:00
import { useSpring, animated, config } from 'react-spring';
2020-08-01 04:05:14 +02:00
import classNames from 'classnames';
2020-08-09 12:04:48 +02:00
import Button from 'components/common/Button';
import Arrow from 'assets/arrow-right.svg';
2020-08-01 04:05:14 +02:00
import { get } from 'lib/web';
2020-08-01 12:34:56 +02:00
import { percentFilter } from 'lib/filters';
2020-08-19 23:35:38 +02:00
import { formatNumber, formatLongNumber } from 'lib/format';
2020-08-01 04:05:14 +02:00
import styles from './RankingsChart.module.css';
export default function RankingsChart({
title,
websiteId,
startDate,
endDate,
type,
2020-08-01 05:37:29 +02:00
heading,
2020-08-01 04:05:14 +02:00
className,
2020-08-01 05:37:29 +02:00
dataFilter,
2020-08-10 00:13:38 +02:00
limit,
2020-08-01 12:34:56 +02:00
onDataLoad = () => {},
2020-08-09 12:04:48 +02:00
onExpand = () => {},
2020-08-01 04:05:14 +02:00
}) {
const [data, setData] = useState();
2020-08-19 23:35:38 +02:00
const [format, setFormat] = useState(true);
const formatFunc = format ? formatLongNumber : formatNumber;
const shouldAnimate = limit > 0;
2020-08-01 04:05:14 +02:00
2020-08-01 06:56:25 +02:00
const rankings = useMemo(() => {
if (data) {
2020-08-10 00:13:38 +02:00
const items = dataFilter ? dataFilter(data) : data;
if (limit) {
return items.filter((e, i) => i < limit);
}
return items;
2020-08-01 06:56:25 +02:00
}
return [];
}, [data]);
2020-08-01 04:05:14 +02:00
async function loadData() {
2020-08-01 12:34:56 +02:00
const data = await get(`/api/website/${websiteId}/rankings`, {
start_at: +startDate,
end_at: +endDate,
type,
});
const updated = percentFilter(data);
setData(updated);
onDataLoad(updated);
2020-08-01 04:05:14 +02:00
}
2020-08-19 23:35:38 +02:00
function handleSetFormat() {
setFormat(state => !state);
2020-08-01 04:05:14 +02:00
}
function getRow(x, y, z) {
2020-08-10 00:13:38 +02:00
return (
<AnimatedRow
key={x}
label={x}
value={y}
percent={z}
animate={shouldAnimate}
format={formatFunc}
onClick={handleSetFormat}
/>
2020-08-10 00:13:38 +02:00
);
}
const Row = ({ index, style }) => {
const { x, y, z } = rankings[index];
return <div style={style}>{getRow(x, y, z)}</div>;
2020-08-10 00:13:38 +02:00
};
2020-08-19 23:35:38 +02:00
useEffect(() => {
if (websiteId) {
loadData();
}
}, [websiteId, startDate, endDate, type]);
if (!data) {
return null;
}
2020-08-01 04:05:14 +02:00
return (
2020-08-10 00:13:38 +02:00
<div className={classNames(styles.container, className)}>
<div className={styles.header}>
2020-08-10 00:13:38 +02:00
<div className={styles.title}>{title}</div>
<div className={styles.heading} onClick={handleSetFormat}>
{heading}
</div>
2020-08-10 00:13:38 +02:00
</div>
<div className={styles.body}>
{limit ? (
rankings.map(({ x, y, z }) => getRow(x, y, z))
2020-08-10 00:13:38 +02:00
) : (
<FixedSizeList height={600} itemCount={rankings.length} itemSize={30}>
{Row}
</FixedSizeList>
)}
</div>
<div className={styles.footer}>
{limit && data.length > limit && (
<Button icon={<Arrow />} size="xsmall" onClick={() => onExpand(type)}>
<div>More</div>
</Button>
)}
</div>
</div>
2020-08-01 04:05:14 +02:00
);
}
const AnimatedRow = ({ label, value = 0, percent, animate, format, onClick }) => {
2020-08-04 03:12:28 +02:00
const props = useSpring({
width: percent,
y: value,
from: { width: 0, y: 0 },
config: animate ? config.default : { duration: 0 },
});
2020-08-01 04:05:14 +02:00
return (
<div className={styles.row} onClick={onClick}>
2020-08-01 04:05:14 +02:00
<div className={styles.label}>{label}</div>
2020-08-20 08:30:31 +02:00
<animated.div className={styles.value}>{props.y?.interpolate(format)}</animated.div>
2020-08-01 05:37:29 +02:00
<div className={styles.percent}>
2020-08-04 03:12:28 +02:00
<animated.div
className={styles.bar}
style={{ width: props.width.interpolate(n => `${n}%`) }}
/>
<animated.span className={styles.percentValue}>
{props.width.interpolate(n => `${n.toFixed(0)}%`)}
</animated.span>
2020-08-01 05:37:29 +02:00
</div>
2020-08-01 04:05:14 +02:00
</div>
);
};