aboutsummaryrefslogtreecommitdiffstats
path: root/webui/src/components/Content/AnchorTag.tsx
blob: 47d5e2fac3433951f3f10cfef9012f92239efd91 (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
import React from 'react';
import { Link } from 'react-router-dom';

import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles((theme) => ({
  tag: {
    color: theme.palette.text.secondary,
  },
}));

const AnchorTag = ({ children, href }: React.HTMLProps<HTMLAnchorElement>) => {
  const classes = useStyles();
  const origin = window.location.origin;
  const destination = href === undefined ? '' : href;
  const isInternalLink =
    destination.startsWith('/') || destination.startsWith(origin);
  const internalDestination = destination.replace(origin, '');
  const internalLink = (
    <Link className={classes.tag} to={internalDestination}>
      {children}
    </Link>
  );
  const externalLink = (
    <a
      className={classes.tag}
      href={destination}
      target="_blank"
      rel="noopener noreferrer"
    >
      {children}
    </a>
  );

  return isInternalLink ? internalLink : externalLink;
};

export default AnchorTag;