2020-08-31 00:29:31 +02:00
|
|
|
import React, { useMemo } from 'react';
|
2020-10-09 08:26:05 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2020-08-18 09:51:32 +02:00
|
|
|
import classNames from 'classnames';
|
2020-08-31 00:29:31 +02:00
|
|
|
import useFetch from 'hooks/useFetch';
|
2020-10-10 20:04:07 +02:00
|
|
|
import Dot from 'components/common/Dot';
|
2020-08-18 09:51:32 +02:00
|
|
|
import styles from './ActiveUsers.module.css';
|
|
|
|
|
2022-01-15 21:39:32 +01:00
|
|
|
export default function ActiveUsers({ websiteId, className, value, interval = 60000 }) {
|
2022-10-04 02:17:53 +02:00
|
|
|
const url = websiteId ? `/websites/${websiteId}/active` : null;
|
2022-03-04 04:45:49 +01:00
|
|
|
const { data } = useFetch(url, {
|
2022-01-15 21:39:32 +01:00
|
|
|
interval,
|
2020-10-09 21:39:03 +02:00
|
|
|
});
|
2020-08-31 00:29:31 +02:00
|
|
|
const count = useMemo(() => {
|
2022-03-27 06:02:18 +02:00
|
|
|
if (websiteId) {
|
2022-05-06 04:04:28 +02:00
|
|
|
return data?.[0]?.x || 0;
|
2022-03-27 06:02:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return value !== undefined ? value : 0;
|
|
|
|
}, [data, value, websiteId]);
|
2020-08-18 09:51:32 +02:00
|
|
|
|
|
|
|
if (count === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classNames(styles.container, className)}>
|
2020-10-10 20:04:07 +02:00
|
|
|
<Dot />
|
2020-08-18 09:51:32 +02:00
|
|
|
<div className={styles.text}>
|
2020-09-06 02:27:01 +02:00
|
|
|
<div>
|
|
|
|
<FormattedMessage
|
2020-09-17 07:29:40 +02:00
|
|
|
id="message.active-users"
|
2020-09-07 10:22:16 +02:00
|
|
|
defaultMessage="{x} current {x, plural, one {visitor} other {visitors}}"
|
|
|
|
values={{ x: count }}
|
2020-09-06 02:27:01 +02:00
|
|
|
/>
|
|
|
|
</div>
|
2020-08-18 09:51:32 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|