aboutsummaryrefslogtreecommitdiffstats
path: root/client/js/app/src/app/components/link/link.jsx
blob: 288174d21be6637b827e598ccf9e45435237cd14 (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
import React from 'react';
import { Link as InternalLink } from 'react-router-dom';
import { Anchor } from '@mantine/core';

export const isInternalLink = (link) => {
  if (!link) return false;
  return !/^[a-z]+:\/\//.test(link);
};

export function Link({ to, api = false, ...props }) {
  const internal = !api && isInternalLink(to);

  if (!props.download && to && internal)
    return <Anchor component={InternalLink} to={to} {...props} />;

  const fixedProps = Object.assign(
    to ? { href: (api ? window.config.api : '') + to } : {},
    to && !internal && { target: '_blank', rel: 'noopener noreferrer' },
    props
  );

  return <Anchor {...fixedProps} />;
}