umami/components/metrics/ActiveUsers.js

38 lines
969 B
JavaScript
Raw Normal View History

import { useMemo } from 'react';
import { StatusLight } from 'react-basics';
2023-02-02 03:39:54 +01:00
import { useIntl } from 'react-intl';
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();
const { get, useQuery } = useApi();
2023-02-02 03:39:54 +01:00
const { data } = useQuery(
['websites:active', websiteId],
() => get(`/websites/${websiteId}/active`),
{
refetchInterval,
enabled: !!websiteId,
2023-02-02 03:39:54 +01:00
},
);
const count = useMemo(() => {
2022-03-27 06:02:18 +02:00
if (websiteId) {
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
);
}