diff --git a/frontend/src/Components/Link/Link.tsx b/frontend/src/Components/Link/Link.tsx index 5015a1fe3..d6d731106 100644 --- a/frontend/src/Components/Link/Link.tsx +++ b/frontend/src/Components/Link/Link.tsx @@ -1,96 +1,89 @@ import classNames from 'classnames'; import React, { - ComponentClass, - FunctionComponent, + ComponentPropsWithoutRef, + ElementType, SyntheticEvent, useCallback, } from 'react'; import { Link as RouterLink } from 'react-router-dom'; import styles from './Link.css'; -interface ReactRouterLinkProps { - to?: string; -} +export type LinkProps = + ComponentPropsWithoutRef & { + component?: C; + to?: string; + target?: string; + isDisabled?: LinkProps['disabled']; + noRouter?: boolean; + onPress?(event: SyntheticEvent): void; + }; -export interface LinkProps extends React.HTMLProps { - className?: string; - component?: - | string - | FunctionComponent - | ComponentClass; - to?: string; - target?: string; - isDisabled?: boolean; - noRouter?: boolean; - onPress?(event: SyntheticEvent): void; -} -function Link(props: LinkProps) { - const { - className, - component = 'button', - to, - target, - type, - isDisabled, - noRouter = false, - onPress, - ...otherProps - } = props; +export default function Link({ + className, + component, + to, + target, + type, + isDisabled, + noRouter, + onPress, + ...otherProps +}: LinkProps) { + const Component = component || 'button'; const onClick = useCallback( (event: SyntheticEvent) => { - if (!isDisabled && onPress) { - onPress(event); + if (isDisabled) { + return; } + + onPress?.(event); }, [isDisabled, onPress] ); - const linkProps: React.HTMLProps & ReactRouterLinkProps = { - target, - }; - let el = component; - - if (to) { - if (/\w+?:\/\//.test(to)) { - el = 'a'; - linkProps.href = to; - linkProps.target = target || '_blank'; - linkProps.rel = 'noreferrer'; - } else if (noRouter) { - el = 'a'; - linkProps.href = to; - linkProps.target = target || '_self'; - } else { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - el = RouterLink; - linkProps.to = `${window.Sonarr.urlBase}/${to.replace(/^\//, '')}`; - linkProps.target = target; - } - } - - if (el === 'button' || el === 'input') { - linkProps.type = type || 'button'; - linkProps.disabled = isDisabled; - } - - linkProps.className = classNames( + const linkClass = classNames( className, styles.link, to && styles.to, isDisabled && 'isDisabled' ); - const elementProps = { - ...otherProps, - type, - ...linkProps, - }; + if (to) { + const toLink = /\w+?:\/\//.test(to); - elementProps.onClick = onClick; + if (toLink || noRouter) { + return ( + + ); + } - return React.createElement(el, elementProps); + return ( + + ); + } + + return ( + + ); } - -export default Link;