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],
|
|
|
|
);
|
|
|
|
|
2023-08-10 18:31:25 +02:00
|
|
|
if (!fields || !report?.parameters) {
|
2023-08-08 08:02:38 +02:00
|
|
|
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-11 18:05:56 +02:00
|
|
|
<GridColumn
|
|
|
|
name="visitors"
|
|
|
|
label={formatMessage(labels.visitors)}
|
|
|
|
width="100px"
|
|
|
|
alignment="end"
|
|
|
|
>
|
2023-08-08 00:05:53 +02:00
|
|
|
{row => row.visitors.toLocaleString()}
|
2023-08-08 00:02:50 +02:00
|
|
|
</GridColumn>
|
2023-08-11 18:05:56 +02:00
|
|
|
<GridColumn name="views" label={formatMessage(labels.views)} width="100px" alignment="end">
|
2023-08-08 08:02:38 +02:00
|
|
|
{row => row.views.toLocaleString()}
|
|
|
|
</GridColumn>
|
2023-07-08 05:38:43 +02:00
|
|
|
</GridTable>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default InsightsTable;
|