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.

Notification.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // @flow
  2. import Flag from '@atlaskit/flag';
  3. import EditorInfoIcon from '@atlaskit/icon/glyph/editor/info';
  4. import React from 'react';
  5. import { translate } from '../../../base/i18n';
  6. import { colors } from '../../../base/ui/Tokens';
  7. import { NOTIFICATION_TYPE } from '../../constants';
  8. import AbstractNotification, {
  9. type Props
  10. } from '../AbstractNotification';
  11. declare var interfaceConfig: Object;
  12. /**
  13. * Secondary colors for notification icons.
  14. *
  15. * @type {{error, info, normal, success, warning}}
  16. */
  17. const ICON_COLOR = {
  18. error: colors.error06,
  19. normal: colors.primary06,
  20. warning: colors.warning05
  21. };
  22. /**
  23. * Implements a React {@link Component} to display a notification.
  24. *
  25. * @extends Component
  26. */
  27. class Notification extends AbstractNotification<Props> {
  28. /**
  29. * Implements React's {@link Component#render()}.
  30. *
  31. * @inheritdoc
  32. * @returns {ReactElement}
  33. */
  34. render() {
  35. const {
  36. hideErrorSupportLink,
  37. t,
  38. title,
  39. titleArguments,
  40. titleKey,
  41. uid
  42. } = this.props;
  43. return (
  44. <Flag
  45. actions = { this._mapAppearanceToButtons(hideErrorSupportLink) }
  46. description = { this._renderDescription() }
  47. icon = { this._mapAppearanceToIcon() }
  48. id = { uid }
  49. testId = { titleKey }
  50. title = { title || t(titleKey, titleArguments) } />
  51. );
  52. }
  53. _getDescription: () => Array<string>
  54. _getDescriptionKey: () => string
  55. _onDismissed: () => void;
  56. /**
  57. * Creates a {@code ReactElement} for displaying the contents of the
  58. * notification.
  59. *
  60. * @private
  61. * @returns {ReactElement}
  62. */
  63. _renderDescription() {
  64. const description = this._getDescription();
  65. // the id is used for testing the UI
  66. return (
  67. <p data-testid = { this._getDescriptionKey() } >
  68. { description }
  69. </p>
  70. );
  71. }
  72. /**
  73. * Opens the support page.
  74. *
  75. * @returns {void}
  76. * @private
  77. */
  78. _onOpenSupportLink() {
  79. window.open(interfaceConfig.SUPPORT_URL, '_blank', 'noopener');
  80. }
  81. /**
  82. * Creates action button configurations for the notification based on
  83. * notification appearance.
  84. *
  85. * @param {boolean} hideErrorSupportLink - Indicates if the support link
  86. * should be hidden in the error messages.
  87. * @private
  88. * @returns {Object[]}
  89. */
  90. _mapAppearanceToButtons(hideErrorSupportLink) {
  91. switch (this.props.appearance) {
  92. case NOTIFICATION_TYPE.ERROR: {
  93. const buttons = [
  94. {
  95. content: this.props.t('dialog.dismiss'),
  96. onClick: this._onDismissed
  97. }
  98. ];
  99. if (!hideErrorSupportLink && interfaceConfig.SUPPORT_URL) {
  100. buttons.push({
  101. content: this.props.t('dialog.contactSupport'),
  102. onClick: this._onOpenSupportLink
  103. });
  104. }
  105. return buttons;
  106. }
  107. case NOTIFICATION_TYPE.WARNING:
  108. return [
  109. {
  110. content: this.props.t('dialog.Ok'),
  111. onClick: this._onDismissed
  112. }
  113. ];
  114. default:
  115. if (this.props.customActionNameKey && this.props.customActionHandler) {
  116. return [
  117. {
  118. content: this.props.t(this.props.customActionNameKey),
  119. onClick: () => {
  120. if (this.props.customActionHandler()) {
  121. this._onDismissed();
  122. }
  123. }
  124. }
  125. ];
  126. }
  127. return [];
  128. }
  129. }
  130. /**
  131. * Creates an icon component depending on the configured notification
  132. * appearance.
  133. *
  134. * @private
  135. * @returns {ReactElement}
  136. */
  137. _mapAppearanceToIcon() {
  138. const appearance = this.props.appearance;
  139. const secIconColor = ICON_COLOR[this.props.appearance];
  140. const iconSize = 'medium';
  141. return <>
  142. <div className = { `ribbon ${appearance}` } />
  143. <EditorInfoIcon
  144. label = { appearance }
  145. secondaryColor = { secIconColor }
  146. size = { iconSize } />
  147. </>;
  148. }
  149. }
  150. export default translate(Notification);