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.

MeasuresBreadcrumbs.tsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 classNames from 'classnames';
  21. import { Breadcrumbs, ClipboardIconButton, HoverLink } from 'design-system';
  22. import * as React from 'react';
  23. import { getBreadcrumbs } from '../../../api/components';
  24. import { getBranchLikeQuery, isSameBranchLike } from '../../../helpers/branch-like';
  25. import { KeyboardKeys } from '../../../helpers/keycodes';
  26. import { translate } from '../../../helpers/l10n';
  27. import { collapsePath, limitComponentName } from '../../../helpers/path';
  28. import { BranchLike } from '../../../types/branch-like';
  29. import { ComponentQualifier, isProject } from '../../../types/component';
  30. import { ComponentMeasure, ComponentMeasureIntern } from '../../../types/types';
  31. interface Props {
  32. backToFirst: boolean;
  33. branchLike?: BranchLike;
  34. className?: string;
  35. component: ComponentMeasure;
  36. handleSelect: (component: ComponentMeasureIntern) => void;
  37. rootComponent: ComponentMeasure;
  38. }
  39. interface State {
  40. breadcrumbs: ComponentMeasure[];
  41. }
  42. export default class MeasuresBreadcrumbs extends React.PureComponent<Props, State> {
  43. mounted = false;
  44. state: State = { breadcrumbs: [] };
  45. componentDidMount() {
  46. this.mounted = true;
  47. this.fetchBreadcrumbs();
  48. document.addEventListener('keydown', this.handleKeyDown);
  49. }
  50. componentDidUpdate(prevProps: Props) {
  51. if (
  52. this.props.component !== prevProps.component ||
  53. !isSameBranchLike(prevProps.branchLike, this.props.branchLike)
  54. ) {
  55. this.fetchBreadcrumbs();
  56. }
  57. }
  58. componentWillUnmount() {
  59. this.mounted = false;
  60. document.removeEventListener('keydown', this.handleKeyDown);
  61. }
  62. handleKeyDown = (event: KeyboardEvent) => {
  63. if (event.key === KeyboardKeys.LeftArrow) {
  64. event.preventDefault();
  65. const { breadcrumbs } = this.state;
  66. if (breadcrumbs.length > 1) {
  67. const idx = this.props.backToFirst ? 0 : breadcrumbs.length - 2;
  68. this.props.handleSelect(breadcrumbs[idx]);
  69. }
  70. }
  71. };
  72. fetchBreadcrumbs = () => {
  73. const { branchLike, component, rootComponent } = this.props;
  74. const isRoot = component.key === rootComponent.key;
  75. if (isRoot) {
  76. if (this.mounted) {
  77. this.setState({ breadcrumbs: [component] });
  78. }
  79. return;
  80. }
  81. getBreadcrumbs({ component: component.key, ...getBranchLikeQuery(branchLike) }).then(
  82. (breadcrumbs) => {
  83. if (this.mounted) {
  84. this.setState({ breadcrumbs });
  85. }
  86. },
  87. () => {},
  88. );
  89. };
  90. render() {
  91. const { breadcrumbs } = this.state;
  92. const lastBreadcrumb = breadcrumbs[breadcrumbs.length - 1];
  93. if (breadcrumbs.length <= 0) {
  94. return null;
  95. }
  96. return (
  97. <Breadcrumbs
  98. ariaLabel={translate('breadcrumbs')}
  99. className={classNames(this.props.className)}
  100. maxWidth={500}
  101. actions={
  102. !isProject(lastBreadcrumb.qualifier) &&
  103. lastBreadcrumb.path && <ClipboardIconButton copyValue={lastBreadcrumb.path} />
  104. }
  105. >
  106. {breadcrumbs.map((component) => (
  107. <HoverLink
  108. key={component.key}
  109. to="#"
  110. onClick={(event: React.MouseEvent<HTMLAnchorElement>) => {
  111. event.preventDefault();
  112. event.currentTarget.blur();
  113. this.props.handleSelect(component);
  114. }}
  115. >
  116. {component.qualifier === ComponentQualifier.Directory
  117. ? collapsePath(component.name, 15)
  118. : limitComponentName(component.name)}
  119. </HoverLink>
  120. ))}
  121. </Breadcrumbs>
  122. );
  123. }
  124. }