aboutsummaryrefslogtreecommitdiff
path: root/src/components/common/FilterLink.tsx
blob: d719a37e8987b908c55c28c5ac985a34a90ae5d8 (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
40
41
42
43
44
45
46
47
48
49
import { Icon, Row, Text } from '@umami/react-zen';
import Link from 'next/link';
import { type HTMLAttributes, type ReactNode, useState } from 'react';
import { useMessages, useNavigation } from '@/components/hooks';
import { ExternalLink } from '@/components/icons';

export interface FilterLinkProps extends HTMLAttributes<HTMLDivElement> {
  type: string;
  value: string;
  label?: string;
  icon?: ReactNode;
  externalUrl?: string;
}

export function FilterLink({ type, value, label, externalUrl, icon }: FilterLinkProps) {
  const [showLink, setShowLink] = useState(false);
  const { formatMessage, labels } = useMessages();
  const { updateParams, query } = useNavigation();
  const active = query[type] !== undefined;
  const selected = query[type] === value;

  return (
    <Row
      alignItems="center"
      gap
      fontWeight={active && selected ? 'bold' : undefined}
      color={active && !selected ? 'muted' : undefined}
      onMouseOver={() => setShowLink(true)}
      onMouseOut={() => setShowLink(false)}
    >
      {icon}
      {!value && `(${label || formatMessage(labels.unknown)})`}
      {value && (
        <Text title={label || value} truncate>
          <Link href={updateParams({ [type]: `eq.${value}` })} replace>
            {label || value}
          </Link>
        </Text>
      )}
      {externalUrl && showLink && (
        <a href={externalUrl} target="_blank" rel="noreferrer noopener">
          <Icon color="muted">
            <ExternalLink />
          </Icon>
        </a>
      )}
    </Row>
  );
}