umami/src/app/(main)/websites/[websiteId]/realtime/RealtimeLog.tsx

195 lines
5.4 KiB
TypeScript
Raw Normal View History

import { useContext, useMemo, useState } from 'react';
import { StatusLight, Icon, Text, SearchField } from 'react-basics';
2020-10-09 11:21:59 +00:00
import { FixedSizeList } from 'react-window';
import { format } from 'date-fns';
2023-12-09 08:35:54 +00:00
import thenby from 'thenby';
import { safeDecodeURI } from 'next-basics';
2020-10-12 23:31:51 +00:00
import FilterButtons from 'components/common/FilterButtons';
2023-07-02 05:02:49 +00:00
import Empty from 'components/common/Empty';
2024-02-06 07:59:33 +00:00
import { useLocale, useCountryNames, useMessages } from 'components/hooks';
import Icons from 'components/icons';
import useFormat from 'components//hooks/useFormat';
2020-10-09 11:21:59 +00:00
import { BROWSERS } from 'lib/constants';
import { stringToColor } from 'lib/format';
import { RealtimeData } from 'lib/types';
import { WebsiteContext } from '../WebsiteProvider';
2020-10-09 09:56:15 +00:00
import styles from './RealtimeLog.module.css';
2023-02-23 04:59:59 +00:00
const TYPE_ALL = 'all';
const TYPE_PAGEVIEW = 'pageview';
const TYPE_SESSION = 'session';
const TYPE_EVENT = 'event';
2020-10-10 00:58:27 +00:00
2023-02-23 04:59:59 +00:00
const icons = {
[TYPE_PAGEVIEW]: <Icons.Eye />,
[TYPE_SESSION]: <Icons.Visitor />,
[TYPE_EVENT]: <Icons.Bolt />,
2020-10-10 00:58:27 +00:00
};
export function RealtimeLog({ data }: { data: RealtimeData }) {
const website = useContext(WebsiteContext);
const [search, setSearch] = useState('');
2023-03-22 21:05:55 +00:00
const { formatMessage, labels, messages, FormattedMessage } = useMessages();
const { formatValue } = useFormat();
const { locale } = useLocale();
2020-10-09 09:56:15 +00:00
const countryNames = useCountryNames(locale);
2020-10-12 23:31:51 +00:00
const [filter, setFilter] = useState(TYPE_ALL);
2020-10-12 23:31:51 +00:00
const buttons = [
{
label: formatMessage(labels.all),
key: TYPE_ALL,
2020-10-12 23:31:51 +00:00
},
{
label: formatMessage(labels.views),
key: TYPE_PAGEVIEW,
2020-10-12 23:31:51 +00:00
},
{
2023-02-23 04:59:59 +00:00
label: formatMessage(labels.visitors),
key: TYPE_SESSION,
2020-10-12 23:31:51 +00:00
},
{
label: formatMessage(labels.events),
key: TYPE_EVENT,
2020-10-12 23:31:51 +00:00
},
];
2024-06-20 04:47:27 +00:00
const getTime = ({ timestamp }) => format(timestamp * 1000, 'h:mm:ss');
2023-02-23 04:59:59 +00:00
2023-07-29 05:40:57 +00:00
const getColor = ({ id, sessionId }) => stringToColor(sessionId || id);
2023-02-23 04:59:59 +00:00
const getIcon = ({ __type }) => icons[__type];
const getDetail = (log: {
2024-06-20 04:47:27 +00:00
__type: string;
eventName: string;
urlPath: string;
browser: string;
os: string;
country: string;
device: string;
}) => {
2023-04-07 01:31:16 +00:00
const { __type, eventName, urlPath: url, browser, os, country, device } = log;
2023-02-23 04:59:59 +00:00
if (__type === TYPE_EVENT) {
return (
<FormattedMessage
{...messages.eventLog}
values={{
event: <b>{eventName || formatMessage(labels.unknown)}</b>,
url: (
<a
href={`//${website?.domain}${url}`}
2023-02-23 04:59:59 +00:00
className={styles.link}
target="_blank"
rel="noreferrer noopener"
>
{url}
</a>
),
}}
/>
);
2020-10-09 09:56:15 +00:00
}
2023-02-23 04:59:59 +00:00
if (__type === TYPE_PAGEVIEW) {
2020-11-03 03:37:13 +00:00
return (
<a
href={`//${website?.domain}${url}`}
2023-02-23 04:59:59 +00:00
className={styles.link}
2020-11-03 03:37:13 +00:00
target="_blank"
rel="noreferrer noopener"
>
2022-09-19 14:58:52 +00:00
{safeDecodeURI(url)}
2020-11-03 03:37:13 +00:00
</a>
);
2020-10-09 09:56:15 +00:00
}
2023-02-23 04:59:59 +00:00
if (__type === TYPE_SESSION) {
2020-10-09 09:56:15 +00:00
return (
<FormattedMessage
2023-02-23 04:59:59 +00:00
{...messages.visitorLog}
2020-10-12 23:31:51 +00:00
values={{
country: <b>{countryNames[country] || formatMessage(labels.unknown)}</b>,
2020-10-14 21:16:00 +00:00
browser: <b>{BROWSERS[browser]}</b>,
os: <b>{os}</b>,
2023-03-16 22:56:05 +00:00
device: <b>{formatMessage(labels[device] || labels.unknown)}</b>,
2020-10-12 23:31:51 +00:00
}}
2020-10-09 09:56:15 +00:00
/>
);
}
2023-02-23 04:59:59 +00:00
};
2020-10-11 05:36:55 +00:00
2020-10-09 11:21:59 +00:00
const Row = ({ index, style }) => {
2020-10-10 08:16:28 +00:00
const row = logs[index];
2020-10-09 11:21:59 +00:00
return (
2020-10-10 08:16:28 +00:00
<div className={styles.row} style={style}>
<div>
<StatusLight color={getColor(row)} />
</div>
2020-10-11 05:36:55 +00:00
<div className={styles.time}>{getTime(row)}</div>
2020-10-10 08:16:28 +00:00
<div className={styles.detail}>
2023-02-23 04:59:59 +00:00
<Icon className={styles.icon}>{getIcon(row)}</Icon>
<Text>{getDetail(row)}</Text>
2020-10-10 08:16:28 +00:00
</div>
2020-10-09 11:21:59 +00:00
</div>
);
};
2023-02-23 04:59:59 +00:00
const logs = useMemo(() => {
if (!data) {
return [];
}
2024-06-20 04:47:27 +00:00
const { events, visitors } = data;
let logs = [
...events.map(e => ({ __type: e.eventName ? TYPE_EVENT : TYPE_PAGEVIEW, ...e })),
...visitors.map(v => ({ __type: TYPE_SESSION, ...v })),
].sort(thenby.firstBy('timestamp', -1));
if (search) {
logs = logs.filter(({ eventName, urlPath, browser, os, country, device }) => {
return [
eventName,
urlPath,
os,
formatValue(browser, 'browser'),
formatValue(country, 'country'),
formatValue(device, 'device'),
]
.filter(n => n)
.map(n => n.toLowerCase())
.join('')
.includes(search.toLowerCase());
});
}
2023-02-23 04:59:59 +00:00
if (filter !== TYPE_ALL) {
return logs.filter(({ __type }) => __type === filter);
}
return logs;
}, [data, filter, formatValue, search]);
2023-02-23 04:59:59 +00:00
2020-10-09 09:56:15 +00:00
return (
2020-10-10 08:16:28 +00:00
<div className={styles.table}>
<div className={styles.actions}>
<SearchField className={styles.search} value={search} onSearch={setSearch} />
<FilterButtons items={buttons} selectedKey={filter} onSelect={setFilter} />
</div>
2023-04-19 23:42:42 +00:00
<div className={styles.header}>{formatMessage(labels.activityLog)}</div>
2020-10-10 08:16:28 +00:00
<div className={styles.body}>
2023-07-02 05:02:49 +00:00
{logs?.length === 0 && <Empty />}
{logs?.length > 0 && (
2023-12-09 09:25:02 +00:00
<FixedSizeList width="100%" height={500} itemCount={logs.length} itemSize={50}>
{Row}
</FixedSizeList>
)}
2020-10-10 08:16:28 +00:00
</div>
2020-10-09 09:56:15 +00:00
</div>
);
}
2023-04-21 15:00:42 +00:00
export default RealtimeLog;