umami/components/metrics/MetricsTable.js

95 lines
2.5 KiB
JavaScript
Raw Normal View History

import React, { useMemo } from 'react';
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';
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';
import useDateRange from 'hooks/useDateRange';
2020-09-26 08:38:28 +02:00
import usePageQuery from 'hooks/usePageQuery';
import ErrorMessage from 'components/common/ErrorMessage';
import DataTable from './DataTable';
import { DEFAULT_ANIMATION_DURATION } from 'lib/constants';
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' },
});
export default function MetricsTable({
2020-08-01 04:05:14 +02:00
websiteId,
type,
className,
2020-08-01 05:37:29 +02:00
dataFilter,
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();
2020-09-26 07:31:18 +02:00
const { data, loading, error } = useFetch(
`/websites/${websiteId}/metrics`,
{
2020-10-09 21:39:03 +02:00
params: {
type,
start_at: +startDate,
end_at: +endDate,
url,
referrer,
2022-04-10 12:51:43 +02:00
os,
browser,
device,
country,
2020-10-09 21:39:03 +02:00
},
onDataLoad,
2022-07-22 01:22:43 +02:00
delay: delay || DEFAULT_ANIMATION_DURATION,
},
2022-07-22 01:22:43 +02:00
[type, modified, url, referrer, os, browser, device, country],
);
2020-08-01 04:05:14 +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 [];
}, [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)}>
{!data && loading && <Loading />}
{error && <ErrorMessage />}
2020-10-13 01:31:51 +02:00
{data && !error && <DataTable {...props} data={filteredData} className={className} />}
<div className={styles.footer}>
2020-10-13 01:31:51 +02:00
{data && !error && limit && (
<Link
icon={<Arrow />}
href={router.pathname}
as={resolve({ view: type })}
size="small"
iconRight
>
2022-09-05 21:04:30 +02:00
{formatMessage(messages.more)}
</Link>
)}
2020-08-01 05:37:29 +02:00
</div>
2020-08-01 04:05:14 +02:00
</div>
);
}