2020-10-10 07:07:08 +00:00
|
|
|
import React, { useMemo } from 'react';
|
2022-09-05 19:04:30 +00:00
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
2020-10-21 23:14:51 +00:00
|
|
|
import firstBy from 'thenby';
|
2020-08-01 02:05:14 +00:00
|
|
|
import classNames from 'classnames';
|
2020-09-26 06:38:28 +00:00
|
|
|
import Link from 'components/common/Link';
|
2020-08-30 22:29:31 +00:00
|
|
|
import Loading from 'components/common/Loading';
|
|
|
|
|
import useFetch from 'hooks/useFetch';
|
2020-08-09 10:04:48 +00:00
|
|
|
import Arrow from 'assets/arrow-right.svg';
|
2020-08-01 10:34:56 +00:00
|
|
|
import { percentFilter } from 'lib/filters';
|
2020-09-18 05:52:20 +00:00
|
|
|
import useDateRange from 'hooks/useDateRange';
|
2020-09-26 06:38:28 +00:00
|
|
|
import usePageQuery from 'hooks/usePageQuery';
|
2020-10-10 07:07:08 +00:00
|
|
|
import ErrorMessage from 'components/common/ErrorMessage';
|
|
|
|
|
import DataTable from './DataTable';
|
2022-05-06 02:04:28 +00:00
|
|
|
import { DEFAULT_ANIMATION_DURATION } from 'lib/constants';
|
2020-08-23 02:05:07 +00:00
|
|
|
import styles from './MetricsTable.module.css';
|
2020-08-01 02:05:14 +00:00
|
|
|
|
2022-09-05 19:04:30 +00:00
|
|
|
const messages = defineMessages({
|
|
|
|
|
more: { id: 'label.more', defaultMessage: 'More' },
|
|
|
|
|
});
|
|
|
|
|
|
2020-08-23 02:05:07 +00:00
|
|
|
export default function MetricsTable({
|
2020-08-01 02:05:14 +00:00
|
|
|
websiteId,
|
|
|
|
|
type,
|
|
|
|
|
className,
|
2020-08-01 03:37:29 +00:00
|
|
|
dataFilter,
|
2020-08-23 02:05:07 +00:00
|
|
|
filterOptions,
|
2020-08-09 22:13:38 +00:00
|
|
|
limit,
|
2020-10-11 08:33:26 +00:00
|
|
|
onDataLoad,
|
2022-07-21 23:22:43 +00:00
|
|
|
delay = null,
|
2020-10-11 08:33:26 +00:00
|
|
|
...props
|
2020-08-01 02:05:14 +00:00
|
|
|
}) {
|
2022-03-02 05:13:15 +00:00
|
|
|
const [{ startDate, endDate, modified }] = useDateRange(websiteId);
|
2020-09-26 05:31:18 +00:00
|
|
|
const {
|
2020-09-26 06:38:28 +00:00
|
|
|
resolve,
|
2020-09-26 07:26:59 +00:00
|
|
|
router,
|
2022-04-10 10:51:43 +00:00
|
|
|
query: { url, referrer, os, browser, device, country },
|
2020-09-26 05:31:18 +00:00
|
|
|
} = usePageQuery();
|
2022-09-05 19:04:30 +00:00
|
|
|
const { formatMessage } = useIntl();
|
2020-09-26 05:31:18 +00:00
|
|
|
|
2020-10-04 05:36:51 +00:00
|
|
|
const { data, loading, error } = useFetch(
|
2022-10-04 00:17:53 +00:00
|
|
|
`/websites/${websiteId}/metrics`,
|
2020-08-30 22:29:31 +00:00
|
|
|
{
|
2020-10-09 19:39:03 +00:00
|
|
|
params: {
|
|
|
|
|
type,
|
|
|
|
|
start_at: +startDate,
|
|
|
|
|
end_at: +endDate,
|
|
|
|
|
url,
|
2022-03-20 21:14:54 +00:00
|
|
|
referrer,
|
2022-04-10 10:51:43 +00:00
|
|
|
os,
|
|
|
|
|
browser,
|
|
|
|
|
device,
|
|
|
|
|
country,
|
2020-10-09 19:39:03 +00:00
|
|
|
},
|
|
|
|
|
onDataLoad,
|
2022-07-21 23:22:43 +00:00
|
|
|
delay: delay || DEFAULT_ANIMATION_DURATION,
|
2020-08-30 22:29:31 +00:00
|
|
|
},
|
2022-07-21 23:22:43 +00:00
|
|
|
[type, modified, url, referrer, os, browser, device, country],
|
2020-08-30 22:29:31 +00:00
|
|
|
);
|
2020-08-01 02:05:14 +00:00
|
|
|
|
2020-10-10 07:07:08 +00:00
|
|
|
const filteredData = useMemo(() => {
|
2020-08-01 04:56:25 +00:00
|
|
|
if (data) {
|
2022-08-08 08:26:20 +00:00
|
|
|
let items = percentFilter(dataFilter ? dataFilter(data, filterOptions) : data);
|
2020-08-09 22:13:38 +00:00
|
|
|
if (limit) {
|
2022-08-08 08:26:20 +00:00
|
|
|
items = items.filter((e, i) => i < limit);
|
|
|
|
|
}
|
|
|
|
|
if (filterOptions?.sort === false) {
|
|
|
|
|
return items;
|
2020-08-09 22:13:38 +00:00
|
|
|
}
|
2020-10-21 23:14:51 +00:00
|
|
|
return items.sort(firstBy('y', -1).thenBy('x'));
|
2020-08-01 04:56:25 +00:00
|
|
|
}
|
|
|
|
|
return [];
|
2020-10-04 05:36:51 +00:00
|
|
|
}, [data, error, dataFilter, filterOptions]);
|
2020-08-01 02:05:14 +00:00
|
|
|
|
|
|
|
|
return (
|
2020-08-09 22:13:38 +00:00
|
|
|
<div className={classNames(styles.container, className)}>
|
2020-10-04 05:36:51 +00:00
|
|
|
{!data && loading && <Loading />}
|
|
|
|
|
{error && <ErrorMessage />}
|
2020-10-12 23:31:51 +00:00
|
|
|
{data && !error && <DataTable {...props} data={filteredData} className={className} />}
|
2020-10-10 07:07:08 +00:00
|
|
|
<div className={styles.footer}>
|
2020-10-12 23:31:51 +00:00
|
|
|
{data && !error && limit && (
|
2020-10-10 07:07:08 +00:00
|
|
|
<Link
|
|
|
|
|
icon={<Arrow />}
|
|
|
|
|
href={router.pathname}
|
|
|
|
|
as={resolve({ view: type })}
|
|
|
|
|
size="small"
|
|
|
|
|
iconRight
|
|
|
|
|
>
|
2022-09-05 19:04:30 +00:00
|
|
|
{formatMessage(messages.more)}
|
2020-10-10 07:07:08 +00:00
|
|
|
</Link>
|
|
|
|
|
)}
|
2020-08-01 03:37:29 +00:00
|
|
|
</div>
|
2020-08-01 02:05:14 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
2020-10-10 07:07:08 +00:00
|
|
|
}
|