aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/component-measures/sidebar/Sidebar.tsx
blob: 7ffcfb03c8d76a2775305638bf164a4fa0ffaee0 (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
/*
 * SonarQube
 * Copyright (C) 2009-2023 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 A11ySkipTarget from '../../../components/a11y/A11ySkipTarget';
import { translate } from '../../../helpers/l10n';
import { Dict, MeasureEnhanced } from '../../../types/types';
import { groupByDomains, KNOWN_DOMAINS, PROJECT_OVERVEW, Query } from '../utils';
import DomainFacet from './DomainFacet';
import ProjectOverviewFacet from './ProjectOverviewFacet';

interface Props {
  measures: MeasureEnhanced[];
  selectedMetric: string;
  showFullMeasures: boolean;
  updateQuery: (query: Partial<Query>) => void;
}

interface State {
  openFacets: Dict<boolean>;
}

export default class Sidebar extends React.PureComponent<Props, State> {
  static getDerivedStateFromProps(props: Props, state: State) {
    return { openFacets: getOpenFacets(state.openFacets, props) };
  }

  state: State = {
    openFacets: {},
  };

  toggleFacet = (name: string) => {
    this.setState(({ openFacets }) => ({
      openFacets: { ...openFacets, [name]: !openFacets[name] },
    }));
  };

  changeMetric = (metric: string) => {
    this.props.updateQuery({ metric });
  };

  render() {
    const { showFullMeasures } = this.props;
    return (
      <nav aria-label={translate('secondary')}>
        <A11ySkipTarget
          anchor="measures_filters"
          label={translate('component_measures.skip_to_filters')}
          weight={10}
        />
        <ProjectOverviewFacet
          onChange={this.changeMetric}
          selected={this.props.selectedMetric}
          value={PROJECT_OVERVEW}
        />
        {groupByDomains(this.props.measures).map((domain) => (
          <DomainFacet
            domain={domain}
            key={domain.name}
            onChange={this.changeMetric}
            onToggle={this.toggleFacet}
            open={this.state.openFacets[domain.name] === true}
            selected={this.props.selectedMetric}
            showFullMeasures={showFullMeasures}
          />
        ))}
      </nav>
    );
  }
}

function getOpenFacets(openFacets: Dict<boolean>, { measures, selectedMetric }: Props) {
  const newOpenFacets = { ...openFacets };
  const measure = measures.find((measure) => measure.metric.key === selectedMetric);
  if (measure && measure.metric && measure.metric.domain) {
    newOpenFacets[measure.metric.domain] = true;
  } else if (KNOWN_DOMAINS.includes(selectedMetric)) {
    newOpenFacets[selectedMetric] = true;
  }
  return newOpenFacets;
}