2020-08-31 23:11:30 +02:00
|
|
|
import React, { useMemo } from 'react';
|
2020-08-02 06:20:52 +02: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';
|
2020-08-31 00:29:31 +02:00
|
|
|
import DateFilter from 'components/common/DateFilter';
|
|
|
|
import StickyHeader from 'components/helpers/StickyHeader';
|
2020-09-26 07:31:18 +02:00
|
|
|
import Button from 'components/common/Button';
|
2020-08-31 00:29:31 +02:00
|
|
|
import useFetch from 'hooks/useFetch';
|
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';
|
2020-09-19 19:35:05 +02:00
|
|
|
import { getDateArray, getDateLength } from 'lib/date';
|
2020-09-26 07:31:18 +02:00
|
|
|
import Times from 'assets/times.svg';
|
2020-08-04 03:12:28 +02:00
|
|
|
import styles from './WebsiteChart.module.css';
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2020-08-01 04:05:14 +02:00
|
|
|
export default function WebsiteChart({
|
|
|
|
websiteId,
|
2020-09-18 07:52:20 +02:00
|
|
|
token,
|
2020-08-31 23:11:30 +02:00
|
|
|
title,
|
2020-08-04 08:20:35 +02:00
|
|
|
stickyHeader = false,
|
2020-08-31 23:11:30 +02:00
|
|
|
showLink = false,
|
2020-08-04 08:20:35 +02:00
|
|
|
onDataLoad = () => {},
|
2020-08-01 04:05:14 +02:00
|
|
|
}) {
|
2020-09-19 19:35:05 +02:00
|
|
|
const [dateRange, setDateRange] = 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 {
|
|
|
|
router,
|
|
|
|
resolve,
|
|
|
|
query: { url },
|
|
|
|
} = usePageQuery();
|
2020-08-31 23:11:30 +02:00
|
|
|
|
2020-09-20 20:28:38 +02:00
|
|
|
const { data, loading } = useFetch(
|
2020-08-31 00:29:31 +02:00
|
|
|
`/api/website/${websiteId}/pageviews`,
|
|
|
|
{
|
|
|
|
start_at: +startDate,
|
|
|
|
end_at: +endDate,
|
|
|
|
unit,
|
2020-09-19 19:35:05 +02:00
|
|
|
tz: timezone,
|
2020-09-26 07:31:18 +02:00
|
|
|
url,
|
2020-09-18 07:52:20 +02:00
|
|
|
token,
|
2020-08-31 00:29:31 +02:00
|
|
|
},
|
2020-08-31 23:11:30 +02:00
|
|
|
{ onDataLoad, update: [modified] },
|
2020-08-31 00:29:31 +02:00
|
|
|
);
|
2020-07-30 10:08:21 +02:00
|
|
|
|
2020-07-29 04:04:45 +02:00
|
|
|
const [pageviews, uniques] = useMemo(() => {
|
|
|
|
if (data) {
|
|
|
|
return [
|
|
|
|
getDateArray(data.pageviews, startDate, endDate, unit),
|
|
|
|
getDateArray(data.uniques, startDate, endDate, unit),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return [[], []];
|
|
|
|
}, [data]);
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2020-09-26 07:31:18 +02:00
|
|
|
function handleCloseFilter() {
|
|
|
|
router.push(resolve({ url: undefined }));
|
|
|
|
}
|
|
|
|
|
2020-07-29 04:04:45 +02:00
|
|
|
return (
|
2020-09-27 09:51:29 +02:00
|
|
|
<div className={styles.container}>
|
2020-09-18 07:52:20 +02:00
|
|
|
<WebsiteHeader websiteId={websiteId} token={token} title={title} showLink={showLink} />
|
2020-08-10 00:13:38 +02:00
|
|
|
<div className={classNames(styles.header, 'row')}>
|
|
|
|
<StickyHeader
|
|
|
|
className={classNames(styles.metrics, 'col row')}
|
|
|
|
stickyClassName={styles.sticky}
|
|
|
|
enabled={stickyHeader}
|
|
|
|
>
|
2020-09-26 07:31:18 +02:00
|
|
|
{url && <PageFilter url={url} onClick={handleCloseFilter} />}
|
2020-09-13 20:33:57 +02:00
|
|
|
<div className="col-12 col-lg-9">
|
2020-09-18 07:52:20 +02:00
|
|
|
<MetricsBar websiteId={websiteId} token={token} />
|
2020-09-13 20:33:57 +02:00
|
|
|
</div>
|
|
|
|
<div className={classNames(styles.filter, 'col-12 col-lg-3')}>
|
|
|
|
<DateFilter
|
|
|
|
value={value}
|
|
|
|
startDate={startDate}
|
|
|
|
endDate={endDate}
|
2020-09-19 19:35:05 +02:00
|
|
|
onChange={setDateRange}
|
2020-09-13 20:33:57 +02:00
|
|
|
/>
|
|
|
|
</div>
|
2020-08-10 00:13:38 +02:00
|
|
|
</StickyHeader>
|
|
|
|
</div>
|
2020-08-02 06:20:52 +02:00
|
|
|
<div className="row">
|
2020-08-28 08:45:37 +02:00
|
|
|
<div className="col">
|
2020-08-31 23:11:30 +02:00
|
|
|
<PageviewsChart
|
|
|
|
websiteId={websiteId}
|
|
|
|
data={{ pageviews, uniques }}
|
|
|
|
unit={unit}
|
|
|
|
records={getDateLength(startDate, endDate, unit)}
|
2020-09-20 20:28:38 +02:00
|
|
|
loading={loading}
|
2020-08-31 23:11:30 +02:00
|
|
|
/>
|
2020-08-28 08:45:37 +02:00
|
|
|
</div>
|
2020-07-30 10:08:21 +02:00
|
|
|
</div>
|
2020-09-27 09:51:29 +02:00
|
|
|
</div>
|
2020-07-29 04:04:45 +02:00
|
|
|
);
|
2020-07-28 10:17:45 +02:00
|
|
|
}
|
2020-09-26 07:31:18 +02:00
|
|
|
|
|
|
|
const PageFilter = ({ url, onClick }) => {
|
|
|
|
return (
|
|
|
|
<div className={classNames(styles.url, 'col-12')}>
|
2020-09-26 08:38:28 +02:00
|
|
|
<Button icon={<Times />} onClick={onClick} variant="action" iconRight>
|
2020-09-26 07:31:18 +02:00
|
|
|
{url}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|