2023-08-08 08:02:38 +02:00
|
|
|
import { useContext, useEffect, useState } from 'react';
|
2023-07-08 05:38:43 +02:00
|
|
|
import { GridTable, GridColumn } from 'react-basics';
|
2023-08-08 00:02:50 +02:00
|
|
|
import { useFormat, useMessages } from 'hooks';
|
2023-07-08 05:38:43 +02:00
|
|
|
import { ReportContext } from '../Report';
|
2023-08-08 08:02:38 +02:00
|
|
|
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
|
2023-07-08 05:38:43 +02:00
|
|
|
|
|
|
|
export function InsightsTable() {
|
2023-08-08 08:02:38 +02:00
|
|
|
const [fields, setFields] = useState();
|
2023-07-08 05:38:43 +02:00
|
|
|
const { report } = useContext(ReportContext);
|
|
|
|
const { formatMessage, labels } = useMessages();
|
2023-08-08 00:02:50 +02:00
|
|
|
const { formatValue } = useFormat();
|
2023-07-08 05:38:43 +02:00
|
|
|
|
2023-08-08 08:02:38 +02:00
|
|
|
useEffect(
|
|
|
|
() => {
|
|
|
|
setFields(report?.parameters?.fields);
|
|
|
|
},
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
[report?.data],
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!fields) {
|
|
|
|
return <EmptyPlaceholder />;
|
|
|
|
}
|
|
|
|
|
2023-07-08 05:38:43 +02:00
|
|
|
return (
|
|
|
|
<GridTable data={report?.data || []}>
|
2023-08-08 08:02:38 +02:00
|
|
|
{fields.map(({ name, label }) => {
|
2023-08-08 00:02:50 +02:00
|
|
|
return (
|
|
|
|
<GridColumn key={name} name={name} label={label}>
|
|
|
|
{row => formatValue(row[name], name)}
|
|
|
|
</GridColumn>
|
|
|
|
);
|
2023-08-03 19:44:35 +02:00
|
|
|
})}
|
2023-08-08 00:02:50 +02:00
|
|
|
<GridColumn name="visitors" label={formatMessage(labels.visitors)} width="100px">
|
2023-08-08 00:05:53 +02:00
|
|
|
{row => row.visitors.toLocaleString()}
|
2023-08-08 00:02:50 +02:00
|
|
|
</GridColumn>
|
2023-08-08 08:02:38 +02:00
|
|
|
<GridColumn name="views" label={formatMessage(labels.views)} width="100px">
|
|
|
|
{row => row.views.toLocaleString()}
|
|
|
|
</GridColumn>
|
2023-07-08 05:38:43 +02:00
|
|
|
</GridTable>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default InsightsTable;
|