2020-08-31 21:11:30 +00:00
|
|
|
import React, { useMemo } from 'react';
|
2020-08-02 04:20:52 +00:00
|
|
|
import classNames from 'classnames';
|
2020-07-28 08:17:45 +00:00
|
|
|
import PageviewsChart from './PageviewsChart';
|
2020-07-31 03:11:43 +00:00
|
|
|
import MetricsBar from './MetricsBar';
|
2020-09-18 05:52:20 +00:00
|
|
|
import WebsiteHeader from './WebsiteHeader';
|
2020-08-30 22:29:31 +00:00
|
|
|
import DateFilter from 'components/common/DateFilter';
|
|
|
|
|
import StickyHeader from 'components/helpers/StickyHeader';
|
2022-01-14 08:04:07 +00:00
|
|
|
import ErrorMessage from 'components/common/ErrorMessage';
|
|
|
|
|
import FilterTags from 'components/metrics/FilterTags';
|
2020-08-30 22:29:31 +00:00
|
|
|
import useFetch from 'hooks/useFetch';
|
2020-09-18 05:52:20 +00:00
|
|
|
import useDateRange from 'hooks/useDateRange';
|
2020-09-19 17:35:05 +00:00
|
|
|
import useTimezone from 'hooks/useTimezone';
|
2020-09-26 05:31:18 +00:00
|
|
|
import usePageQuery from 'hooks/usePageQuery';
|
2022-03-02 05:13:15 +00:00
|
|
|
import { getDateArray, getDateLength, getDateRangeValues } from 'lib/date';
|
2021-11-22 06:00:14 +00:00
|
|
|
import useShareToken from 'hooks/useShareToken';
|
2022-03-02 05:13:15 +00:00
|
|
|
import useApi from 'hooks/useApi';
|
2021-11-22 06:00:14 +00:00
|
|
|
import { TOKEN_HEADER } from 'lib/constants';
|
2020-08-04 01:12:28 +00:00
|
|
|
import styles from './WebsiteChart.module.css';
|
2020-07-28 08:17:45 +00:00
|
|
|
|
2020-08-01 02:05:14 +00:00
|
|
|
export default function WebsiteChart({
|
|
|
|
|
websiteId,
|
2020-08-31 21:11:30 +00:00
|
|
|
title,
|
2020-10-21 13:44:43 +00:00
|
|
|
domain,
|
2020-08-04 06:20:35 +00:00
|
|
|
stickyHeader = false,
|
2020-08-31 21:11:30 +00:00
|
|
|
showLink = false,
|
2022-01-14 08:04:07 +00:00
|
|
|
showChart = true,
|
2020-08-04 06:20:35 +00:00
|
|
|
onDataLoad = () => {},
|
2020-08-01 02:05:14 +00:00
|
|
|
}) {
|
2020-10-11 09:29:55 +00:00
|
|
|
const shareToken = useShareToken();
|
2020-09-19 17:35:05 +00:00
|
|
|
const [dateRange, setDateRange] = useDateRange(websiteId);
|
2020-08-31 22:02:32 +00:00
|
|
|
const { startDate, endDate, unit, value, modified } = dateRange;
|
2020-09-19 17:35:05 +00:00
|
|
|
const [timezone] = useTimezone();
|
2020-09-26 05:31:18 +00:00
|
|
|
const {
|
|
|
|
|
router,
|
|
|
|
|
resolve,
|
2021-11-22 06:00:14 +00:00
|
|
|
query: { url, ref },
|
2020-09-26 05:31:18 +00:00
|
|
|
} = usePageQuery();
|
2022-03-02 05:13:15 +00:00
|
|
|
const { get } = useApi();
|
2020-08-31 21:11:30 +00:00
|
|
|
|
2020-10-04 05:36:51 +00:00
|
|
|
const { data, loading, error } = useFetch(
|
2022-02-23 07:52:31 +00:00
|
|
|
`/website/${websiteId}/pageviews`,
|
2020-08-30 22:29:31 +00:00
|
|
|
{
|
2020-10-09 19:39:03 +00:00
|
|
|
params: {
|
|
|
|
|
start_at: +startDate,
|
|
|
|
|
end_at: +endDate,
|
|
|
|
|
unit,
|
|
|
|
|
tz: timezone,
|
|
|
|
|
url,
|
2021-11-22 06:00:14 +00:00
|
|
|
ref,
|
2020-10-09 19:39:03 +00:00
|
|
|
},
|
|
|
|
|
onDataLoad,
|
2020-10-11 09:29:55 +00:00
|
|
|
headers: { [TOKEN_HEADER]: shareToken?.token },
|
2020-08-30 22:29:31 +00:00
|
|
|
},
|
2021-11-22 06:00:14 +00:00
|
|
|
[modified, url, ref],
|
2020-08-30 22:29:31 +00:00
|
|
|
);
|
2020-07-30 08:08:21 +00:00
|
|
|
|
2020-10-09 06:26:05 +00:00
|
|
|
const chartData = useMemo(() => {
|
2020-07-29 02:04:45 +00:00
|
|
|
if (data) {
|
2020-10-09 06:26:05 +00:00
|
|
|
return {
|
|
|
|
|
pageviews: getDateArray(data.pageviews, startDate, endDate, unit),
|
|
|
|
|
sessions: getDateArray(data.sessions, startDate, endDate, unit),
|
|
|
|
|
};
|
2020-07-29 02:04:45 +00:00
|
|
|
}
|
2020-10-09 06:26:05 +00:00
|
|
|
return { pageviews: [], sessions: [] };
|
2022-01-21 03:54:58 +00:00
|
|
|
}, [data, startDate, endDate, unit]);
|
2020-07-28 08:17:45 +00:00
|
|
|
|
2021-11-22 06:00:14 +00:00
|
|
|
function handleCloseFilter(param) {
|
|
|
|
|
router.push(resolve({ [param]: undefined }));
|
2020-09-26 05:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-14 08:04:07 +00:00
|
|
|
async function handleDateChange(value) {
|
|
|
|
|
if (value === 'all') {
|
2022-03-02 05:13:15 +00:00
|
|
|
const { data, ok } = await get(`/website/${websiteId}`);
|
2022-01-14 08:04:07 +00:00
|
|
|
if (ok) {
|
|
|
|
|
setDateRange({ value, ...getDateRangeValues(new Date(data.created_at), Date.now()) });
|
|
|
|
|
}
|
2022-01-21 03:54:58 +00:00
|
|
|
} else {
|
|
|
|
|
setDateRange(value);
|
2022-01-14 08:04:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 02:04:45 +00:00
|
|
|
return (
|
2020-09-27 07:51:29 +00:00
|
|
|
<div className={styles.container}>
|
2020-10-21 13:44:43 +00:00
|
|
|
<WebsiteHeader websiteId={websiteId} title={title} domain={domain} showLink={showLink} />
|
2020-08-09 22:13:38 +00:00
|
|
|
<div className={classNames(styles.header, 'row')}>
|
|
|
|
|
<StickyHeader
|
|
|
|
|
className={classNames(styles.metrics, 'col row')}
|
|
|
|
|
stickyClassName={styles.sticky}
|
|
|
|
|
enabled={stickyHeader}
|
|
|
|
|
>
|
2021-11-22 06:00:14 +00:00
|
|
|
<FilterTags params={{ url, ref }} onClick={handleCloseFilter} />
|
2020-09-13 18:33:57 +00:00
|
|
|
<div className="col-12 col-lg-9">
|
2020-10-11 09:29:55 +00:00
|
|
|
<MetricsBar websiteId={websiteId} />
|
2020-09-13 18:33:57 +00:00
|
|
|
</div>
|
|
|
|
|
<div className={classNames(styles.filter, 'col-12 col-lg-3')}>
|
|
|
|
|
<DateFilter
|
|
|
|
|
value={value}
|
|
|
|
|
startDate={startDate}
|
|
|
|
|
endDate={endDate}
|
2022-01-14 08:04:07 +00:00
|
|
|
onChange={handleDateChange}
|
2020-09-13 18:33:57 +00:00
|
|
|
/>
|
|
|
|
|
</div>
|
2020-08-09 22:13:38 +00:00
|
|
|
</StickyHeader>
|
|
|
|
|
</div>
|
2020-08-02 04:20:52 +00:00
|
|
|
<div className="row">
|
2021-11-22 06:00:14 +00:00
|
|
|
<div className={classNames(styles.chart, 'col')}>
|
2020-10-04 05:36:51 +00:00
|
|
|
{error && <ErrorMessage />}
|
2022-01-14 08:04:07 +00:00
|
|
|
{showChart && (
|
2021-04-28 08:52:06 +00:00
|
|
|
<PageviewsChart
|
|
|
|
|
websiteId={websiteId}
|
|
|
|
|
data={chartData}
|
|
|
|
|
unit={unit}
|
|
|
|
|
records={getDateLength(startDate, endDate, unit)}
|
|
|
|
|
loading={loading}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2020-08-28 06:45:37 +00:00
|
|
|
</div>
|
2020-07-30 08:08:21 +00:00
|
|
|
</div>
|
2020-09-27 07:51:29 +00:00
|
|
|
</div>
|
2020-07-29 02:04:45 +00:00
|
|
|
);
|
2020-07-28 08:17:45 +00:00
|
|
|
}
|