aboutsummaryrefslogtreecommitdiff
path: root/src/components/metrics/ActiveUsers.tsx
blob: a4bc7da22a8cef5880b39e9a84abd98a7fa7010f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { StatusLight, Text } from '@umami/react-zen';
import { useMemo } from 'react';
import { LinkButton } from '@/components/common/LinkButton';
import { useActyiveUsersQuery, useMessages } from '@/components/hooks';

export function ActiveUsers({
  websiteId,
  value,
  refetchInterval = 60000,
}: {
  websiteId: string;
  value?: number;
  refetchInterval?: number;
}) {
  const { formatMessage, labels } = useMessages();
  const { data } = useActyiveUsersQuery(websiteId, { refetchInterval });

  const count = useMemo(() => {
    if (websiteId) {
      return data?.visitors || 0;
    }

    return value !== undefined ? value : 0;
  }, [data, value, websiteId]);

  if (count === 0) {
    return null;
  }

  return (
    <LinkButton href={`/websites/${websiteId}/realtime`} variant="quiet">
      <StatusLight variant="success">
        <Text size="2" weight="medium">
          {count} {formatMessage(labels.online)}
        </Text>
      </StatusLight>
    </LinkButton>
  );
}