/* * 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 { stringify } from 'querystring'; import * as React from 'react'; import { Link } from 'react-router'; import { ButtonIcon } from '../../components/controls/buttons'; import { ClipboardIconButton } from '../../components/controls/clipboard'; import Dropdown from '../../components/controls/Dropdown'; import ListIcon from '../../components/icons/ListIcon'; import QualifierIcon from '../../components/icons/QualifierIcon'; import { PopupPlacement } from '../../components/ui/popups'; import { getBranchLikeQuery } from '../../helpers/branch-like'; import { ISSUE_TYPES } from '../../helpers/constants'; import { ISSUETYPE_METRIC_KEYS_MAP } from '../../helpers/issues'; import { translate } from '../../helpers/l10n'; import { formatMeasure } from '../../helpers/measures'; import { collapsedDirFromPath, fileFromPath } from '../../helpers/path'; import { omitNil } from '../../helpers/request'; import { getBaseUrl } from '../../helpers/system'; import { getBranchLikeUrl, getCodeUrl, getComponentIssuesUrl, getPathUrlAsString } from '../../helpers/urls'; import { BranchLike } from '../../types/branch-like'; import { ComponentQualifier } from '../../types/component'; import { WorkspaceContextShape } from '../workspace/context'; import MeasuresOverlay from './components/MeasuresOverlay'; interface Props { branchLike: BranchLike | undefined; componentMeasures?: T.Measure[]; openComponent: WorkspaceContextShape['openComponent']; showMeasures?: boolean; sourceViewerFile: T.SourceViewerFile; } interface State { measuresOverlay: boolean; } export default class SourceViewerHeader extends React.PureComponent { state: State = { measuresOverlay: false }; handleShowMeasuresClick = (event: React.SyntheticEvent) => { event.preventDefault(); this.setState({ measuresOverlay: true }); }; handleMeasuresOverlayClose = () => { this.setState({ measuresOverlay: false }); }; openInWorkspace = (event: React.SyntheticEvent) => { event.preventDefault(); const { key } = this.props.sourceViewerFile; this.props.openComponent({ branchLike: this.props.branchLike, key }); }; renderIssueMeasures = () => { const { branchLike, componentMeasures, sourceViewerFile } = this.props; return ( componentMeasures && componentMeasures.length > 0 && ( <>
{ISSUE_TYPES.map((type: T.IssueType) => { const params = { ...getBranchLikeQuery(branchLike), files: sourceViewerFile.path, resolved: 'false', types: type }; const measure = componentMeasures.find( m => m.metric === ISSUETYPE_METRIC_KEYS_MAP[type].metric ); return (
{translate('issue.type', type)} {formatMeasure((measure && measure.value) || 0, 'INT')}
); })} ) ); }; render() { const { showMeasures } = this.props; const { key, measures, path, project, projectName, q, subProject, subProjectName } = this.props.sourceViewerFile; const unitTestsOrLines = q === ComponentQualifier.TestFile ? 'tests' : 'lines'; const workspace = false; const rawSourcesLink = getBaseUrl() + '/api/sources/raw?' + stringify(omitNil({ key, ...getBranchLikeQuery(this.props.branchLike) })); // TODO favorite return (
{subProject !== undefined && (
{subProjectName}
)}
{collapsedDirFromPath(path)} {fileFromPath(path)}
{this.state.measuresOverlay && ( )} {showMeasures && (
{measures[unitTestsOrLines] && (
{translate(`metric.${unitTestsOrLines}.name`)} {formatMeasure(measures[unitTestsOrLines], 'SHORT_INT')}
)} {measures.coverage !== undefined && (
{translate('metric.coverage.name')} {formatMeasure(measures.coverage, 'PERCENT')}
)} {measures.duplicationDensity !== undefined && (
{translate('duplications')} {formatMeasure(measures.duplicationDensity, 'PERCENT')}
)} {this.renderIssueMeasures()}
)}
  • {translate('component_viewer.show_details')}
  • {translate('component_viewer.new_window')}
  • {!workspace && (
  • {translate('component_viewer.open_in_workspace')}
  • )}
  • {translate('component_viewer.show_raw_source')}
  • } overlayPlacement={PopupPlacement.BottomRight}>
    ); } }