summaryrefslogtreecommitdiffhomepage
path: root/desktop/packages/mullvad-vpn/src/renderer/components/ExternalLink.tsx
blob: 2a7c6a0c62187a34c7b35fa02c13c1ccc0a9e33c (plain)
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
import { useCallback } from 'react';

import { Url } from '../../shared/constants';
import { useAppContext } from '../context';
import { Link, LinkProps } from '../lib/components/link';

export type ExternalLinkProps = Omit<LinkProps, 'href' | 'as'> & {
  to: Url;
  withAuth?: boolean;
};

function ExternalLink({ to, onClick, withAuth, ...props }: ExternalLinkProps) {
  const { openUrl, openUrlWithAuth } = useAppContext();
  const navigate = useCallback(
    (e: React.MouseEvent<HTMLAnchorElement>) => {
      e.preventDefault();
      if (onClick) {
        onClick(e);
      }

      if (withAuth) {
        return openUrlWithAuth(to);
      }
      return openUrl(to);
    },
    [onClick, openUrl, openUrlWithAuth, to, withAuth],
  );
  return <Link href="" onClick={navigate} {...props} />;
}

const ExternalLinkNamespace = Object.assign(ExternalLink, {
  Text: Link.Text,
  Icon: Link.Icon,
});

export { ExternalLinkNamespace as ExternalLink };