2022-12-29 00:43:22 +01:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { StatusLight } from 'react-basics';
|
2023-02-02 03:39:54 +01:00
|
|
|
import { useIntl } from 'react-intl';
|
2022-12-29 00:43:22 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
2023-02-02 03:39:54 +01:00
|
|
|
import { messages } from 'components/messages';
|
2020-08-18 09:51:32 +02:00
|
|
|
import styles from './ActiveUsers.module.css';
|
|
|
|
|
2023-02-02 03:39:54 +01:00
|
|
|
export default function ActiveUsers({ websiteId, value, refetchInterval = 60000 }) {
|
|
|
|
const { formatMessage } = useIntl();
|
2022-12-29 00:43:22 +01:00
|
|
|
const { get, useQuery } = useApi();
|
2023-02-02 03:39:54 +01:00
|
|
|
const { data } = useQuery(
|
|
|
|
['websites:active', websiteId],
|
|
|
|
() => get(`/websites/${websiteId}/active`),
|
|
|
|
{
|
|
|
|
refetchInterval,
|
2023-02-15 02:23:20 +01:00
|
|
|
enabled: !!websiteId,
|
2023-02-02 03:39:54 +01:00
|
|
|
},
|
|
|
|
);
|
2022-12-29 00:43:22 +01: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 (
|
2023-02-02 03:39:54 +01:00
|
|
|
<StatusLight variant="success">
|
|
|
|
<div className={styles.text}>{formatMessage(messages.activeUsers, { x: count })}</div>
|
|
|
|
</StatusLight>
|
2020-08-18 09:51:32 +02:00
|
|
|
);
|
|
|
|
}
|