aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/overview/branches/BranchOverviewRenderer.tsx
blob: 82dd9f0f985d88761e565a7c3434e074f55bd0bf (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * 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 { CardSeparator, CenteredLayout, PageContentFontWrapper } from 'design-system';
import * as React from 'react';
import { useState } from 'react';
import A11ySkipTarget from '~sonar-aligned/components/a11y/A11ySkipTarget';
import { useLocation, useRouter } from '~sonar-aligned/components/hoc/withRouter';
import { isPortfolioLike } from '~sonar-aligned/helpers/component';
import { ComponentQualifier } from '~sonar-aligned/types/component';
import { CurrentUserContext } from '../../../app/components/current-user/CurrentUserContext';
import AnalysisMissingInfoMessage from '../../../components/shared/AnalysisMissingInfoMessage';
import { parseDate } from '../../../helpers/dates';
import { translate } from '../../../helpers/l10n';
import { areCCTMeasuresComputed, isDiffMetric } from '../../../helpers/measures';
import { CodeScope } from '../../../helpers/urls';
import { useDismissNoticeMutation } from '../../../queries/users';
import { ApplicationPeriod } from '../../../types/application';
import { Branch } from '../../../types/branch-like';
import { Analysis, GraphType, MeasureHistory } from '../../../types/project-activity';
import { QualityGateStatus } from '../../../types/quality-gates';
import { Component, MeasureEnhanced, Metric, Period, QualityGate } from '../../../types/types';
import { NoticeType } from '../../../types/users';
import { AnalysisStatus } from '../components/AnalysisStatus';
import LastAnalysisLabel from '../components/LastAnalysisLabel';
import { Status } from '../utils';
import ActivityPanel from './ActivityPanel';
import BranchMetaTopBar from './BranchMetaTopBar';
import CaycPromotionGuide from './CaycPromotionGuide';
import FirstAnalysisNextStepsNotif from './FirstAnalysisNextStepsNotif';
import MeasuresPanelNoNewCode from './MeasuresPanelNoNewCode';
import NewCodeMeasuresPanel from './NewCodeMeasuresPanel';
import NoCodeWarning from './NoCodeWarning';
import OverallCodeMeasuresPanel from './OverallCodeMeasuresPanel';
import PromotedSection from './PromotedSection';
import QGStatus from './QualityGateStatus';
import ReplayTourGuide from './ReplayTour';
import TabsPanel from './TabsPanel';

export interface BranchOverviewRendererProps {
  analyses?: Analysis[];
  appLeak?: ApplicationPeriod;
  branch?: Branch;
  branchesEnabled?: boolean;
  component: Component;
  detectedCIOnLastAnalysis?: boolean;
  graph?: GraphType;
  loadingHistory?: boolean;
  loadingStatus?: boolean;
  measures?: MeasureEnhanced[];
  measuresHistory?: MeasureHistory[];
  metrics?: Metric[];
  onGraphChange: (graph: GraphType) => void;
  period?: Period;
  projectIsEmpty?: boolean;
  qgStatuses?: QualityGateStatus[];
  qualityGate?: QualityGate;
}

export default function BranchOverviewRenderer(props: BranchOverviewRendererProps) {
  const {
    analyses,
    appLeak,
    branch,
    branchesEnabled,
    component,
    detectedCIOnLastAnalysis,
    graph,
    loadingHistory,
    loadingStatus,
    measures = [],
    measuresHistory = [],
    metrics = [],
    onGraphChange,
    period,
    projectIsEmpty,
    qgStatuses,
    qualityGate,
  } = props;

  const { query } = useLocation();
  const router = useRouter();

  const { currentUser } = React.useContext(CurrentUserContext);

  const { mutateAsync: dismissNotice } = useDismissNoticeMutation();

  const [startTour, setStartTour] = useState(false);
  const [tourCompleted, setTourCompleted] = useState(false);
  const [showReplay, setShowReplay] = useState(false);
  const [dismissedTour, setDismissedTour] = useState(
    currentUser.isLoggedIn &&
      !!currentUser.dismissedNotices[NoticeType.ONBOARDING_CAYC_BRANCH_SUMMARY_GUIDE],
  );

  const tab = query.codeScope === CodeScope.Overall ? CodeScope.Overall : CodeScope.New;
  const leakPeriod = component.qualifier === ComponentQualifier.Application ? appLeak : period;
  const isNewCodeTab = tab === CodeScope.New;
  const hasNewCodeMeasures = measures.some((m) => isDiffMetric(m.metric.key));

  // Check if any potentially missing uncomputed measure is not present
  // const isMissingMeasures =
  //   !areCCTMeasuresComputed(measures) || !areSoftwareQualityRatingsComputed(measures);
  const isMissingMeasures = !areCCTMeasuresComputed(measures);

  const selectTab = (tab: CodeScope) => {
    router.replace({ query: { ...query, codeScope: tab } });
  };

  React.useEffect(() => {
    // Open Overall tab by default if there are no new measures.
    if (loadingStatus === false && !hasNewCodeMeasures && isNewCodeTab) {
      selectTab(CodeScope.Overall);
    }
    // In this case, we explicitly do NOT want to mark tab as a dependency, as
    // it would prevent the user from selecting it, even if it's empty.
    /* eslint-disable-next-line react-hooks/exhaustive-deps */
  }, [loadingStatus, hasNewCodeMeasures]);

  const analysisMissingInfo = isMissingMeasures && (
    <AnalysisMissingInfoMessage
      qualifier={component.qualifier}
      hide={isPortfolioLike(component.qualifier)}
      className="sw-mb-8"
    />
  );

  const dismissPromotedSection = () => {
    dismissNotice(NoticeType.ONBOARDING_CAYC_BRANCH_SUMMARY_GUIDE);

    setDismissedTour(true);
    setShowReplay(true);
  };

  const closeTour = (action: string) => {
    setStartTour(false);
    if (action === 'skip' && !dismissedTour) {
      dismissPromotedSection();
    }

    if (action === 'close' && !dismissedTour) {
      dismissPromotedSection();
      setTourCompleted(true);
    }
  };

  const startTourGuide = () => {
    if (!isNewCodeTab) {
      selectTab(CodeScope.New);
    }
    setShowReplay(false);
    setStartTour(true);
  };

  const qgStatus = qgStatuses?.map((s) => s.status).includes('ERROR') ? Status.ERROR : Status.OK;

  return (
    <>
      <FirstAnalysisNextStepsNotif
        component={component}
        branchesEnabled={branchesEnabled}
        detectedCIOnLastAnalysis={detectedCIOnLastAnalysis}
      />
      <CenteredLayout>
        <PageContentFontWrapper>
          <CaycPromotionGuide closeTour={closeTour} run={startTour} />
          {showReplay && (
            <ReplayTourGuide
              closeTour={() => setShowReplay(false)}
              run={showReplay}
              tourCompleted={tourCompleted}
            />
          )}
          <div className="overview sw-my-6 sw-body-sm">
            <A11ySkipTarget anchor="overview_main" />

            {projectIsEmpty ? (
              <NoCodeWarning branchLike={branch} component={component} measures={measures} />
            ) : (
              <div>
                {branch && (
                  <>
                    <BranchMetaTopBar
                      branch={branch}
                      component={component}
                      measures={measures}
                      showTakeTheTourButton={
                        dismissedTour && currentUser.isLoggedIn && hasNewCodeMeasures
                      }
                      startTour={startTourGuide}
                    />

                    <CardSeparator />

                    {currentUser.isLoggedIn && hasNewCodeMeasures && (
                      <PromotedSection
                        content={translate('overview.promoted_section.content')}
                        dismissed={dismissedTour ?? false}
                        onDismiss={dismissPromotedSection}
                        onPrimaryButtonClick={startTourGuide}
                        primaryButtonLabel={translate('overview.promoted_section.button_primary')}
                        secondaryButtonLabel={translate(
                          'overview.promoted_section.button_secondary',
                        )}
                        title={translate('overview.promoted_section.title')}
                      />
                    )}
                  </>
                )}
                <div
                  data-testid="overview__quality-gate-panel"
                  className="sw-flex sw-justify-between sw-items-start sw-my-6"
                >
                  <QGStatus status={qgStatus} titleSize="extra-large" />
                  <LastAnalysisLabel analysisDate={branch?.analysisDate} />
                </div>
                <AnalysisStatus component={component} />
                <div className="sw-flex sw-flex-col sw-mt-6">
                  <TabsPanel
                    analyses={analyses}
                    component={component}
                    loading={loadingStatus}
                    qgStatuses={qgStatuses}
                    isNewCode={isNewCodeTab}
                    onTabSelect={selectTab}
                  >
                    {isNewCodeTab && (
                      <>
                        {hasNewCodeMeasures ? (
                          <NewCodeMeasuresPanel
                            qgStatuses={qgStatuses}
                            branch={branch}
                            component={component}
                            measures={measures}
                            appLeak={appLeak}
                            period={period}
                            loading={loadingStatus}
                            qualityGate={qualityGate}
                          />
                        ) : (
                          <MeasuresPanelNoNewCode
                            branch={branch}
                            component={component}
                            period={period}
                          />
                        )}
                      </>
                    )}

                    {!isNewCodeTab && (
                      <>
                        {analysisMissingInfo}
                        <OverallCodeMeasuresPanel
                          branch={branch}
                          qgStatuses={qgStatuses}
                          component={component}
                          measures={measures}
                          loading={loadingStatus}
                          qualityGate={qualityGate}
                        />
                      </>
                    )}
                  </TabsPanel>

                  <ActivityPanel
                    analyses={analyses}
                    branchLike={branch}
                    component={component}
                    graph={graph}
                    leakPeriodDate={leakPeriod && parseDate(leakPeriod.date)}
                    loading={loadingHistory}
                    measuresHistory={measuresHistory}
                    metrics={metrics}
                    onGraphChange={onGraphChange}
                  />
                </div>
              </div>
            )}
          </div>
        </PageContentFontWrapper>
      </CenteredLayout>
    </>
  );
}