/* * 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 { sortBy, uniq, without } from 'lodash'; import FacetBox from './components/FacetBox'; import FacetHeader from './components/FacetHeader'; import FacetItem from './components/FacetItem'; import FacetItemsList from './components/FacetItemsList'; import FacetFooter from './components/FacetFooter'; import type { ReferencedComponent, Component } from '../utils'; import Organization from '../../../components/shared/Organization'; import QualifierIcon from '../../../components/shared/QualifierIcon'; import { searchComponents, getTree } from '../../../api/components'; import { translate } from '../../../helpers/l10n'; type Props = {| component?: Component, facetMode: string, onChange: (changes: { [string]: Array }) => void, onToggle: (property: string) => void, open: boolean, stats?: { [string]: number }, referencedComponents: { [string]: ReferencedComponent }, projects: Array |}; export default class ProjectFacet extends React.PureComponent { props: Props; static defaultProps = { open: true }; property = 'projects'; handleItemClick = (itemValue: string) => { const { projects } = this.props; const newValue = sortBy( projects.includes(itemValue) ? without(projects, itemValue) : [...projects, itemValue] ); this.props.onChange({ [this.property]: newValue }); }; handleHeaderClick = () => { this.props.onToggle(this.property); }; handleSearch = (query: string) => { const { component } = this.props; return component != null && ['VW', 'SVW'].includes(component.qualifier) ? getTree(component.key, { ps: 50, q: query, qualifiers: 'TRK' }).then(response => response.components.map(component => ({ label: component.name, organization: component.organization, value: component.refId })) ) : searchComponents({ ps: 50, q: query, qualifiers: 'TRK' }).then(response => response.components.map(component => ({ label: component.name, organization: component.organization, value: component.id })) ); }; handleSelect = (rule: string) => { const { projects } = this.props; this.props.onChange({ [this.property]: uniq([...projects, rule]) }); }; getStat(project: string): ?number { const { stats } = this.props; return stats ? stats[project] : null; } renderName(project: string): React.Element<*> | string { const { referencedComponents } = this.props; return referencedComponents[project] ? {referencedComponents[project].name} : {project} ; } renderOption = (option: { label: string, organization: string }) => { return ( {option.label} ); }; render() { const { stats } = this.props; if (!stats) { return null; } const projects = sortBy(Object.keys(stats), key => -stats[key]); return ( 0} name={translate('issues.facet', this.property)} onClick={this.handleHeaderClick} open={this.props.open} /> {this.props.open && {projects.map(project => ( ))} } {this.props.open && } ); } }