/* * SonarQube * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as React from 'react'; import { Link } from 'react-router'; import DetachIcon from '../../components/icons/DetachIcon'; import { isSonarCloud } from '../../helpers/system'; import { AppState } from '../../types/types'; import { withAppState } from '../hoc/withAppState'; interface OwnProps { appState: Pick; customProps?: { [k: string]: any; }; } type Props = OwnProps & React.AnchorHTMLAttributes; const SONARCLOUD_LINK = '/#sonarcloud#/'; const SONARQUBE_LINK = '/#sonarqube#/'; const SONARQUBE_ADMIN_LINK = '/#sonarqube-admin#/'; export class DocLink extends React.PureComponent { handleClickOnAnchor = (event: React.MouseEvent) => { const { customProps, href = '#' } = this.props; if (customProps && customProps.onAnchorClick) { customProps.onAnchorClick(href, event); } }; render() { const { appState, children, href, customProps, ...other } = this.props; if (href && href.startsWith('#')) { return ( {children} ); } if (href && href.startsWith('/')) { if (href.startsWith(SONARCLOUD_LINK)) { return {children}; } else if (href.startsWith(SONARQUBE_LINK)) { return {children}; } else if (href.startsWith(SONARQUBE_ADMIN_LINK)) { return ( {children} ); } else { const url = '/documentation' + href; return ( {children} ); } } return ( <> {children} ); } } export default withAppState(DocLink); interface SonarCloudLinkProps { children: React.ReactNode; url: string; } function SonarCloudLink({ children, url }: SonarCloudLinkProps) { if (!isSonarCloud()) { return <>{children}; } else { const to = `/${url.substr(SONARCLOUD_LINK.length)}`; return {children}; } } interface SonarQubeLinkProps { children: React.ReactNode; url: string; } function SonarQubeLink({ children, url }: SonarQubeLinkProps) { if (isSonarCloud()) { return <>{children}; } else { const to = `/${url.substr(SONARQUBE_LINK.length)}`; return ( {children} ); } } interface SonarQubeAdminLinkProps { canAdmin?: boolean; children: React.ReactNode; url: string; } function SonarQubeAdminLink({ canAdmin, children, url }: SonarQubeAdminLinkProps) { if (isSonarCloud() || !canAdmin) { return <>{children}; } else { const to = `/${url.substr(SONARQUBE_ADMIN_LINK.length)}`; return ( {children} ); } }