2020-10-10 09:07:08 +02:00
|
|
|
import React, { useMemo } from 'react';
|
2020-09-11 07:21:38 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2020-10-22 01:14:51 +02:00
|
|
|
import firstBy from 'thenby';
|
2020-08-01 04:05:14 +02:00
|
|
|
import classNames from 'classnames';
|
2020-09-26 08:38:28 +02:00
|
|
|
import Link from 'components/common/Link';
|
2020-08-31 00:29:31 +02:00
|
|
|
import Loading from 'components/common/Loading';
|
|
|
|
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-09-18 07:52:20 +02:00
|
|
|
import useDateRange from 'hooks/useDateRange';
|
2020-09-26 08:38:28 +02:00
|
|
|
import usePageQuery from 'hooks/usePageQuery';
|
2020-10-11 11:29:55 +02:00
|
|
|
import useShareToken from 'hooks/useShareToken';
|
2020-10-10 09:07:08 +02:00
|
|
|
import ErrorMessage from 'components/common/ErrorMessage';
|
|
|
|
import DataTable from './DataTable';
|
2020-10-11 11:29:55 +02:00
|
|
|
import { DEFAULT_ANIMATION_DURATION, TOKEN_HEADER } from 'lib/constants';
|
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,
|
|
|
|
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-10-11 10:33:26 +02:00
|
|
|
onDataLoad,
|
|
|
|
...props
|
2020-08-01 04:05:14 +02:00
|
|
|
}) {
|
2020-10-11 11:29:55 +02:00
|
|
|
const shareToken = useShareToken();
|
2022-03-02 06:13:15 +01:00
|
|
|
const [{ startDate, endDate, modified }] = useDateRange(websiteId);
|
2020-09-26 07:31:18 +02:00
|
|
|
const {
|
2020-09-26 08:38:28 +02:00
|
|
|
resolve,
|
2020-09-26 09:26:59 +02:00
|
|
|
router,
|
2022-03-20 22:14:54 +01:00
|
|
|
query: { url, referrer },
|
2020-09-26 07:31:18 +02:00
|
|
|
} = usePageQuery();
|
|
|
|
|
2020-10-04 07:36:51 +02:00
|
|
|
const { data, loading, error } = useFetch(
|
2022-02-23 08:52:31 +01:00
|
|
|
`/website/${websiteId}/metrics`,
|
2020-08-31 00:29:31 +02:00
|
|
|
{
|
2020-10-09 21:39:03 +02:00
|
|
|
params: {
|
|
|
|
type,
|
|
|
|
start_at: +startDate,
|
|
|
|
end_at: +endDate,
|
|
|
|
url,
|
2022-03-20 22:14:54 +01:00
|
|
|
referrer,
|
2020-10-09 21:39:03 +02:00
|
|
|
},
|
|
|
|
onDataLoad,
|
2020-10-10 09:07:08 +02:00
|
|
|
delay: DEFAULT_ANIMATION_DURATION,
|
2020-10-11 11:29:55 +02:00
|
|
|
headers: { [TOKEN_HEADER]: shareToken?.token },
|
2020-08-31 00:29:31 +02:00
|
|
|
},
|
2022-03-20 22:14:54 +01:00
|
|
|
[modified, url, referrer],
|
2020-08-31 00:29:31 +02:00
|
|
|
);
|
2020-08-01 04:05:14 +02:00
|
|
|
|
2020-10-10 09:07:08 +02:00
|
|
|
const filteredData = useMemo(() => {
|
2020-08-01 06:56:25 +02:00
|
|
|
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) {
|
2020-10-22 01:14:51 +02:00
|
|
|
return items.filter((e, i) => i < limit).sort(firstBy('y', -1).thenBy('x'));
|
2020-08-10 00:13:38 +02:00
|
|
|
}
|
2020-10-22 01:14:51 +02:00
|
|
|
return items.sort(firstBy('y', -1).thenBy('x'));
|
2020-08-01 06:56:25 +02:00
|
|
|
}
|
|
|
|
return [];
|
2020-10-04 07:36:51 +02:00
|
|
|
}, [data, error, dataFilter, filterOptions]);
|
2020-08-01 04:05:14 +02:00
|
|
|
|
|
|
|
return (
|
2020-08-10 00:13:38 +02:00
|
|
|
<div className={classNames(styles.container, className)}>
|
2020-10-04 07:36:51 +02:00
|
|
|
{!data && loading && <Loading />}
|
|
|
|
{error && <ErrorMessage />}
|
2020-10-13 01:31:51 +02:00
|
|
|
{data && !error && <DataTable {...props} data={filteredData} className={className} />}
|
2020-10-10 09:07:08 +02:00
|
|
|
<div className={styles.footer}>
|
2020-10-13 01:31:51 +02:00
|
|
|
{data && !error && limit && (
|
2020-10-10 09:07:08 +02:00
|
|
|
<Link
|
|
|
|
icon={<Arrow />}
|
|
|
|
href={router.pathname}
|
|
|
|
as={resolve({ view: type })}
|
|
|
|
size="small"
|
|
|
|
iconRight
|
|
|
|
>
|
2020-10-13 07:53:59 +02:00
|
|
|
<FormattedMessage id="label.more" defaultMessage="More" />
|
2020-10-10 09:07:08 +02:00
|
|
|
</Link>
|
|
|
|
)}
|
2020-08-01 05:37:29 +02:00
|
|
|
</div>
|
2020-08-01 04:05:14 +02:00
|
|
|
</div>
|
|
|
|
);
|
2020-10-10 09:07:08 +02:00
|
|
|
}
|