aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/issues/components/IssuesSourceViewer.tsx
blob: 2ac7e78287dae66a80571dd4d5ab1c64dc17e263 (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
/*
 * SonarQube
 * Copyright (C) 2009-2018 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 * as React from 'react';
import { getLocations, getSelectedLocation } from '../utils';
import SourceViewer from '../../../components/SourceViewer/SourceViewer';
import { scrollToElement } from '../../../helpers/scrolling';

interface Props {
  branchLike: T.BranchLike | undefined;
  loadIssues: (component: string, from: number, to: number) => Promise<T.Issue[]>;
  locationsNavigator: boolean;
  onIssueChange: (issue: T.Issue) => void;
  onIssueSelect: (issueKey: string) => void;
  onLocationSelect: (index: number) => void;
  openIssue: T.Issue;
  selectedFlowIndex: number | undefined;
  selectedLocationIndex: number | undefined;
}

export default class IssuesSourceViewer extends React.PureComponent<Props> {
  node?: HTMLElement | null;

  componentDidUpdate(prevProps: Props) {
    const { openIssue, selectedLocationIndex } = this.props;

    // Scroll back to the issue when the selected location is set to -1
    const shouldScrollBackToIssue =
      selectedLocationIndex === -1 && selectedLocationIndex !== prevProps.selectedLocationIndex;
    if (
      prevProps.openIssue.component === openIssue.component &&
      (prevProps.openIssue !== openIssue || shouldScrollBackToIssue)
    ) {
      this.scrollToIssue();
    }
  }

  scrollToIssue = (smooth = true) => {
    if (this.node) {
      const element = this.node.querySelector(`[data-issue="${this.props.openIssue.key}"]`);
      if (element) {
        this.handleScroll(element, smooth);
      }
    }
  };

  handleScroll = (element: Element, smooth = true) => {
    const offset = window.innerHeight / 2;
    scrollToElement(element, { topOffset: offset - 100, bottomOffset: offset, smooth });
  };

  handleLoaded = () => {
    this.scrollToIssue(false);
  };

  render() {
    const { openIssue, selectedFlowIndex, selectedLocationIndex } = this.props;

    const locations = getLocations(openIssue, selectedFlowIndex);
    const selectedLocation = getSelectedLocation(
      openIssue,
      selectedFlowIndex,
      selectedLocationIndex
    );

    const component = selectedLocation ? selectedLocation.component : openIssue.component;

    // if location is selected, show (and load) code around it
    // otherwise show code around the open issue
    const aroundLine = selectedLocation
      ? selectedLocation.textRange.startLine
      : openIssue.textRange && openIssue.textRange.endLine;

    // replace locations in another file with `undefined` to keep the same location indexes
    const highlightedLocations = locations.map(
      location => (location.component === component ? location : undefined)
    );

    const highlightedLocationMessage =
      this.props.locationsNavigator && selectedLocationIndex !== undefined
        ? selectedLocation && { index: selectedLocationIndex, text: selectedLocation.msg }
        : undefined;

    const allMessagesEmpty = locations !== undefined && locations.every(location => !location.msg);

    // do not load issues when open another file for a location
    const loadIssues =
      component === openIssue.component ? this.props.loadIssues : () => Promise.resolve([]);
    const selectedIssue = component === openIssue.component ? openIssue.key : undefined;

    return (
      <div ref={node => (this.node = node)}>
        <SourceViewer
          aroundLine={aroundLine}
          branchLike={this.props.branchLike}
          component={component}
          displayAllIssues={true}
          displayIssueLocationsCount={false}
          displayIssueLocationsLink={false}
          displayLocationMarkers={!allMessagesEmpty}
          highlightedLocationMessage={highlightedLocationMessage}
          highlightedLocations={highlightedLocations}
          loadIssues={loadIssues}
          onIssueChange={this.props.onIssueChange}
          onIssueSelect={this.props.onIssueSelect}
          onLoaded={this.handleLoaded}
          onLocationSelect={this.props.onLocationSelect}
          scroll={this.handleScroll}
          selectedIssue={selectedIssue}
        />
      </div>
    );
  }
}