umami/src/components/metrics/MetricsTable.js

121 lines
3.2 KiB
JavaScript
Raw Normal View History

import { useMemo } from 'react';
2023-10-15 20:12:29 +00:00
import { Loading, Icon, Text } from 'react-basics';
2020-10-21 23:14:51 +00:00
import firstBy from 'thenby';
2020-08-01 02:05:14 +00:00
import classNames from 'classnames';
import useApi from 'components/hooks/useApi';
2020-08-01 10:34:56 +00:00
import { percentFilter } from 'lib/filters';
import useDateRange from 'components/hooks/useDateRange';
2023-10-03 16:45:02 +00:00
import useNavigation from 'components/hooks/useNavigation';
import ErrorMessage from 'components/common/ErrorMessage';
2023-10-15 20:12:29 +00:00
import LinkButton from 'components/common/LinkButton';
2023-08-26 19:46:27 +00:00
import ListTable from './ListTable';
import { DEFAULT_ANIMATION_DURATION } from 'lib/constants';
2023-02-09 07:14:11 +00:00
import Icons from 'components/icons';
import useMessages from 'components/hooks/useMessages';
import useLocale from 'components/hooks/useLocale';
2023-10-15 20:12:29 +00:00
import styles from './MetricsTable.module.css';
2020-08-01 02:05:14 +00:00
2023-04-21 15:00:42 +00:00
export function MetricsTable({
2020-08-01 02:05:14 +00:00
websiteId,
type,
className,
2020-08-01 03:37:29 +00:00
dataFilter,
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 {
2023-10-03 16:45:02 +00:00
makeUrl,
2023-04-26 06:25:51 +00:00
query: { url, referrer, title, os, browser, device, country, region, city },
2023-10-03 16:45:02 +00:00
} = useNavigation();
2023-03-22 21:05:55 +00:00
const { formatMessage, labels } = useMessages();
const { get, useQuery } = useApi();
const { dir } = useLocale();
2020-09-26 05:31:18 +00:00
2023-12-02 04:27:59 +00:00
const { data, isLoading, isFetched, error } = useQuery({
queryKey: [
2023-04-07 03:34:02 +00:00
'websites:metrics',
2023-04-26 06:25:51 +00:00
{
websiteId,
type,
modified,
url,
referrer,
os,
title,
browser,
device,
country,
region,
city,
},
2023-04-07 03:34:02 +00:00
],
2023-12-02 04:27:59 +00:00
queryFn: () => {
2023-08-29 03:09:54 +00:00
const filters = { url, title, referrer, os, browser, device, country, region, city };
filters[type] = undefined;
2023-12-02 04:27:59 +00:00
onDataLoad?.();
2023-08-29 03:09:54 +00:00
return get(`/websites/${websiteId}/metrics`, {
2020-10-09 19:39:03 +00:00
type,
2022-12-27 01:36:48 +00:00
startAt: +startDate,
endAt: +endDate,
2023-08-29 03:09:54 +00:00
...filters,
});
},
2023-12-02 04:27:59 +00:00
retryDelay: delay || DEFAULT_ANIMATION_DURATION,
});
2020-08-01 02:05:14 +00:00
const filteredData = useMemo(() => {
2020-08-01 04:56:25 +00:00
if (data) {
2023-05-17 02:31:40 +00:00
let items = data;
2023-05-17 02:17:12 +00:00
if (dataFilter) {
if (Array.isArray(dataFilter)) {
items = dataFilter.reduce((arr, filter) => {
return filter(arr);
2023-05-17 02:31:40 +00:00
}, items);
2023-05-17 02:17:12 +00:00
} else {
items = dataFilter(data);
}
}
items = percentFilter(items);
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
}
2023-05-17 02:17:12 +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 [];
}, [data, error, dataFilter, filterOptions, limit]);
2020-08-01 02:05:14 +00:00
return (
2020-08-09 22:13:38 +00:00
<div className={classNames(styles.container, className)}>
2023-02-04 16:59:52 +00:00
{!data && isLoading && !isFetched && <Loading icon="dots" />}
{error && <ErrorMessage />}
2023-08-26 19:46:27 +00:00
{data && !error && <ListTable {...props} data={filteredData} className={className} />}
<div className={styles.footer}>
2020-10-12 23:31:51 +00:00
{data && !error && limit && (
2023-10-15 20:12:29 +00:00
<LinkButton href={makeUrl({ view: type })} variant="quiet">
<Text>{formatMessage(labels.more)}</Text>
<Icon size="sm" rotate={dir === 'rtl' ? 180 : 0}>
<Icons.ArrowRight />
</Icon>
</LinkButton>
)}
2020-08-01 03:37:29 +00:00
</div>
2020-08-01 02:05:14 +00:00
</div>
);
}
2023-04-21 15:00:42 +00:00
export default MetricsTable;