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.

NavLatestNotification.tsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 differenceInSeconds from 'date-fns/difference_in_seconds';
  21. import * as React from 'react';
  22. import ClearIcon from 'sonar-ui-common/components/icons/ClearIcon';
  23. import NotificationIcon from 'sonar-ui-common/components/icons/NotificationIcon';
  24. import { parseDate } from 'sonar-ui-common/helpers/dates';
  25. import { translate } from 'sonar-ui-common/helpers/l10n';
  26. import { PrismicFeatureNews } from '../../../api/news';
  27. import './notifications.css';
  28. interface Props {
  29. lastNews: PrismicFeatureNews;
  30. notificationsLastReadDate?: Date;
  31. notificationsOptOut?: boolean;
  32. onClick: () => void;
  33. setCurrentUserSetting: (setting: T.CurrentUserSetting) => void;
  34. }
  35. export default class NavLatestNotification extends React.PureComponent<Props> {
  36. mounted = false;
  37. checkHasUnread = () => {
  38. const { notificationsLastReadDate, lastNews } = this.props;
  39. return (
  40. !notificationsLastReadDate ||
  41. differenceInSeconds(parseDate(lastNews.publicationDate), notificationsLastReadDate) > 0
  42. );
  43. };
  44. handleClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
  45. event.preventDefault();
  46. event.currentTarget.blur();
  47. this.props.onClick();
  48. };
  49. handleDismiss = (event: React.MouseEvent<HTMLAnchorElement>) => {
  50. event.preventDefault();
  51. event.stopPropagation();
  52. this.props.setCurrentUserSetting({
  53. key: 'notifications.readDate',
  54. value: Date.now().toString()
  55. });
  56. };
  57. render() {
  58. const { notificationsOptOut, lastNews } = this.props;
  59. const hasUnread = this.checkHasUnread();
  60. const showNotifications = Boolean(!notificationsOptOut && lastNews && hasUnread);
  61. return (
  62. <>
  63. {showNotifications && (
  64. <>
  65. <li className="navbar-latest-notification" onClick={this.props.onClick}>
  66. <div className="navbar-latest-notification-wrapper">
  67. <span className="badge badge-info">{translate('new')}</span>
  68. <span className="label">{lastNews.notification}</span>
  69. </div>
  70. </li>
  71. <li className="navbar-latest-notification-dismiss">
  72. <a className="navbar-icon" href="#" onClick={this.handleDismiss}>
  73. <ClearIcon size={12} thin={true} />
  74. </a>
  75. </li>
  76. </>
  77. )}
  78. <li>
  79. <a className="navbar-icon" href="#" onClick={this.handleClick}>
  80. <NotificationIcon hasUnread={hasUnread && !notificationsOptOut} />
  81. </a>
  82. </li>
  83. </>
  84. );
  85. }
  86. }