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.

BranchLikeNavigation.tsx 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 * as classNames from 'classnames';
  21. import * as React from 'react';
  22. import Toggler from 'sonar-ui-common/components/controls/Toggler';
  23. import { withAppState } from '../../../../../components/hoc/withAppState';
  24. import { BranchLike } from '../../../../../types/branch-like';
  25. import './BranchLikeNavigation.css';
  26. import CurrentBranchLike from './CurrentBranchLike';
  27. import Menu from './Menu';
  28. export interface BranchLikeNavigationProps {
  29. appState: Pick<T.AppState, 'branchesEnabled'>;
  30. branchLikes: BranchLike[];
  31. component: T.Component;
  32. currentBranchLike: BranchLike;
  33. }
  34. export function BranchLikeNavigation(props: BranchLikeNavigationProps) {
  35. const {
  36. appState: { branchesEnabled },
  37. branchLikes,
  38. component,
  39. component: { configuration },
  40. currentBranchLike
  41. } = props;
  42. const [isMenuOpen, setIsMenuOpen] = React.useState(false);
  43. const canAdminComponent = configuration && configuration.showSettings;
  44. const hasManyBranches = branchLikes.length >= 2;
  45. const isMenuEnabled = branchesEnabled && hasManyBranches;
  46. const currentBranchLikeElement = (
  47. <CurrentBranchLike
  48. branchesEnabled={Boolean(branchesEnabled)}
  49. component={component}
  50. currentBranchLike={currentBranchLike}
  51. hasManyBranches={hasManyBranches}
  52. />
  53. );
  54. return (
  55. <span
  56. className={classNames('big-spacer-left flex-0 branch-like-navigation-toggler-container', {
  57. dropdown: isMenuEnabled
  58. })}>
  59. {isMenuEnabled ? (
  60. <Toggler
  61. onRequestClose={() => setIsMenuOpen(false)}
  62. open={isMenuOpen}
  63. overlay={
  64. <Menu
  65. branchLikes={branchLikes}
  66. canAdminComponent={canAdminComponent}
  67. component={component}
  68. currentBranchLike={currentBranchLike}
  69. onClose={() => setIsMenuOpen(false)}
  70. />
  71. }>
  72. <a
  73. className="link-base-color link-no-underline"
  74. href="#"
  75. onClick={() => setIsMenuOpen(!isMenuOpen)}>
  76. {currentBranchLikeElement}
  77. </a>
  78. </Toggler>
  79. ) : (
  80. currentBranchLikeElement
  81. )}
  82. </span>
  83. );
  84. }
  85. export default withAppState(React.memo(BranchLikeNavigation));