umami/components/pages/realtime/RealtimeLog.js

188 lines
4.8 KiB
JavaScript
Raw Normal View History

import { useMemo, useState } from 'react';
import { StatusLight, Icon } from 'react-basics';
import { useIntl, FormattedMessage } from 'react-intl';
2020-10-09 13:21:59 +02:00
import { FixedSizeList } from 'react-window';
2020-10-09 11:56:15 +02:00
import firstBy from 'thenby';
2020-10-13 01:31:51 +02:00
import FilterButtons from 'components/common/FilterButtons';
2021-02-27 07:41:05 +01:00
import NoData from 'components/common/NoData';
2022-02-22 22:31:37 +01:00
import { getDeviceMessage, labels } from 'components/messages';
2020-10-09 13:21:59 +02:00
import useLocale from 'hooks/useLocale';
import useCountryNames from 'hooks/useCountryNames';
import { BROWSERS } from 'lib/constants';
import { stringToColor } from 'lib/format';
2021-02-27 07:41:05 +01:00
import { dateFormat } from 'lib/date';
2022-09-19 16:58:52 +02:00
import { safeDecodeURI } from 'next-basics';
import Icons from 'components/icons';
2020-10-09 11:56:15 +02:00
import styles from './RealtimeLog.module.css';
const TYPE_ALL = 'type-all';
const TYPE_PAGEVIEW = 'type-pageview';
const TYPE_SESSION = 'type-session';
const TYPE_EVENT = 'type-event';
2020-10-10 02:58:27 +02:00
const TYPE_ICONS = {
[TYPE_PAGEVIEW]: <Icons.Eye />,
[TYPE_SESSION]: <Icons.Visitor />,
[TYPE_EVENT]: <Icons.Bolt />,
2020-10-10 02:58:27 +02:00
};
2020-11-07 02:43:04 +01:00
export default function RealtimeLog({ data, websites, websiteId }) {
const { formatMessage } = useIntl();
const { locale } = useLocale();
2020-10-09 11:56:15 +02:00
const countryNames = useCountryNames(locale);
2020-10-13 01:31:51 +02:00
const [filter, setFilter] = useState(TYPE_ALL);
2020-10-09 11:56:15 +02:00
const logs = useMemo(() => {
if (!data) {
return [];
}
2020-10-09 11:56:15 +02:00
const { pageviews, sessions, events } = data;
const logs = [...pageviews, ...sessions, ...events].sort(firstBy('createdAt', -1));
2020-10-13 01:31:51 +02:00
if (filter) {
return logs.filter(row => getType(row) === filter);
}
return logs;
}, [data, filter]);
2020-10-09 11:56:15 +02:00
const uuids = useMemo(() => {
if (!data) {
return [];
}
return data.sessions.reduce((obj, { sessionId, sessionUuid }) => {
obj[sessionId] = sessionUuid;
return obj;
}, {});
}, [data]);
2020-10-13 01:31:51 +02:00
const buttons = [
{
label: formatMessage(labels.all),
key: TYPE_ALL,
2020-10-13 01:31:51 +02:00
},
{
label: formatMessage(labels.views),
key: TYPE_PAGEVIEW,
2020-10-13 01:31:51 +02:00
},
{
label: formatMessage(labels.sessions),
key: TYPE_SESSION,
2020-10-13 01:31:51 +02:00
},
{
label: formatMessage(labels.events),
key: TYPE_EVENT,
2020-10-13 01:31:51 +02:00
},
];
function getType({ pageviewId, sessionId, eventId }) {
if (eventId) {
2020-10-10 02:58:27 +02:00
return TYPE_EVENT;
2020-10-09 11:56:15 +02:00
}
if (pageviewId) {
2020-10-10 02:58:27 +02:00
return TYPE_PAGEVIEW;
2020-10-09 11:56:15 +02:00
}
if (sessionId) {
2020-10-10 02:58:27 +02:00
return TYPE_SESSION;
2020-10-09 13:21:59 +02:00
}
return null;
}
2020-10-10 02:58:27 +02:00
function getIcon(row) {
return TYPE_ICONS[getType(row)];
2020-10-09 11:56:15 +02:00
}
function getWebsite({ websiteId }) {
return websites.find(n => n.id === websiteId);
2020-10-09 11:56:15 +02:00
}
2020-10-10 02:58:27 +02:00
function getDetail({
eventName,
pageviewId,
sessionId,
2020-10-09 11:56:15 +02:00
url,
browser,
os,
country,
device,
websiteId,
2020-10-09 11:56:15 +02:00
}) {
if (eventName) {
return <div>{eventName}</div>;
2020-10-09 11:56:15 +02:00
}
if (pageviewId) {
const domain = getWebsite({ websiteId })?.domain;
2020-11-03 04:37:13 +01:00
return (
<a
className={styles.link}
href={`//${domain}${url}`}
target="_blank"
rel="noreferrer noopener"
>
2022-09-19 16:58:52 +02:00
{safeDecodeURI(url)}
2020-11-03 04:37:13 +01:00
</a>
);
2020-10-09 11:56:15 +02:00
}
if (sessionId) {
2020-10-09 11:56:15 +02:00
return (
<FormattedMessage
id="message.log.visitor"
defaultMessage="Visitor from {country} using {browser} on {os} {device}"
2020-10-13 01:31:51 +02:00
values={{
country: <b>{countryNames[country] || formatMessage(labels.unknown)}</b>,
2020-10-14 23:16:00 +02:00
browser: <b>{BROWSERS[browser]}</b>,
os: <b>{os}</b>,
device: <b>{formatMessage(getDeviceMessage(device))}</b>,
2020-10-13 01:31:51 +02:00
}}
2020-10-09 11:56:15 +02:00
/>
);
}
}
function getTime({ createdAt }) {
return dateFormat(new Date(createdAt), 'pp', locale);
2020-10-11 07:36:55 +02:00
}
function getColor(row) {
const { sessionId } = row;
2020-10-11 07:36:55 +02:00
return stringToColor(uuids[sessionId] || `${sessionId}${getWebsite(row)}`);
2020-10-11 07:36:55 +02:00
}
2020-10-09 13:21:59 +02:00
const Row = ({ index, style }) => {
2020-10-10 10:16:28 +02:00
const row = logs[index];
2020-10-09 13:21:59 +02:00
return (
2020-10-10 10:16:28 +02:00
<div className={styles.row} style={style}>
<div>
<StatusLight color={getColor(row)} />
</div>
2020-10-11 07:36:55 +02:00
<div className={styles.time}>{getTime(row)}</div>
2020-10-10 10:16:28 +02:00
<div className={styles.detail}>
<Icon className={styles.icon} icon={getIcon(row)} />
{getDetail(row)}
</div>
2020-11-07 02:43:04 +01:00
{!websiteId && websites.length > 1 && (
<div className={styles.website}>{getWebsite(row)?.domain}</div>
)}
2020-10-09 13:21:59 +02:00
</div>
);
};
2020-10-09 11:56:15 +02:00
return (
2020-10-10 10:16:28 +02:00
<div className={styles.table}>
<FilterButtons items={buttons} selectedKey={filter} onSelect={setFilter} />
2020-10-10 05:37:24 +02:00
<div className={styles.header}>
<FormattedMessage id="label.realtime-logs" defaultMessage="Realtime logs" />
</div>
2020-10-10 10:16:28 +02:00
<div className={styles.body}>
2020-10-14 23:16:00 +02:00
{logs?.length === 0 && <NoData />}
{logs?.length > 0 && (
<FixedSizeList height={400} itemCount={logs.length} itemSize={40}>
{Row}
</FixedSizeList>
)}
2020-10-10 10:16:28 +02:00
</div>
2020-10-09 11:56:15 +02:00
</div>
);
}