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.

Breadcrumbs.tsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 key from 'keymaster';
  21. import * as React from 'react';
  22. import { getBreadcrumbs } from '../../../api/components';
  23. import { getBranchLikeQuery, isSameBranchLike } from '../../../helpers/branch-like';
  24. import { BranchLike } from '../../../types/branch-like';
  25. import Breadcrumb from './Breadcrumb';
  26. interface Props {
  27. backToFirst: boolean;
  28. branchLike?: BranchLike;
  29. className?: string;
  30. component: T.ComponentMeasure;
  31. handleSelect: (component: T.ComponentMeasureIntern) => void;
  32. rootComponent: T.ComponentMeasure;
  33. }
  34. interface State {
  35. breadcrumbs: T.ComponentMeasure[];
  36. }
  37. export default class Breadcrumbs extends React.PureComponent<Props, State> {
  38. mounted = false;
  39. state: State = { breadcrumbs: [] };
  40. componentDidMount() {
  41. this.mounted = true;
  42. this.fetchBreadcrumbs();
  43. this.attachShortcuts();
  44. }
  45. componentDidUpdate(prevProps: Props) {
  46. if (
  47. this.props.component !== prevProps.component ||
  48. !isSameBranchLike(prevProps.branchLike, this.props.branchLike)
  49. ) {
  50. this.fetchBreadcrumbs();
  51. }
  52. }
  53. componentWillUnmount() {
  54. this.mounted = false;
  55. this.detachShortcuts();
  56. }
  57. attachShortcuts() {
  58. key('left', 'measures-files', () => {
  59. const { breadcrumbs } = this.state;
  60. if (breadcrumbs.length > 1) {
  61. const idx = this.props.backToFirst ? 0 : breadcrumbs.length - 2;
  62. this.props.handleSelect(breadcrumbs[idx]);
  63. }
  64. return false;
  65. });
  66. }
  67. detachShortcuts() {
  68. key.unbind('left', 'measures-files');
  69. }
  70. fetchBreadcrumbs = () => {
  71. const { branchLike, component, rootComponent } = this.props;
  72. const isRoot = component.key === rootComponent.key;
  73. if (isRoot) {
  74. if (this.mounted) {
  75. this.setState({ breadcrumbs: [component] });
  76. }
  77. return;
  78. }
  79. getBreadcrumbs({ component: component.key, ...getBranchLikeQuery(branchLike) }).then(
  80. breadcrumbs => {
  81. if (this.mounted) {
  82. this.setState({ breadcrumbs });
  83. }
  84. },
  85. () => {}
  86. );
  87. };
  88. render() {
  89. const { breadcrumbs } = this.state;
  90. if (breadcrumbs.length <= 0) {
  91. return null;
  92. }
  93. const lastItem = breadcrumbs[breadcrumbs.length - 1];
  94. return (
  95. <div className={this.props.className}>
  96. {breadcrumbs.map(component => (
  97. <Breadcrumb
  98. canBrowse={component.key !== lastItem.key}
  99. component={component}
  100. handleSelect={this.props.handleSelect}
  101. isLast={component.key === lastItem.key}
  102. key={component.key}
  103. />
  104. ))}
  105. </div>
  106. );
  107. }
  108. }