From 0a5f275bb98ffa26e432affa3f525e2c991f9450 Mon Sep 17 00:00:00 2001 From: David Cho-Lerat Date: Tue, 5 Dec 2023 14:55:58 +0100 Subject: [PATCH] SONAR-21056 Code page: don't show data from default branch on refresh --- .../main/js/apps/code/__tests__/Code-it.ts | 7 +- .../main/js/apps/code/components/CodeApp.tsx | 97 +++++++++++-------- 2 files changed, 60 insertions(+), 44 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/code/__tests__/Code-it.ts b/server/sonar-web/src/main/js/apps/code/__tests__/Code-it.ts index b432a72abf0..af91e924584 100644 --- a/server/sonar-web/src/main/js/apps/code/__tests__/Code-it.ts +++ b/server/sonar-web/src/main/js/apps/code/__tests__/Code-it.ts @@ -22,6 +22,7 @@ import userEvent from '@testing-library/user-event'; import { UserEvent } from '@testing-library/user-event/dist/types/setup/setup'; import { keyBy, times } from 'lodash'; import { act } from 'react-dom/test-utils'; +import BranchesServiceMock from '../../../api/mocks/BranchesServiceMock'; import ComponentsServiceMock from '../../../api/mocks/ComponentsServiceMock'; import IssuesServiceMock from '../../../api/mocks/IssuesServiceMock'; import { isDiffMetric } from '../../../helpers/measures'; @@ -47,8 +48,9 @@ jest.mock('../../../components/SourceViewer/helpers/lines', () => { const DEFAULT_LINES_LOADED = 19; const originalScrollTo = window.scrollTo; -const issuesHandler = new IssuesServiceMock(); +const branchesHandler = new BranchesServiceMock(); const componentsHandler = new ComponentsServiceMock(); +const issuesHandler = new IssuesServiceMock(); beforeAll(() => { Object.defineProperty(window, 'scrollTo', { @@ -67,8 +69,9 @@ afterAll(() => { }); beforeEach(() => { - issuesHandler.reset(); + branchesHandler.reset(); componentsHandler.reset(); + issuesHandler.reset(); }); it('should allow navigating through the tree', async () => { diff --git a/server/sonar-web/src/main/js/apps/code/components/CodeApp.tsx b/server/sonar-web/src/main/js/apps/code/components/CodeApp.tsx index f86495158a5..0e5cdc76e7b 100644 --- a/server/sonar-web/src/main/js/apps/code/components/CodeApp.tsx +++ b/server/sonar-web/src/main/js/apps/code/components/CodeApp.tsx @@ -22,22 +22,20 @@ import withComponentContext from '../../../app/components/componentContext/withC import withMetricsContext from '../../../app/components/metrics/withMetricsContext'; import { Location, Router, withRouter } from '../../../components/hoc/withRouter'; import { CodeScope, getCodeUrl, getProjectUrl } from '../../../helpers/urls'; -import { withBranchLikes } from '../../../queries/branch'; +import { useBranchesQuery } from '../../../queries/branch'; import { BranchLike } from '../../../types/branch-like'; -import { ComponentQualifier } from '../../../types/component'; +import { ComponentQualifier, isPortfolioLike } from '../../../types/component'; import { Breadcrumb, Component, ComponentMeasure, Dict, Metric } from '../../../types/types'; import { addComponent, addComponentBreadcrumbs, clearBucket } from '../bucket'; import '../code.css'; import { loadMoreChildren, retrieveComponent, retrieveComponentChildren } from '../utils'; import CodeAppRenderer from './CodeAppRenderer'; -interface Props { - branchLike?: BranchLike; - branchLikes: BranchLike[]; +interface Props extends WithBranchLikesProps { component: Component; location: Location; - router: Router; metrics: Dict; + router: Router; } interface State { @@ -46,11 +44,11 @@ interface State { components?: ComponentMeasure[]; highlighted?: ComponentMeasure; loading: boolean; + newCodeSelected: boolean; page: number; searchResults?: ComponentMeasure[]; sourceViewer?: ComponentMeasure; total: number; - newCodeSelected: boolean; } class CodeApp extends React.Component { @@ -62,9 +60,9 @@ class CodeApp extends React.Component { this.state = { breadcrumbs: [], loading: true, + newCodeSelected: true, page: 0, total: 0, - newCodeSelected: true, }; } @@ -92,41 +90,37 @@ class CodeApp extends React.Component { this, this.props.branchLike, ).then((r) => { - if (this.mounted) { - if ( - [ComponentQualifier.File, ComponentQualifier.TestFile].includes( - r.component.qualifier as ComponentQualifier, - ) - ) { - this.setState({ - breadcrumbs: r.breadcrumbs, - components: r.components, - loading: false, - page: 0, - searchResults: undefined, - sourceViewer: r.component, - total: 0, - }); - } else { - this.setState({ - baseComponent: r.component, - breadcrumbs: r.breadcrumbs, - components: r.components, - loading: false, - page: r.page, - searchResults: undefined, - sourceViewer: undefined, - total: r.total, - }); - } + if ( + [ComponentQualifier.File, ComponentQualifier.TestFile].includes( + r.component.qualifier as ComponentQualifier, + ) + ) { + this.setState({ + breadcrumbs: r.breadcrumbs, + components: r.components, + loading: false, + page: 0, + searchResults: undefined, + sourceViewer: r.component, + total: 0, + }); + } else { + this.setState({ + baseComponent: r.component, + breadcrumbs: r.breadcrumbs, + components: r.components, + loading: false, + page: r.page, + searchResults: undefined, + sourceViewer: undefined, + total: r.total, + }); } }, this.stopLoading); }; stopLoading = () => { - if (this.mounted) { - this.setState({ loading: false }); - } + this.setState({ loading: false }); }; handleComponentChange = () => { @@ -138,9 +132,7 @@ class CodeApp extends React.Component { this.setState({ loading: true }); retrieveComponentChildren(component.key, component.qualifier, this, branchLike).then(() => { addComponent(component); - if (this.mounted) { - this.handleUpdate(); - } + this.handleUpdate(); }, this.stopLoading); }; @@ -156,7 +148,7 @@ class CodeApp extends React.Component { this, this.props.branchLike, ).then((r) => { - if (this.mounted && r.components.length) { + if (r.components.length) { this.setState({ components: [...components, ...r.components], page: r.page, @@ -233,4 +225,25 @@ class CodeApp extends React.Component { } } +interface WithBranchLikesProps { + branchLikes?: BranchLike[]; + branchLike?: BranchLike; +} + +function withBranchLikes

( + WrappedComponent: React.ComponentType>, +): React.ComponentType>> { + return function WithBranchLike(p: P) { + const { data, isFetching } = useBranchesQuery(p.component); + + return isPortfolioLike(p.component?.qualifier) || !isFetching ? ( + + ) : null; + }; +} + export default withRouter(withComponentContext(withMetricsContext(withBranchLikes(CodeApp)))); -- 2.39.5