2022-12-10 23:26:52 +01:00
|
|
|
import { useMemo } from 'react';
|
2023-03-03 21:37:26 +01:00
|
|
|
import { Button, Icon, Text, Row, Column } from 'react-basics';
|
2023-02-02 03:39:54 +01:00
|
|
|
import Link from 'next/link';
|
2023-03-22 09:53:34 +01:00
|
|
|
import classNames from 'classnames';
|
2020-07-28 10:17:45 +02:00
|
|
|
import PageviewsChart from './PageviewsChart';
|
2020-07-31 05:11:43 +02:00
|
|
|
import MetricsBar from './MetricsBar';
|
2020-09-18 07:52:20 +02:00
|
|
|
import WebsiteHeader from './WebsiteHeader';
|
2023-02-15 11:27:18 +01:00
|
|
|
import DateFilter from 'components/input/DateFilter';
|
2022-01-14 09:04:07 +01:00
|
|
|
import ErrorMessage from 'components/common/ErrorMessage';
|
|
|
|
import FilterTags from 'components/metrics/FilterTags';
|
2023-02-15 02:23:20 +01:00
|
|
|
import RefreshButton from 'components/input/RefreshButton';
|
2022-12-29 00:43:22 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
2020-09-18 07:52:20 +02:00
|
|
|
import useDateRange from 'hooks/useDateRange';
|
2020-09-19 19:35:05 +02:00
|
|
|
import useTimezone from 'hooks/useTimezone';
|
2020-09-26 07:31:18 +02:00
|
|
|
import usePageQuery from 'hooks/usePageQuery';
|
2023-03-03 21:37:26 +01:00
|
|
|
import { getDateArray, getDateLength } from 'lib/date';
|
2023-02-02 03:39:54 +01:00
|
|
|
import Icons from 'components/icons';
|
2023-03-22 22:05:55 +01:00
|
|
|
import useSticky from 'hooks/useSticky';
|
|
|
|
import useMessages from 'hooks/useMessages';
|
2023-04-10 05:22:28 +02:00
|
|
|
import styles from './WebsiteChart.module.css';
|
2023-04-19 06:31:25 +02:00
|
|
|
import useLocale from 'hooks/useLocale';
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2020-08-01 04:05:14 +02:00
|
|
|
export default function WebsiteChart({
|
|
|
|
websiteId,
|
2023-03-31 14:55:28 +02:00
|
|
|
name,
|
2020-10-21 15:44:43 +02:00
|
|
|
domain,
|
2020-08-04 08:20:35 +02:00
|
|
|
stickyHeader = false,
|
2022-01-14 09:04:07 +01:00
|
|
|
showChart = true,
|
2023-02-02 03:39:54 +01:00
|
|
|
showDetailsButton = false,
|
2020-08-04 08:20:35 +02:00
|
|
|
onDataLoad = () => {},
|
2020-08-01 04:05:14 +02:00
|
|
|
}) {
|
2023-03-22 22:05:55 +01:00
|
|
|
const { formatMessage, labels } = useMessages();
|
2023-02-15 02:23:20 +01:00
|
|
|
const [dateRange] = useDateRange(websiteId);
|
2020-09-01 00:02:32 +02:00
|
|
|
const { startDate, endDate, unit, value, modified } = dateRange;
|
2020-09-19 19:35:05 +02:00
|
|
|
const [timezone] = useTimezone();
|
2020-09-26 07:31:18 +02:00
|
|
|
const {
|
2023-04-14 07:28:29 +02:00
|
|
|
query: { url, referrer, os, browser, device, country, region, city, title },
|
2020-09-26 07:31:18 +02:00
|
|
|
} = usePageQuery();
|
2022-12-29 00:43:22 +01:00
|
|
|
const { get, useQuery } = useApi();
|
2023-03-22 09:53:34 +01:00
|
|
|
const { ref, isSticky } = useSticky({ enabled: stickyHeader });
|
2020-08-31 23:11:30 +02:00
|
|
|
|
2022-12-29 00:43:22 +01:00
|
|
|
const { data, isLoading, error } = useQuery(
|
2023-04-14 07:28:29 +02:00
|
|
|
[
|
|
|
|
'websites:pageviews',
|
|
|
|
{ websiteId, modified, url, referrer, os, browser, device, country, region, city, title },
|
|
|
|
],
|
2022-12-29 00:43:22 +01:00
|
|
|
() =>
|
|
|
|
get(`/websites/${websiteId}/pageviews`, {
|
2022-12-27 02:36:48 +01:00
|
|
|
startAt: +startDate,
|
|
|
|
endAt: +endDate,
|
2020-10-09 21:39:03 +02:00
|
|
|
unit,
|
2022-12-29 00:43:22 +01:00
|
|
|
timezone,
|
2020-10-09 21:39:03 +02:00
|
|
|
url,
|
2022-03-20 22:10:41 +01:00
|
|
|
referrer,
|
2022-04-10 12:51:43 +02:00
|
|
|
os,
|
|
|
|
browser,
|
|
|
|
device,
|
|
|
|
country,
|
2023-04-14 07:28:29 +02:00
|
|
|
region,
|
|
|
|
city,
|
|
|
|
title,
|
2022-12-29 00:43:22 +01:00
|
|
|
}),
|
|
|
|
{ onSuccess: onDataLoad },
|
2020-08-31 00:29:31 +02:00
|
|
|
);
|
2020-07-30 10:08:21 +02:00
|
|
|
|
2020-10-09 08:26:05 +02:00
|
|
|
const chartData = useMemo(() => {
|
2020-07-29 04:04:45 +02:00
|
|
|
if (data) {
|
2020-10-09 08:26:05 +02:00
|
|
|
return {
|
|
|
|
pageviews: getDateArray(data.pageviews, startDate, endDate, unit),
|
|
|
|
sessions: getDateArray(data.sessions, startDate, endDate, unit),
|
|
|
|
};
|
2020-07-29 04:04:45 +02:00
|
|
|
}
|
2020-10-09 08:26:05 +02:00
|
|
|
return { pageviews: [], sessions: [] };
|
2023-04-04 02:26:37 +02:00
|
|
|
}, [data, modified]);
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2023-04-19 06:31:25 +02:00
|
|
|
const { dir } = useLocale();
|
2020-07-29 04:04:45 +02:00
|
|
|
return (
|
2022-12-10 23:26:52 +01:00
|
|
|
<>
|
2023-03-31 14:55:28 +02:00
|
|
|
<WebsiteHeader websiteId={websiteId} name={name} domain={domain}>
|
2023-02-02 03:39:54 +01:00
|
|
|
{showDetailsButton && (
|
|
|
|
<Link href={`/websites/${websiteId}`}>
|
2023-03-24 00:33:10 +01:00
|
|
|
<Button variant="primary">
|
2023-03-09 05:23:32 +01:00
|
|
|
<Text>{formatMessage(labels.viewDetails)}</Text>
|
|
|
|
<Icon>
|
2023-04-19 06:31:25 +02:00
|
|
|
<Icon rotate={dir === 'rtl' ? 180 : 0}>
|
|
|
|
<Icons.ArrowRight />
|
|
|
|
</Icon>
|
2023-03-09 05:23:32 +01:00
|
|
|
</Icon>
|
|
|
|
</Button>
|
2023-02-02 03:39:54 +01:00
|
|
|
</Link>
|
|
|
|
)}
|
2023-02-10 12:26:57 +01:00
|
|
|
</WebsiteHeader>
|
2023-03-31 14:55:28 +02:00
|
|
|
<FilterTags
|
|
|
|
websiteId={websiteId}
|
2023-04-14 07:28:29 +02:00
|
|
|
params={{ url, referrer, os, browser, device, country, region, city, title }}
|
2023-03-31 14:55:28 +02:00
|
|
|
/>
|
2023-03-22 09:53:34 +01:00
|
|
|
<Row
|
|
|
|
ref={ref}
|
|
|
|
className={classNames(styles.header, {
|
|
|
|
[styles.sticky]: stickyHeader,
|
|
|
|
[styles.isSticky]: isSticky,
|
|
|
|
})}
|
2023-03-03 21:37:26 +01:00
|
|
|
>
|
2023-04-10 05:22:28 +02:00
|
|
|
<Column defaultSize={12} xl={8}>
|
2023-03-22 09:53:34 +01:00
|
|
|
<MetricsBar websiteId={websiteId} />
|
|
|
|
</Column>
|
2023-04-10 05:22:28 +02:00
|
|
|
<Column defaultSize={12} xl={4}>
|
|
|
|
<div className={styles.actions}>
|
|
|
|
<RefreshButton websiteId={websiteId} isLoading={isLoading} />
|
|
|
|
<DateFilter websiteId={websiteId} value={value} className={styles.dropdown} />
|
|
|
|
</div>
|
2023-03-22 09:53:34 +01:00
|
|
|
</Column>
|
|
|
|
</Row>
|
2022-12-10 23:26:52 +01:00
|
|
|
<Row>
|
|
|
|
<Column className={styles.chart}>
|
2020-10-04 07:36:51 +02:00
|
|
|
{error && <ErrorMessage />}
|
2022-01-14 09:04:07 +01:00
|
|
|
{showChart && (
|
2021-04-28 10:52:06 +02:00
|
|
|
<PageviewsChart
|
|
|
|
websiteId={websiteId}
|
|
|
|
data={chartData}
|
|
|
|
unit={unit}
|
|
|
|
records={getDateLength(startDate, endDate, unit)}
|
2022-12-29 00:43:22 +01:00
|
|
|
loading={isLoading}
|
2021-04-28 10:52:06 +02:00
|
|
|
/>
|
|
|
|
)}
|
2022-12-10 23:26:52 +01:00
|
|
|
</Column>
|
|
|
|
</Row>
|
|
|
|
</>
|
2020-07-29 04:04:45 +02:00
|
|
|
);
|
2020-07-28 10:17:45 +02:00
|
|
|
}
|