]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9787 Allow to navigate back to the issue from the last issue locations
authorGrégoire Aubert <gregoire.aubert@sonarsource.com>
Wed, 4 Oct 2017 10:00:56 +0000 (12:00 +0200)
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>
Thu, 5 Oct 2017 10:11:12 +0000 (12:11 +0200)
server/sonar-web/src/main/js/apps/issues/actions.js
server/sonar-web/src/main/js/apps/issues/components/App.js
server/sonar-web/src/main/js/apps/issues/components/IssuesSourceViewer.js

index 2303befa5648a4ee517c5cc6c16f04de9c949b64..6f14185dd6ca2eabd6c802545de17c3048bbbb21 100644 (file)
 /*:: import type { State } from './components/App'; */
 
 export const enableLocationsNavigator = (state /*: State */) => {
-  const { openIssue } = state;
+  const { openIssue, selectedLocationIndex } = state;
   if (openIssue && (openIssue.secondaryLocations.length > 0 || openIssue.flows.length > 0)) {
     return {
       locationsNavigator: true,
       selectedFlowIndex: state.selectedFlowIndex || (openIssue.flows.length > 0 ? 0 : null),
-      selectedLocationIndex: state.selectedLocationIndex || 0
+      // Also reset index = -1 to 0, we don't want to start on the issue when enabling the location navigator
+      selectedLocationIndex:
+        !selectedLocationIndex || selectedLocationIndex < 0 ? 0 : selectedLocationIndex
     };
   }
 };
@@ -57,15 +59,28 @@ export const selectNextLocation = (state /*: State */) => {
   if (openIssue) {
     const locations =
       selectedFlowIndex != null ? openIssue.flows[selectedFlowIndex] : openIssue.secondaryLocations;
+    const lastLocationIdx = locations.length - 1;
+    if (index === lastLocationIdx) {
+      // -1 to jump back to the issue itself
+      return { selectedLocationIndex: -1 };
+    }
     return {
-      selectedLocationIndex: index != null && locations.length > index + 1 ? index + 1 : index
+      selectedLocationIndex: index != null && index < lastLocationIdx ? index + 1 : index
     };
   }
 };
 
 export const selectPreviousLocation = (state /*: State */) => {
-  const { selectedLocationIndex: index, openIssue } = state;
+  const { selectedFlowIndex, selectedLocationIndex: index, openIssue } = state;
   if (openIssue) {
+    if (index === -1) {
+      const locations =
+        selectedFlowIndex != null
+          ? openIssue.flows[selectedFlowIndex]
+          : openIssue.secondaryLocations;
+      const lastLocationIdx = locations.length - 1;
+      return { selectedLocationIndex: lastLocationIdx };
+    }
     return { selectedLocationIndex: index != null && index > 0 ? index - 1 : index };
   }
 };
index f5f83df90e752fa113d61ea6263b0c227bec01d9..f7543b877c72e8ca8b73db2ace62a69d18e4c364 100644 (file)
@@ -236,11 +236,11 @@ export default class App extends React.PureComponent {
       event.preventDefault();
       this.setState(actions.enableLocationsNavigator);
     } else if (event.keyCode === 40 && event.altKey) {
-      // alt + up
+      // alt + down
       event.preventDefault();
       this.selectNextLocation();
     } else if (event.keyCode === 38 && event.altKey) {
-      // alt + down
+      // alt + up
       event.preventDefault();
       this.selectPreviousLocation();
     } else if (event.keyCode === 37 && event.altKey) {
index 06ba694e1cdb9a1029552277ab754bb11622490b..9e00d1e57f7f7615bea8007fdee5c651790a39a1 100644 (file)
@@ -43,9 +43,14 @@ export default class IssuesSourceViewer extends React.PureComponent {
   /*:: props: Props; */
 
   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 !== this.props.openIssue &&
-      prevProps.openIssue.component === this.props.openIssue.component
+      prevProps.openIssue.component === openIssue.component &&
+      (prevProps.openIssue !== openIssue || shouldScrollBackToIssue)
     ) {
       this.scrollToIssue();
     }
@@ -77,9 +82,12 @@ export default class IssuesSourceViewer extends React.PureComponent {
 
     let locationMessage = undefined;
     let locationLine = undefined;
+
+    // We don't want to display a location message when selected location is -1
     if (
       locations != null &&
       selectedLocationIndex != null &&
+      selectedLocationIndex >= 0 &&
       locations.length >= selectedLocationIndex
     ) {
       locationMessage = {