You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

actions.ts 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. import { State } from './components/IssuesApp';
  21. import { getLocations } from './utils';
  22. export function enableLocationsNavigator(state: State) {
  23. const { openIssue, selectedLocationIndex } = state;
  24. if (openIssue && (openIssue.secondaryLocations.length > 0 || openIssue.flows.length > 0)) {
  25. const selectedFlowIndex =
  26. state.selectedFlowIndex || (openIssue.flows.length > 0 ? 0 : undefined);
  27. return {
  28. locationsNavigator: true,
  29. selectedFlowIndex,
  30. // Also reset index = -1 to 0, we don't want to start on the issue when enabling the location navigator
  31. selectedLocationIndex:
  32. selectedLocationIndex && selectedLocationIndex < 0 ? 0 : selectedLocationIndex,
  33. };
  34. }
  35. return null;
  36. }
  37. export function disableLocationsNavigator() {
  38. return { locationsNavigator: false };
  39. }
  40. export function selectNextLocation(
  41. state: Pick<State, 'selectedFlowIndex' | 'selectedLocationIndex' | 'openIssue'>,
  42. ) {
  43. const { selectedFlowIndex, selectedLocationIndex: index = -1, openIssue } = state;
  44. if (openIssue) {
  45. const locations = getLocations(openIssue, selectedFlowIndex);
  46. const lastLocationIdx = locations.length - 1;
  47. if (index === lastLocationIdx) {
  48. // -1 to jump back to the issue itself
  49. return { selectedLocationIndex: -1, locationsNavigator: true };
  50. }
  51. return {
  52. selectedLocationIndex: index !== undefined && index < lastLocationIdx ? index + 1 : index,
  53. locationsNavigator: true,
  54. };
  55. }
  56. return null;
  57. }
  58. export function selectPreviousLocation(state: State) {
  59. const { selectedFlowIndex, selectedLocationIndex: index, openIssue } = state;
  60. if (openIssue) {
  61. if (index === -1) {
  62. const locations = getLocations(openIssue, selectedFlowIndex);
  63. const lastLocationIdx = locations.length - 1;
  64. return { selectedLocationIndex: lastLocationIdx, locationsNavigator: true };
  65. }
  66. return {
  67. selectedLocationIndex: index !== undefined && index > 0 ? index - 1 : index,
  68. locationsNavigator: true,
  69. };
  70. }
  71. return null;
  72. }
  73. export function selectFlow(nextIndex?: number) {
  74. return () => {
  75. return {
  76. locationsNavigator: true,
  77. selectedFlowIndex: nextIndex,
  78. selectedLocationIndex: undefined,
  79. };
  80. };
  81. }
  82. export function selectNextFlow(state: State) {
  83. const { openIssue, selectedFlowIndex } = state;
  84. if (
  85. openIssue &&
  86. selectedFlowIndex !== undefined &&
  87. (openIssue.flows.length > selectedFlowIndex + 1 ||
  88. openIssue.flowsWithType.length > selectedFlowIndex + 1)
  89. ) {
  90. return {
  91. selectedFlowIndex: selectedFlowIndex + 1,
  92. selectedLocationIndex: 0,
  93. locationsNavigator: true,
  94. };
  95. } else if (
  96. openIssue &&
  97. selectedFlowIndex === undefined &&
  98. (openIssue.flows.length > 0 || openIssue.flowsWithType.length > 0)
  99. ) {
  100. return { selectedFlowIndex: 0, selectedLocationIndex: 0, locationsNavigator: true };
  101. }
  102. return null;
  103. }
  104. export function selectPreviousFlow(state: State) {
  105. const { openIssue, selectedFlowIndex } = state;
  106. if (openIssue && selectedFlowIndex !== undefined && selectedFlowIndex > 0) {
  107. return {
  108. selectedFlowIndex: selectedFlowIndex - 1,
  109. selectedLocationIndex: 0,
  110. locationsNavigator: true,
  111. };
  112. }
  113. return null;
  114. }