2020-08-31 00:29:31 +02:00
|
|
|
import React, { useState, useMemo } from 'react';
|
2020-09-11 07:21:38 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
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';
|
2020-08-31 00:29:31 +02:00
|
|
|
import Loading from 'components/common/Loading';
|
2020-09-11 07:21:38 +02:00
|
|
|
import NoData from 'components/common/NoData';
|
2020-08-31 00:29:31 +02:00
|
|
|
import useFetch from 'hooks/useFetch';
|
2020-08-09 12:04:48 +02:00
|
|
|
import Arrow from 'assets/arrow-right.svg';
|
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-09-01 00:02:32 +02:00
|
|
|
import { useDateRange } from 'hooks/useDateRange';
|
2020-08-23 04:05:07 +02:00
|
|
|
import styles from './MetricsTable.module.css';
|
2020-08-01 04:05:14 +02:00
|
|
|
|
2020-08-23 04:05:07 +02:00
|
|
|
export default function MetricsTable({
|
2020-08-01 04:05:14 +02:00
|
|
|
websiteId,
|
2020-08-29 06:34:20 +02:00
|
|
|
websiteDomain,
|
2020-08-31 23:11:30 +02:00
|
|
|
title,
|
|
|
|
metric,
|
2020-08-01 04:05:14 +02:00
|
|
|
type,
|
|
|
|
className,
|
2020-08-01 05:37:29 +02:00
|
|
|
dataFilter,
|
2020-08-23 04:05:07 +02:00
|
|
|
filterOptions,
|
2020-08-10 00:13:38 +02:00
|
|
|
limit,
|
2020-08-23 04:05:07 +02:00
|
|
|
headerComponent,
|
2020-08-25 08:49:14 +02:00
|
|
|
renderLabel,
|
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
|
|
|
}) {
|
2020-09-01 00:02:32 +02:00
|
|
|
const dateRange = useDateRange(websiteId);
|
|
|
|
const { startDate, endDate, modified } = dateRange;
|
2020-08-31 00:29:31 +02:00
|
|
|
const { data } = useFetch(
|
|
|
|
`/api/website/${websiteId}/rankings`,
|
|
|
|
{
|
|
|
|
type,
|
|
|
|
start_at: +startDate,
|
|
|
|
end_at: +endDate,
|
|
|
|
domain: websiteDomain,
|
|
|
|
},
|
2020-08-31 23:11:30 +02:00
|
|
|
{ onDataLoad, delay: 300, update: [modified] },
|
2020-08-31 00:29:31 +02:00
|
|
|
);
|
2020-08-19 23:35:38 +02:00
|
|
|
const [format, setFormat] = useState(true);
|
|
|
|
const formatFunc = format ? formatLongNumber : formatNumber;
|
2020-08-21 22:43:42 +02:00
|
|
|
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-23 04:05:07 +02:00
|
|
|
const items = percentFilter(dataFilter ? dataFilter(data, filterOptions) : data);
|
2020-08-10 00:13:38 +02:00
|
|
|
if (limit) {
|
|
|
|
return items.filter((e, i) => i < limit);
|
|
|
|
}
|
|
|
|
return items;
|
2020-08-01 06:56:25 +02:00
|
|
|
}
|
|
|
|
return [];
|
2020-08-23 07:01:14 +02:00
|
|
|
}, [data, dataFilter, filterOptions]);
|
2020-08-01 04:05:14 +02:00
|
|
|
|
2020-08-25 08:49:14 +02:00
|
|
|
const handleSetFormat = () => setFormat(state => !state);
|
2020-08-01 04:05:14 +02:00
|
|
|
|
2020-08-25 08:49:14 +02:00
|
|
|
const getRow = row => {
|
|
|
|
const { x: label, y: value, z: percent } = row;
|
2020-08-10 00:13:38 +02:00
|
|
|
return (
|
2020-08-21 22:43:42 +02:00
|
|
|
<AnimatedRow
|
2020-08-25 08:49:14 +02:00
|
|
|
key={label}
|
|
|
|
label={renderLabel ? renderLabel(row) : label}
|
|
|
|
value={value}
|
|
|
|
percent={percent}
|
2020-08-21 22:43:42 +02:00
|
|
|
animate={shouldAnimate}
|
|
|
|
format={formatFunc}
|
|
|
|
onClick={handleSetFormat}
|
|
|
|
/>
|
2020-08-10 00:13:38 +02:00
|
|
|
);
|
2020-08-25 08:49:14 +02:00
|
|
|
};
|
2020-08-21 22:43:42 +02:00
|
|
|
|
|
|
|
const Row = ({ index, style }) => {
|
2020-08-25 08:49:14 +02:00
|
|
|
return <div style={style}>{getRow(rankings[index])}</div>;
|
2020-08-10 00:13:38 +02:00
|
|
|
};
|
|
|
|
|
2020-08-01 04:05:14 +02:00
|
|
|
return (
|
2020-08-10 00:13:38 +02:00
|
|
|
<div className={classNames(styles.container, className)}>
|
2020-09-11 07:21:38 +02:00
|
|
|
{!data && <Loading />}
|
|
|
|
{data && (
|
2020-08-28 08:45:37 +02:00
|
|
|
<>
|
|
|
|
<div className={styles.header}>
|
|
|
|
<div className={styles.title}>{title}</div>
|
|
|
|
{headerComponent}
|
|
|
|
<div className={styles.metric} onClick={handleSetFormat}>
|
|
|
|
{metric}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className={styles.body}>
|
2020-09-11 07:21:38 +02:00
|
|
|
{rankings?.length === 0 && <NoData />}
|
2020-08-28 08:45:37 +02:00
|
|
|
{limit
|
|
|
|
? rankings.map(row => getRow(row))
|
2020-09-11 06:05:02 +02:00
|
|
|
: rankings.length > 0 && (
|
2020-08-28 08:45:37 +02:00
|
|
|
<FixedSizeList height={500} itemCount={rankings.length} itemSize={30}>
|
|
|
|
{Row}
|
|
|
|
</FixedSizeList>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div className={styles.footer}>
|
2020-09-11 07:21:38 +02:00
|
|
|
{limit && data.length > limit && (
|
2020-08-28 08:45:37 +02:00
|
|
|
<Button icon={<Arrow />} size="xsmall" onClick={() => onExpand(type)}>
|
2020-09-06 02:27:01 +02:00
|
|
|
<div>
|
|
|
|
<FormattedMessage id="button.more" defaultMessage="More" />
|
|
|
|
</div>
|
2020-08-28 08:45:37 +02:00
|
|
|
</Button>
|
2020-08-25 08:49:14 +02:00
|
|
|
)}
|
2020-08-28 08:45:37 +02:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
2020-08-10 00:13:38 +02:00
|
|
|
</div>
|
2020-08-01 04:05:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-25 08:49: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 (
|
2020-08-23 04:05:07 +02:00
|
|
|
<div className={styles.row}>
|
2020-08-25 08:49:14 +02:00
|
|
|
<div className={styles.label}>{label}</div>
|
2020-08-23 04:05:07 +02:00
|
|
|
<div className={styles.value} onClick={onClick}>
|
|
|
|
<animated.div className={styles.value}>{props.y?.interpolate(format)}</animated.div>
|
|
|
|
</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>
|
|
|
|
);
|
|
|
|
};
|