3 * Copyright (C) 2009-2019 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 ComponentSourceSnippetViewer from './ComponentSourceSnippetViewer';
22 import { groupLocationsByComponent } from './utils';
23 import DeferredSpinner from '../../../components/common/DeferredSpinner';
24 import DuplicationPopup from '../../../components/SourceViewer/components/DuplicationPopup';
25 import { SourceViewerContext } from '../../../components/SourceViewer/SourceViewerContext';
26 import { WorkspaceContext } from '../../../components/workspace/context';
27 import { getIssueFlowSnippets } from '../../../api/issues';
29 filterDuplicationBlocksByLine,
30 isDuplicationBlockInRemovedComponent,
31 getDuplicationBlocksForIndex
32 } from '../../../components/SourceViewer/helpers/duplications';
35 issuesByComponentAndLine
36 } from '../../../components/SourceViewer/helpers/indexing';
37 import { getDuplications } from '../../../api/components';
38 import { getBranchLikeQuery } from '../../../helpers/branches';
41 branchLike: T.Branch | T.PullRequest | undefined;
42 highlightedLocationMessage?: { index: number; text: string | undefined };
45 locations: T.FlowLocation[];
46 onIssueChange: (issue: T.Issue) => void;
47 onLoaded?: () => void;
48 onLocationSelect: (index: number) => void;
49 scroll?: (element: HTMLElement) => void;
50 selectedFlowIndex: number | undefined;
54 components: T.Dict<T.SnippetsByComponent>;
55 duplicatedFiles?: T.Dict<T.DuplicatedFile>;
56 duplications?: T.Duplication[];
57 duplicationsByLine: { [line: number]: number[] };
58 issuePopup?: { issue: string; name: string };
59 linePopup?: T.LinePopup & { component: string };
63 export default class CrossComponentSourceViewerWrapper extends React.PureComponent<Props, State> {
67 duplicationsByLine: {},
73 this.fetchIssueFlowSnippets(this.props.issue.key);
76 componentWillReceiveProps(newProps: Props) {
77 if (newProps.issue.key !== this.props.issue.key) {
78 this.fetchIssueFlowSnippets(newProps.issue.key);
82 componentWillUnmount() {
86 fetchDuplications = (component: string, line: T.SourceLine) => {
89 ...getBranchLikeQuery(this.props.branchLike)
93 this.setState(state => ({
94 duplicatedFiles: r.files,
95 duplications: r.duplications,
96 duplicationsByLine: duplicationsByLine(r.duplications),
98 r.duplications.length === 1
99 ? { component, index: 0, line: line.line, name: 'duplications' }
108 fetchIssueFlowSnippets(issueKey: string) {
109 this.setState({ loading: true });
110 getIssueFlowSnippets(issueKey).then(
115 issuePopup: undefined,
116 linePopup: undefined,
119 if (this.props.onLoaded) {
120 this.props.onLoaded();
126 this.setState({ loading: false });
132 handleIssuePopupToggle = (issue: string, popupName: string, open?: boolean) => {
133 this.setState((state: State) => {
135 state.issuePopup && state.issuePopup.name === popupName && state.issuePopup.issue === issue;
136 if (open !== false && !samePopup) {
137 return { issuePopup: { issue, name: popupName } };
138 } else if (open !== true && samePopup) {
139 return { issuePopup: undefined };
145 handleLinePopupToggle = ({
151 }: T.LinePopup & { component: string }) => {
152 this.setState((state: State) => {
154 state.linePopup !== undefined &&
155 state.linePopup.line === line &&
156 state.linePopup.name === name &&
157 state.linePopup.component === component &&
158 state.linePopup.index === index;
159 if (open !== false && !samePopup) {
160 return { linePopup: { component, index, line, name } };
161 } else if (open !== true && samePopup) {
162 return { linePopup: undefined };
168 handleCloseLinePopup = () => {
169 this.setState({ linePopup: undefined });
172 renderDuplicationPopup = (component: T.SourceViewerFile, index: number, line: number) => {
173 const { duplicatedFiles, duplications } = this.state;
175 if (!component || !duplicatedFiles) {
179 const blocks = getDuplicationBlocksForIndex(duplications, index);
182 <WorkspaceContext.Consumer>
183 {({ openComponent }) => (
185 blocks={filterDuplicationBlocksByLine(blocks, line)}
186 branchLike={this.props.branchLike}
187 duplicatedFiles={duplicatedFiles}
188 inRemovedComponent={isDuplicationBlockInRemovedComponent(blocks)}
189 onClose={this.handleCloseLinePopup}
190 openComponent={openComponent}
191 sourceViewerFile={component}
194 </WorkspaceContext.Consumer>
199 const { loading } = this.state;
209 const { components, duplications, duplicationsByLine, linePopup } = this.state;
210 const issuesByComponent = issuesByComponentAndLine(this.props.issues);
211 const locationsByComponent = groupLocationsByComponent(this.props.locations, components);
215 {locationsByComponent.map((snippetGroup, i) => {
216 let componentProps = {};
217 if (linePopup && snippetGroup.component.key === linePopup.component) {
221 linePopup: { index: linePopup.index, line: linePopup.line, name: linePopup.name }
225 <SourceViewerContext.Provider
226 key={`${this.props.issue.key}-${this.props.selectedFlowIndex}-${i}`}
227 value={{ branchLike: this.props.branchLike, file: snippetGroup.component }}>
228 <ComponentSourceSnippetViewer
229 branchLike={this.props.branchLike}
230 highlightedLocationMessage={this.props.highlightedLocationMessage}
231 issue={this.props.issue}
232 issuePopup={this.state.issuePopup}
233 issuesByLine={issuesByComponent[snippetGroup.component.key] || {}}
234 last={i === locationsByComponent.length - 1}
235 loadDuplications={this.fetchDuplications}
236 locations={snippetGroup.locations || []}
237 onIssueChange={this.props.onIssueChange}
238 onIssuePopupToggle={this.handleIssuePopupToggle}
239 onLinePopupToggle={this.handleLinePopupToggle}
240 onLocationSelect={this.props.onLocationSelect}
241 renderDuplicationPopup={this.renderDuplicationPopup}
242 scroll={this.props.scroll}
243 snippetGroup={snippetGroup}
246 </SourceViewerContext.Provider>