aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/component-measures/components/MeasureTree.js
blob: 8e4ceb1654275dfac1720bed4168a8e142f22153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact 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 React from 'react';

import Spinner from './Spinner';
import ComponentsList from './ComponentsList';
import NoResults from './NoResults';
import SourceViewer from '../../code/components/SourceViewer';
import { getSingleMeasureValue } from '../utils';
import { getChildren } from '../../../api/components';

export default class MeasureTree extends React.Component {
  state = {
    components: [],
    breadcrumbs: [],
    selected: null,
    fetching: true
  };

  componentDidMount () {
    this.mounted = true;
    this.fetchComponents(this.context.component);
  }

  componentDidUpdate (nextProps, nextState, nextContext) {
    if ((nextProps.metric !== this.props.metric) ||
        (nextContext.component !== this.context.component)) {
      this.fetchComponents(nextContext.component);
    }
  }

  componentWillUnmount () {
    this.mounted = false;
  }

  fetchComponents (baseComponent) {
    const { metric } = this.props;
    const asc = metric.direction === 1;

    const options = {
      s: 'metric,name',
      metricSort: metric.key,
      asc
    };

    this.setState({ fetching: true });

    getChildren(baseComponent.key, [metric.key], options).then(children => {
      if (this.mounted) {
        const componentsWithMappedMeasure = children
            .map(component => {
              return {
                ...component,
                value: getSingleMeasureValue(component.measures)
              };
            })
            .filter(component => component.value != null);

        const indexInBreadcrumbs = this.state.breadcrumbs.findIndex(component => component === baseComponent);
        const breadcrumbs = indexInBreadcrumbs !== -1 ?
            this.state.breadcrumbs.slice(0, indexInBreadcrumbs + 1) :
            [...this.state.breadcrumbs, baseComponent];

        this.setState({
          baseComponent,
          breadcrumbs,
          components: componentsWithMappedMeasure,
          selected: null,
          fetching: false
        });
      }
    });
  }

  handleFileClick (component) {
    if (component.qualifier === 'FIL' || component.qualifier === 'UTS') {
      this.handleFileOpen(component);
    } else {
      this.fetchComponents(component);
    }
  }

  handleFileOpen (selected) {
    this.setState({ selected });
  }

  render () {
    const { metric } = this.props;
    const { components, selected, breadcrumbs, fetching } = this.state;
    const parent = breadcrumbs.length > 1 ? breadcrumbs[breadcrumbs.length - 2] : null;

    if (fetching) {
      return <Spinner/>;
    }

    if (!components.length) {
      return <NoResults/>;
    }

    return (
        <div className="measure-details-tree">
          <div className="measure-details-components">
            <ComponentsList
                components={components}
                selected={selected}
                parent={parent}
                metric={metric}
                onClick={this.handleFileClick.bind(this)}/>
          </div>

          {selected && (
              <div className="measure-details-viewer">
                <SourceViewer component={selected}/>
              </div>
          )}
        </div>
    );
  }
}

MeasureTree.contextTypes = {
  component: React.PropTypes.object
};