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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. isDismissAllowed,
  43. onDismissed,
  44. t,
  45. title,
  46. titleArguments,
  47. titleKey,
  48. uid
  49. } = this.props;
  50. return (
  51. <Flag
  52. actions = { this._mapAppearanceToButtons(hideErrorSupportLink) }
  53. appearance = { appearance }
  54. description = { this._renderDescription() }
  55. icon = { this._mapAppearanceToIcon() }
  56. id = { uid }
  57. isDismissAllowed = { isDismissAllowed }
  58. onDismissed = { onDismissed }
  59. title = { title || t(titleKey, titleArguments) } />
  60. );
  61. }
  62. _getDescription: () => Array<string>
  63. _getDescriptionKey: () => string
  64. _onDismissed: () => void;
  65. /**
  66. * Creates a {@code ReactElement} for displaying the contents of the
  67. * notification.
  68. *
  69. * @private
  70. * @returns {ReactElement}
  71. */
  72. _renderDescription() {
  73. const description = this._getDescription();
  74. // the id is used for testing the UI
  75. return (
  76. <div id = { this._getDescriptionKey() || description || this.props.titleKey || this.props.title } >
  77. { description }
  78. </div>
  79. );
  80. }
  81. /**
  82. * Opens the support page.
  83. *
  84. * @returns {void}
  85. * @private
  86. */
  87. _onOpenSupportLink() {
  88. window.open(interfaceConfig.SUPPORT_URL, '_blank', 'noopener');
  89. }
  90. /**
  91. * Creates action button configurations for the notification based on
  92. * notification appearance.
  93. *
  94. * @param {boolean} hideErrorSupportLink - Indicates if the support link
  95. * should be hidden in the error messages.
  96. * @private
  97. * @returns {Object[]}
  98. */
  99. _mapAppearanceToButtons(hideErrorSupportLink) {
  100. switch (this.props.appearance) {
  101. case NOTIFICATION_TYPE.ERROR: {
  102. const buttons = [
  103. {
  104. content: this.props.t('dialog.dismiss'),
  105. onClick: this._onDismissed
  106. }
  107. ];
  108. if (!hideErrorSupportLink) {
  109. buttons.push({
  110. content: this.props.t('dialog.contactSupport'),
  111. onClick: this._onOpenSupportLink
  112. });
  113. }
  114. return buttons;
  115. }
  116. case NOTIFICATION_TYPE.WARNING:
  117. return [
  118. {
  119. content: this.props.t('dialog.Ok'),
  120. onClick: this._onDismissed
  121. }
  122. ];
  123. default:
  124. if (this.props.customActionNameKey && this.props.customActionHandler) {
  125. return [
  126. {
  127. content: this.props.t(this.props.customActionNameKey),
  128. onClick: () => {
  129. if (this.props.customActionHandler()) {
  130. this._onDismissed();
  131. }
  132. }
  133. }
  134. ];
  135. }
  136. return [];
  137. }
  138. }
  139. /**
  140. * Creates an icon component depending on the configured notification
  141. * appearance.
  142. *
  143. * @private
  144. * @returns {ReactElement}
  145. */
  146. _mapAppearanceToIcon() {
  147. const appearance = this.props.appearance;
  148. const secIconColor = ICON_COLOR[this.props.appearance];
  149. const iconSize = 'medium';
  150. switch (appearance) {
  151. case NOTIFICATION_TYPE.ERROR:
  152. return (
  153. <ErrorIcon
  154. label = { appearance }
  155. secondaryColor = { secIconColor }
  156. size = { iconSize } />
  157. );
  158. case NOTIFICATION_TYPE.WARNING:
  159. return (
  160. <WarningIcon
  161. label = { appearance }
  162. secondaryColor = { secIconColor }
  163. size = { iconSize } />
  164. );
  165. default:
  166. return (
  167. <EditorInfoIcon
  168. label = { appearance }
  169. secondaryColor = { secIconColor }
  170. size = { iconSize } />
  171. );
  172. }
  173. }
  174. }
  175. export default translate(Notification);