/* * SonarQube * Copyright (C) 2009-2017 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. */ // @flow import React from 'react'; import Breadcrumbs from './Breadcrumbs'; import BubbleChart from '../drilldown/BubbleChart'; import LeakPeriodLegend from './LeakPeriodLegend'; import MeasureFavoriteContainer from './MeasureFavoriteContainer'; import PageActions from './PageActions'; import SourceViewer from '../../../components/SourceViewer/SourceViewer'; import { getComponentLeaves } from '../../../api/components'; import { enhanceComponent, getBubbleMetrics, isFileType } from '../utils'; import type { Component, ComponentEnhanced, Paging, Period } from '../types'; import type { Metric } from '../../../store/metrics/actions'; type Props = {| className?: string, component: Component, currentUser: { isLoggedIn: boolean }, domain: string, leakPeriod: Period, loading: boolean, metrics: { [string]: Metric }, rootComponent: Component, updateLoading: ({ [string]: boolean }) => void, updateSelected: string => void |}; type State = { components: Array, paging?: Paging }; const BUBBLES_LIMIT = 500; export default class MeasureOverview extends React.PureComponent { mounted: boolean; props: Props; state: State = { components: [], paging: null }; componentDidMount() { this.mounted = true; this.fetchComponents(this.props); } componentWillReceiveProps(nextProps: Props) { if ( nextProps.component !== this.props.component || nextProps.metrics !== this.props.metrics || nextProps.domain !== this.props.domain ) { this.fetchComponents(nextProps); } } componentWillUnmount() { this.mounted = false; } fetchComponents = (props: Props) => { const { component, domain, metrics } = props; if (isFileType(component)) { return this.setState({ components: [], paging: null }); } const { xMetric, yMetric, sizeMetric, colorsMetric } = getBubbleMetrics(domain, metrics); const metricsKey = [xMetric.key, yMetric.key, sizeMetric.key]; if (colorsMetric) { metricsKey.push(colorsMetric.map(metric => metric.key)); } const options = { s: 'metric', metricSort: sizeMetric.key, asc: false, ps: BUBBLES_LIMIT }; this.props.updateLoading({ bubbles: true }); getComponentLeaves(component.key, metricsKey, options).then( r => { if (domain === this.props.domain) { if (this.mounted) { this.setState({ components: r.components.map(component => enhanceComponent(component, null, metrics)), paging: r.paging }); } this.props.updateLoading({ bubbles: false }); } }, () => this.props.updateLoading({ bubbles: false }) ); }; renderContent() { const { component } = this.props; if (isFileType(component)) { return (
); } return ( ); } render() { const { component, currentUser, leakPeriod, rootComponent } = this.props; const isLoggedIn = currentUser && currentUser.isLoggedIn; const isFile = isFileType(component); return (
{component.key !== rootComponent.key && isLoggedIn && }
{leakPeriod != null && }
{!this.props.loading && this.renderContent()}
); } }