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