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.

HomePageSelect.tsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 * as React from 'react';
  22. import { setHomePage } from '../../api/users';
  23. import { CurrentUserContextInterface } from '../../app/components/current-user/CurrentUserContext';
  24. import withCurrentUserContext from '../../app/components/current-user/withCurrentUserContext';
  25. import { ButtonLink } from '../../components/controls/buttons';
  26. import Tooltip from '../../components/controls/Tooltip';
  27. import HomeIcon from '../../components/icons/HomeIcon';
  28. import { translate } from '../../helpers/l10n';
  29. import { isSameHomePage } from '../../helpers/users';
  30. import { HomePage, isLoggedIn } from '../../types/users';
  31. interface Props
  32. extends Pick<CurrentUserContextInterface, 'currentUser' | 'updateCurrentUserHomepage'> {
  33. className?: string;
  34. currentPage: HomePage;
  35. }
  36. export const DEFAULT_HOMEPAGE: HomePage = { type: 'PROJECTS' };
  37. export class HomePageSelect extends React.PureComponent<Props> {
  38. buttonNode?: HTMLElement | null;
  39. async setCurrentUserHomepage(homepage: HomePage) {
  40. const { currentUser } = this.props;
  41. if (currentUser && isLoggedIn(currentUser)) {
  42. await setHomePage(homepage);
  43. this.props.updateCurrentUserHomepage(homepage);
  44. if (this.buttonNode) {
  45. this.buttonNode.focus();
  46. }
  47. }
  48. }
  49. handleClick = () => {
  50. this.setCurrentUserHomepage(this.props.currentPage);
  51. };
  52. handleReset = () => {
  53. this.setCurrentUserHomepage(DEFAULT_HOMEPAGE);
  54. };
  55. render() {
  56. const { className, currentPage, currentUser } = this.props;
  57. if (!isLoggedIn(currentUser)) {
  58. return null;
  59. }
  60. const { homepage } = currentUser;
  61. const isChecked = homepage !== undefined && isSameHomePage(homepage, currentPage);
  62. const isDefault = isChecked && isSameHomePage(currentPage, DEFAULT_HOMEPAGE);
  63. const tooltip = isChecked
  64. ? translate(isDefault ? 'homepage.current.is_default' : 'homepage.current')
  65. : translate('homepage.check');
  66. return (
  67. <Tooltip overlay={tooltip}>
  68. {isDefault ? (
  69. <span
  70. aria-label={tooltip}
  71. className={classNames('display-inline-block', className)}
  72. role="img">
  73. <HomeIcon filled={isChecked} />
  74. </span>
  75. ) : (
  76. <ButtonLink
  77. aria-label={tooltip}
  78. className={classNames('link-no-underline', 'set-homepage-link', className)}
  79. onClick={isChecked ? this.handleReset : this.handleClick}
  80. innerRef={node => (this.buttonNode = node)}>
  81. <HomeIcon filled={isChecked} />
  82. </ButtonLink>
  83. )}
  84. </Tooltip>
  85. );
  86. }
  87. }
  88. export default withCurrentUserContext(HomePageSelect);