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 5.1KB

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