aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/security-hotspots/components/HotspotSidebarHeader.tsx
blob: 743b3ab14b136a714a6efcd4ac7a54d4ad26d8ab (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * SonarQube
 * Copyright (C) 2009-2024 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 {
  CoverageIndicator,
  DiscreetInteractiveIcon,
  Dropdown,
  FilterIcon,
  HelperHintIcon,
  ItemCheckbox,
  ItemDangerButton,
  ItemDivider,
  ItemHeader,
  PopupZLevel,
  Spinner,
} from 'design-system';
import * as React from 'react';
import HelpTooltip from '~sonar-aligned/components/controls/HelpTooltip';
import { isBranch } from '~sonar-aligned/helpers/branch-like';
import withComponentContext from '../../../app/components/componentContext/withComponentContext';
import withCurrentUserContext from '../../../app/components/current-user/withCurrentUserContext';
import Tooltip from '../../../components/controls/Tooltip';
import Measure from '../../../components/measure/Measure';
import { PopupPlacement } from '../../../components/ui/popups';
import { translate } from '../../../helpers/l10n';
import { BranchLike } from '../../../types/branch-like';
import { ComponentContextShape } from '../../../types/component';
import { MetricKey, MetricType } from '../../../types/metrics';
import { HotspotFilters } from '../../../types/security-hotspots';
import { CurrentUser, isLoggedIn } from '../../../types/users';
import { HotspotDisabledFilterTooltip } from './HotspotDisabledFilterTooltip';

export interface SecurityHotspotsAppRendererProps extends ComponentContextShape {
  branchLike?: BranchLike;
  currentUser: CurrentUser;
  filters: HotspotFilters;
  hotspotsReviewedMeasure?: string;
  isStaticListOfHotspots: boolean;
  loadingMeasure: boolean;
  onChangeFilters: (filters: Partial<HotspotFilters>) => void;
}

function HotspotSidebarHeader(props: SecurityHotspotsAppRendererProps) {
  const {
    branchLike,
    component,
    currentUser,
    filters,
    hotspotsReviewedMeasure,
    isStaticListOfHotspots,
    loadingMeasure,
  } = props;

  const userLoggedIn = isLoggedIn(currentUser);

  const filtersCount =
    Number(filters.assignedToMe) + Number(isBranch(branchLike) && filters.inNewCodePeriod);

  const isFiltered = Boolean(filtersCount);

  return (
    <div className="sw-flex sw-h-6 sw-items-center sw-px-4 sw-py-4">
      <Spinner loading={loadingMeasure}>
        {hotspotsReviewedMeasure !== undefined && (
          <CoverageIndicator value={hotspotsReviewedMeasure} />
        )}

        <Measure
          className="it__hs-review-percentage sw-body-sm-highlight sw-ml-2"
          metricKey={
            isBranch(branchLike) && !filters.inNewCodePeriod
              ? MetricKey.security_hotspots_reviewed
              : MetricKey.new_security_hotspots_reviewed
          }
          metricType={MetricType.Percent}
          value={hotspotsReviewedMeasure}
        />
      </Spinner>

      <span className="sw-body-sm sw-ml-1">
        {translate('metric.security_hotspots_reviewed.name')}
      </span>

      <HelpTooltip className="sw-ml-1" overlay={translate('hotspots.reviewed.tooltip')}>
        <HelperHintIcon aria-label="help-tooltip" />
      </HelpTooltip>

      {!isStaticListOfHotspots && (isBranch(branchLike) || userLoggedIn || isFiltered) && (
        <div className="sw-flex sw-flex-grow sw-justify-end">
          <Dropdown
            allowResizing
            closeOnClick={false}
            id="filter-hotspots-menu"
            zLevel={PopupZLevel.Global}
            overlay={
              <>
                <ItemHeader>{translate('hotspot.filters.title')}</ItemHeader>

                {isBranch(branchLike) && (
                  <ItemCheckbox
                    checked={Boolean(filters.inNewCodePeriod)}
                    onCheck={(inNewCodePeriod: boolean) =>
                      props.onChangeFilters({ inNewCodePeriod })
                    }
                  >
                    <span className="sw-mx-2">
                      {translate('hotspot.filters.period.since_leak_period')}
                    </span>
                  </ItemCheckbox>
                )}

                {userLoggedIn && (
                  <Tooltip
                    classNameSpace={component?.needIssueSync ? 'tooltip' : 'sw-hidden'}
                    overlay={<HotspotDisabledFilterTooltip />}
                    placement="right"
                  >
                    <ItemCheckbox
                      checked={Boolean(filters.assignedToMe)}
                      disabled={component?.needIssueSync}
                      onCheck={(assignedToMe: boolean) => props.onChangeFilters({ assignedToMe })}
                    >
                      <span className="sw-mx-2">
                        {translate('hotspot.filters.assignee.assigned_to_me')}
                      </span>
                    </ItemCheckbox>
                  </Tooltip>
                )}

                {isFiltered && <ItemDivider />}

                {isFiltered && (
                  <ItemDangerButton
                    onClick={() =>
                      props.onChangeFilters({
                        assignedToMe: false,
                        inNewCodePeriod: false,
                      })
                    }
                  >
                    {translate('hotspot.filters.clear')}
                  </ItemDangerButton>
                )}
              </>
            }
            placement={PopupPlacement.BottomRight}
          >
            <DiscreetInteractiveIcon
              Icon={FilterIcon}
              aria-label={translate('hotspot.filters.title')}
            >
              {isFiltered ? filtersCount : null}
            </DiscreetInteractiveIcon>
          </Dropdown>
        </div>
      )}
    </div>
  );
}

export default withComponentContext(withCurrentUserContext(HotspotSidebarHeader));