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

import { RoutePath } from '../../shared/routes';
import { Link, LinkProps } from '../lib/components/link';
import { useHistory } from '../lib/history';

export type InternalLinkProps = Omit<LinkProps, 'href' | 'as'> & {
  to: RoutePath;
};

function InternalLink({ to, onClick, ...props }: InternalLinkProps) {
  const history = useHistory();
  const navigate = useCallback(
    (e: React.MouseEvent<HTMLAnchorElement>) => {
      e.preventDefault();
      if (onClick) {
        onClick(e);
      }
      return history.push(to);
    },
    [history, to, onClick],
  );
  return <Link href="" onClick={navigate} {...props} />;
}

const InternalLinkNamespace = Object.assign(InternalLink, {
  Text: Link.Text,
  Icon: Link.Icon,
});

export { InternalLinkNamespace as InternalLink };