3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 import * as React from 'react';
21 import DeferredSpinner from 'sonar-ui-common/components/ui/DeferredSpinner';
22 import { getDuplications } from '../../../api/components';
23 import { getIssueFlowSnippets } from '../../../api/issues';
24 import DuplicationPopup from '../../../components/SourceViewer/components/DuplicationPopup';
26 filterDuplicationBlocksByLine,
27 getDuplicationBlocksForIndex,
28 isDuplicationBlockInRemovedComponent
29 } from '../../../components/SourceViewer/helpers/duplications';
32 issuesByComponentAndLine
33 } from '../../../components/SourceViewer/helpers/indexing';
34 import { SourceViewerContext } from '../../../components/SourceViewer/SourceViewerContext';
35 import { WorkspaceContext } from '../../../components/workspace/context';
36 import { getBranchLikeQuery } from '../../../helpers/branch-like';
37 import { BranchLike } from '../../../types/branch-like';
38 import ComponentSourceSnippetViewer from './ComponentSourceSnippetViewer';
39 import { groupLocationsByComponent } from './utils';
42 branchLike: BranchLike | undefined;
43 highlightedLocationMessage?: { index: number; text: string | undefined };
46 locations: T.FlowLocation[];
47 onIssueChange: (issue: T.Issue) => void;
48 onLoaded?: () => void;
49 onLocationSelect: (index: number) => void;
50 scroll?: (element: HTMLElement) => void;
51 selectedFlowIndex: number | undefined;
55 components: T.Dict<T.SnippetsByComponent>;
56 duplicatedFiles?: T.Dict<T.DuplicatedFile>;
57 duplications?: T.Duplication[];
58 duplicationsByLine: { [line: number]: number[] };
59 issuePopup?: { issue: string; name: string };
60 linePopup?: T.LinePopup & { component: string };
64 export default class CrossComponentSourceViewerWrapper extends React.PureComponent<Props, State> {
68 duplicationsByLine: {},
74 this.fetchIssueFlowSnippets(this.props.issue.key);
77 componentWillReceiveProps(newProps: Props) {
78 if (newProps.issue.key !== this.props.issue.key) {
79 this.fetchIssueFlowSnippets(newProps.issue.key);
83 componentWillUnmount() {
87 fetchDuplications = (component: string, line: T.SourceLine) => {
90 ...getBranchLikeQuery(this.props.branchLike)
94 this.setState(state => ({
95 duplicatedFiles: r.files,
96 duplications: r.duplications,
97 duplicationsByLine: duplicationsByLine(r.duplications),
99 r.duplications.length === 1
100 ? { component, index: 0, line: line.line, name: 'duplications' }
109 fetchIssueFlowSnippets(issueKey: string) {
110 this.setState({ loading: true });
111 getIssueFlowSnippets(issueKey).then(
116 issuePopup: undefined,
117 linePopup: undefined,
120 if (this.props.onLoaded) {
121 this.props.onLoaded();
127 this.setState({ loading: false });
133 handleIssuePopupToggle = (issue: string, popupName: string, open?: boolean) => {
134 this.setState((state: State) => {
136 state.issuePopup && state.issuePopup.name === popupName && state.issuePopup.issue === issue;
137 if (open !== false && !samePopup) {
138 return { issuePopup: { issue, name: popupName } };
139 } else if (open !== true && samePopup) {
140 return { issuePopup: undefined };
146 handleLinePopupToggle = ({
152 }: T.LinePopup & { component: string }) => {
153 this.setState((state: State) => {
155 state.linePopup !== undefined &&
156 state.linePopup.line === line &&
157 state.linePopup.name === name &&
158 state.linePopup.component === component &&
159 state.linePopup.index === index;
160 if (open !== false && !samePopup) {
161 return { linePopup: { component, index, line, name } };
162 } else if (open !== true && samePopup) {
163 return { linePopup: undefined };
169 handleCloseLinePopup = () => {
170 this.setState({ linePopup: undefined });
173 renderDuplicationPopup = (component: T.SourceViewerFile, index: number, line: number) => {
174 const { duplicatedFiles, duplications } = this.state;
176 if (!component || !duplicatedFiles) {
180 const blocks = getDuplicationBlocksForIndex(duplications, index);
183 <WorkspaceContext.Consumer>
184 {({ openComponent }) => (
186 blocks={filterDuplicationBlocksByLine(blocks, line)}
187 branchLike={this.props.branchLike}
188 duplicatedFiles={duplicatedFiles}
189 inRemovedComponent={isDuplicationBlockInRemovedComponent(blocks)}
190 onClose={this.handleCloseLinePopup}
191 openComponent={openComponent}
192 sourceViewerFile={component}
195 </WorkspaceContext.Consumer>
200 const { loading } = this.state;
210 const { components, duplications, duplicationsByLine, linePopup } = this.state;
211 const issuesByComponent = issuesByComponentAndLine(this.props.issues);
212 const locationsByComponent = groupLocationsByComponent(this.props.locations, components);
216 {locationsByComponent.map((snippetGroup, i) => {
217 let componentProps = {};
218 if (linePopup && snippetGroup.component.key === linePopup.component) {
222 linePopup: { index: linePopup.index, line: linePopup.line, name: linePopup.name }
226 <SourceViewerContext.Provider
227 key={`${this.props.issue.key}-${this.props.selectedFlowIndex || 0}-${i}`}
228 value={{ branchLike: this.props.branchLike, file: snippetGroup.component }}>
229 <ComponentSourceSnippetViewer
230 branchLike={this.props.branchLike}
231 highlightedLocationMessage={this.props.highlightedLocationMessage}
232 issue={this.props.issue}
233 issuePopup={this.state.issuePopup}
234 issuesByLine={issuesByComponent[snippetGroup.component.key] || {}}
235 last={i === locationsByComponent.length - 1}
236 loadDuplications={this.fetchDuplications}
237 locations={snippetGroup.locations || []}
238 onIssueChange={this.props.onIssueChange}
239 onIssuePopupToggle={this.handleIssuePopupToggle}
240 onLinePopupToggle={this.handleLinePopupToggle}
241 onLocationSelect={this.props.onLocationSelect}
242 renderDuplicationPopup={this.renderDuplicationPopup}
243 scroll={this.props.scroll}
244 snippetGroup={snippetGroup}
247 </SourceViewerContext.Provider>