umami/src/app/(main)/websites/[websiteId]/events/EventProperties.tsx

66 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-08-09 08:09:54 +00:00
import { GridColumn, GridTable } from 'react-basics';
import { useEventDataProperties, useEventDataValues, useMessages } from 'components/hooks';
import { LoadingPanel } from 'components/common/LoadingPanel';
import PieChart from 'components/charts/PieChart';
import { useState } from 'react';
import { CHART_COLORS } from 'lib/constants';
2024-08-09 08:41:21 +00:00
import styles from './EventProperties.module.css';
2024-08-09 08:09:54 +00:00
export function EventProperties({ websiteId }: { websiteId: string }) {
const [propertyName, setPropertyName] = useState('');
2024-08-14 19:29:47 +00:00
const [eventName, setEventName] = useState('');
2024-08-09 08:09:54 +00:00
const { formatMessage, labels } = useMessages();
const { data, isLoading, isFetched, error } = useEventDataProperties(websiteId);
2024-08-14 19:29:47 +00:00
const { data: values } = useEventDataValues(websiteId, eventName, propertyName);
2024-08-09 08:09:54 +00:00
const chartData =
propertyName && values
? {
labels: values.map(({ value }) => value),
datasets: [
{
data: values.map(({ total }) => total),
backgroundColor: CHART_COLORS,
borderWidth: 0,
},
],
}
: null;
2024-08-14 19:29:47 +00:00
const handleRowClick = row => {
setEventName(row.eventName);
setPropertyName(row.propertyName);
};
2024-08-09 08:09:54 +00:00
return (
<LoadingPanel isLoading={isLoading} isFetched={isFetched} data={data} error={error}>
<div className={styles.container}>
<GridTable data={data} cardMode={false} className={styles.table}>
2024-08-14 19:29:47 +00:00
<GridColumn name="eventName" label={formatMessage(labels.name)}>
{row => (
<div className={styles.link} onClick={() => handleRowClick(row)}>
{row.eventName}
</div>
)}
</GridColumn>
2024-08-09 08:09:54 +00:00
<GridColumn name="propertyName" label={formatMessage(labels.property)}>
{row => (
2024-08-14 19:29:47 +00:00
<div className={styles.link} onClick={() => handleRowClick(row)}>
2024-08-09 08:09:54 +00:00
{row.propertyName}
</div>
)}
</GridColumn>
2024-08-09 08:41:21 +00:00
<GridColumn name="total" label={formatMessage(labels.count)} alignment="end" />
2024-08-09 08:09:54 +00:00
</GridTable>
{propertyName && (
2024-08-09 08:41:21 +00:00
<div className={styles.chart}>
<div className={styles.title}>{propertyName}</div>
2024-08-09 08:09:54 +00:00
<PieChart key={propertyName} type="doughnut" data={chartData} />
</div>
)}
</div>
</LoadingPanel>
);
}
export default EventProperties;