2022-01-15 21:39:32 +01:00
|
|
|
import React, { useMemo } from 'react';
|
2020-10-10 02:58:27 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2022-01-15 21:39:32 +01:00
|
|
|
import { differenceInMinutes } from 'date-fns';
|
2022-01-16 04:16:58 +01:00
|
|
|
import PageHeader from 'components/layout/PageHeader';
|
|
|
|
import DropDown from 'components/common/DropDown';
|
2021-12-26 02:01:57 +01:00
|
|
|
import ActiveUsers from './ActiveUsers';
|
2020-10-10 02:58:27 +02:00
|
|
|
import MetricCard from './MetricCard';
|
|
|
|
import styles from './RealtimeHeader.module.css';
|
|
|
|
|
|
|
|
export default function RealtimeHeader({ websites, data, websiteId, onSelect }) {
|
|
|
|
const options = [
|
2022-10-25 18:24:20 +02:00
|
|
|
{
|
|
|
|
label: <FormattedMessage id="label.all-websites" defaultMessage="All websites" />,
|
|
|
|
value: null,
|
|
|
|
},
|
2020-10-10 20:04:07 +02:00
|
|
|
].concat(
|
2022-10-25 18:24:20 +02:00
|
|
|
websites.map(({ name, websiteUuid }, index) => ({
|
2020-10-10 20:04:07 +02:00
|
|
|
label: name,
|
2022-10-25 18:24:20 +02:00
|
|
|
value: websiteUuid,
|
2020-10-10 20:04:07 +02:00
|
|
|
divider: index === 0,
|
|
|
|
})),
|
|
|
|
);
|
2020-10-10 02:58:27 +02:00
|
|
|
|
2020-10-10 09:07:08 +02:00
|
|
|
const { pageviews, sessions, events, countries } = data;
|
2020-10-10 02:58:27 +02:00
|
|
|
|
2022-01-15 21:39:32 +01:00
|
|
|
const count = useMemo(() => {
|
|
|
|
return sessions.filter(
|
2022-10-10 22:42:18 +02:00
|
|
|
({ createdAt }) => differenceInMinutes(new Date(), new Date(createdAt)) <= 5,
|
2022-01-15 21:39:32 +01:00
|
|
|
).length;
|
2022-04-11 09:26:43 +02:00
|
|
|
}, [sessions, websiteId]);
|
2022-01-15 21:39:32 +01:00
|
|
|
|
2020-10-10 02:58:27 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<PageHeader>
|
|
|
|
<div>
|
|
|
|
<FormattedMessage id="label.realtime" defaultMessage="Realtime" />
|
|
|
|
</div>
|
2021-12-26 02:01:57 +01:00
|
|
|
<div>
|
2022-04-11 09:26:43 +02:00
|
|
|
<ActiveUsers className={styles.active} value={count} />
|
2021-12-26 02:01:57 +01:00
|
|
|
</div>
|
2020-10-10 02:58:27 +02:00
|
|
|
<DropDown value={websiteId} options={options} onChange={onSelect} />
|
|
|
|
</PageHeader>
|
|
|
|
<div className={styles.metrics}>
|
|
|
|
<MetricCard
|
|
|
|
label={<FormattedMessage id="metrics.views" defaultMessage="Views" />}
|
|
|
|
value={pageviews.length}
|
2021-12-02 00:07:10 +01:00
|
|
|
hideComparison
|
2020-10-10 02:58:27 +02:00
|
|
|
/>
|
|
|
|
<MetricCard
|
|
|
|
label={<FormattedMessage id="metrics.visitors" defaultMessage="Visitors" />}
|
|
|
|
value={sessions.length}
|
2021-12-02 00:07:10 +01:00
|
|
|
hideComparison
|
2020-10-10 02:58:27 +02:00
|
|
|
/>
|
|
|
|
<MetricCard
|
|
|
|
label={<FormattedMessage id="metrics.events" defaultMessage="Events" />}
|
|
|
|
value={events.length}
|
2021-12-02 00:07:10 +01:00
|
|
|
hideComparison
|
2020-10-10 02:58:27 +02:00
|
|
|
/>
|
|
|
|
<MetricCard
|
|
|
|
label={<FormattedMessage id="metrics.countries" defaultMessage="Countries" />}
|
2020-10-10 09:07:08 +02:00
|
|
|
value={countries.length}
|
2021-12-02 00:07:10 +01:00
|
|
|
hideComparison
|
2020-10-10 02:58:27 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|