2021-02-15 16:31:53 +01:00
|
|
|
import React, { useState } from 'react';
|
2020-09-06 02:27:01 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2020-08-25 08:49:14 +02:00
|
|
|
import MetricsTable from './MetricsTable';
|
2020-10-09 21:39:03 +02:00
|
|
|
import Tag from 'components/common/Tag';
|
2021-02-15 16:31:53 +01:00
|
|
|
import DropDown from 'components/common/DropDown';
|
|
|
|
import { eventTypeFilter } from 'lib/filters';
|
2022-03-18 04:56:43 +01:00
|
|
|
import usePageQuery from 'hooks/usePageQuery';
|
2021-02-15 16:44:27 +01:00
|
|
|
import styles from './EventsTable.module.css';
|
2021-02-15 16:31:53 +01:00
|
|
|
|
|
|
|
const EVENT_FILTER_DEFAULT = {
|
2022-03-18 04:56:43 +01:00
|
|
|
value: 'all',
|
2021-02-15 18:16:23 +01:00
|
|
|
label: <FormattedMessage id="label.all-events" defaultMessage="All events" />,
|
2021-02-15 16:31:53 +01:00
|
|
|
};
|
2020-08-25 08:49:14 +02:00
|
|
|
|
2020-10-11 11:29:55 +02:00
|
|
|
export default function EventsTable({ websiteId, ...props }) {
|
2021-02-15 16:31:53 +01:00
|
|
|
const [eventType, setEventType] = useState(EVENT_FILTER_DEFAULT.value);
|
2021-02-21 02:27:56 +01:00
|
|
|
const [eventTypes, setEventTypes] = useState([]);
|
2022-03-18 04:56:43 +01:00
|
|
|
const { resolve, router } = usePageQuery();
|
2021-02-15 16:31:53 +01:00
|
|
|
|
|
|
|
const dropDownOptions = [EVENT_FILTER_DEFAULT, ...eventTypes.map(t => ({ value: t, label: t }))];
|
|
|
|
|
2021-02-21 02:27:56 +01:00
|
|
|
function handleDataLoad(data) {
|
|
|
|
setEventTypes([...new Set(data.map(({ x }) => x.split('\t')[0]))]);
|
2021-02-21 10:31:23 +01:00
|
|
|
props.onDataLoad?.(data);
|
2021-02-21 02:27:56 +01:00
|
|
|
}
|
|
|
|
|
2022-03-18 04:56:43 +01:00
|
|
|
function handleChange(value) {
|
|
|
|
router.replace(resolve({ eventType: value === 'all' ? undefined : value }));
|
|
|
|
setEventType(value);
|
|
|
|
}
|
|
|
|
|
2020-08-25 08:49:14 +02:00
|
|
|
return (
|
2021-02-15 16:31:53 +01:00
|
|
|
<>
|
2021-02-21 02:27:56 +01:00
|
|
|
{eventTypes?.length > 1 && (
|
2021-02-15 16:44:27 +01:00
|
|
|
<div className={styles.filter}>
|
2022-03-18 04:56:43 +01:00
|
|
|
<DropDown value={eventType} options={dropDownOptions} onChange={handleChange} />
|
2021-02-15 16:31:53 +01:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<MetricsTable
|
|
|
|
{...props}
|
|
|
|
title={<FormattedMessage id="metrics.events" defaultMessage="Events" />}
|
|
|
|
type="event"
|
|
|
|
metric={<FormattedMessage id="metrics.actions" defaultMessage="Actions" />}
|
|
|
|
websiteId={websiteId}
|
|
|
|
dataFilter={eventTypeFilter}
|
|
|
|
filterOptions={eventType === EVENT_FILTER_DEFAULT.value ? [] : [eventType]}
|
|
|
|
renderLabel={({ x }) => <Label value={x} />}
|
2021-02-21 02:27:56 +01:00
|
|
|
onDataLoad={handleDataLoad}
|
2021-02-15 16:31:53 +01:00
|
|
|
/>
|
|
|
|
</>
|
2020-08-25 08:49:14 +02:00
|
|
|
);
|
|
|
|
}
|
2020-08-28 03:44:20 +02:00
|
|
|
|
|
|
|
const Label = ({ value }) => {
|
2020-12-05 05:45:12 +01:00
|
|
|
const [event, label] = value.split('\t');
|
2020-08-28 03:44:20 +02:00
|
|
|
return (
|
|
|
|
<>
|
2020-10-09 21:39:03 +02:00
|
|
|
<Tag>{event}</Tag>
|
2020-08-28 03:44:20 +02:00
|
|
|
{label}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|