2022-12-29 00:43:22 +01:00
|
|
|
import { useMemo } from 'react';
|
2023-01-31 06:44:07 +01:00
|
|
|
import { Loading, Icons } from 'react-basics';
|
2022-09-05 21:04:30 +02:00
|
|
|
import { defineMessages, useIntl } 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';
|
2022-12-29 00:43:22 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
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-10 09:07:08 +02:00
|
|
|
import ErrorMessage from 'components/common/ErrorMessage';
|
|
|
|
import DataTable from './DataTable';
|
2022-05-06 04:04:28 +02:00
|
|
|
import { DEFAULT_ANIMATION_DURATION } 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
|
|
|
|
2022-09-05 21:04:30 +02:00
|
|
|
const messages = defineMessages({
|
|
|
|
more: { id: 'label.more', defaultMessage: 'More' },
|
|
|
|
});
|
|
|
|
|
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,
|
2022-07-22 01:22:43 +02:00
|
|
|
delay = null,
|
2020-10-11 10:33:26 +02:00
|
|
|
...props
|
2020-08-01 04:05:14 +02:00
|
|
|
}) {
|
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-04-10 12:51:43 +02:00
|
|
|
query: { url, referrer, os, browser, device, country },
|
2020-09-26 07:31:18 +02:00
|
|
|
} = usePageQuery();
|
2022-09-05 21:04:30 +02:00
|
|
|
const { formatMessage } = useIntl();
|
2022-12-29 00:43:22 +01:00
|
|
|
const { get, useQuery } = useApi();
|
2020-09-26 07:31:18 +02:00
|
|
|
|
2023-02-04 17:59:52 +01:00
|
|
|
const { data, isLoading, isFetched, error } = useQuery(
|
2022-12-29 00:43:22 +01:00
|
|
|
[
|
|
|
|
'websites:mnetrics',
|
|
|
|
{ websiteId, type, modified, url, referrer, os, browser, device, country },
|
|
|
|
],
|
|
|
|
() =>
|
|
|
|
get(`/websites/${websiteId}/metrics`, {
|
2020-10-09 21:39:03 +02:00
|
|
|
type,
|
2022-12-27 02:36:48 +01:00
|
|
|
startAt: +startDate,
|
|
|
|
endAt: +endDate,
|
2020-10-09 21:39:03 +02:00
|
|
|
url,
|
2022-03-20 22:14:54 +01:00
|
|
|
referrer,
|
2022-04-10 12:51:43 +02:00
|
|
|
os,
|
|
|
|
browser,
|
|
|
|
device,
|
|
|
|
country,
|
2022-12-29 00:43:22 +01:00
|
|
|
}),
|
|
|
|
{ onSuccess: onDataLoad, retryDelay: delay || DEFAULT_ANIMATION_DURATION },
|
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) {
|
2022-08-08 10:26:20 +02:00
|
|
|
let items = percentFilter(dataFilter ? dataFilter(data, filterOptions) : data);
|
2020-08-10 00:13:38 +02:00
|
|
|
if (limit) {
|
2022-08-08 10:26:20 +02:00
|
|
|
items = items.filter((e, i) => i < limit);
|
|
|
|
}
|
|
|
|
if (filterOptions?.sort === false) {
|
|
|
|
return items;
|
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)}>
|
2023-02-04 17:59:52 +01:00
|
|
|
{!data && isLoading && !isFetched && <Loading icon="dots" />}
|
2020-10-04 07:36:51 +02:00
|
|
|
{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
|
2023-01-31 06:44:07 +01:00
|
|
|
icon={<Icons.ArrowRight />}
|
2020-10-10 09:07:08 +02:00
|
|
|
href={router.pathname}
|
|
|
|
as={resolve({ view: type })}
|
|
|
|
size="small"
|
|
|
|
iconRight
|
|
|
|
>
|
2022-09-05 21:04:30 +02:00
|
|
|
{formatMessage(messages.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
|
|
|
}
|