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.web.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. _onDismissed: () => void;
  64. /**
  65. * Creates a {@code ReactElement} for displaying the contents of the
  66. * notification.
  67. *
  68. * @private
  69. * @returns {ReactElement}
  70. */
  71. _renderDescription() {
  72. return (
  73. <div>
  74. {
  75. this._getDescription()
  76. }
  77. </div>
  78. );
  79. }
  80. /**
  81. * Opens the support page.
  82. *
  83. * @returns {void}
  84. * @private
  85. */
  86. _onOpenSupportLink() {
  87. window.open(interfaceConfig.SUPPORT_URL, '_blank', 'noopener');
  88. }
  89. /**
  90. * Creates action button configurations for the notification based on
  91. * notification appearance.
  92. *
  93. * @param {boolean} hideErrorSupportLink - Indicates if the support link
  94. * should be hidden in the error messages.
  95. * @private
  96. * @returns {Object[]}
  97. */
  98. _mapAppearanceToButtons(hideErrorSupportLink) {
  99. switch (this.props.appearance) {
  100. case NOTIFICATION_TYPE.ERROR: {
  101. const buttons = [
  102. {
  103. content: this.props.t('dialog.dismiss'),
  104. onClick: this._onDismissed
  105. }
  106. ];
  107. if (!hideErrorSupportLink) {
  108. buttons.push({
  109. content: this.props.t('dialog.contactSupport'),
  110. onClick: this._onOpenSupportLink
  111. });
  112. }
  113. return buttons;
  114. }
  115. case NOTIFICATION_TYPE.WARNING:
  116. return [
  117. {
  118. content: this.props.t('dialog.Ok'),
  119. onClick: this._onDismissed
  120. }
  121. ];
  122. default:
  123. return [];
  124. }
  125. }
  126. /**
  127. * Creates an icon component depending on the configured notification
  128. * appearance.
  129. *
  130. * @private
  131. * @returns {ReactElement}
  132. */
  133. _mapAppearanceToIcon() {
  134. const appearance = this.props.appearance;
  135. const secIconColor = ICON_COLOR[this.props.appearance];
  136. const iconSize = 'medium';
  137. switch (appearance) {
  138. case NOTIFICATION_TYPE.ERROR:
  139. return (
  140. <ErrorIcon
  141. label = { appearance }
  142. secondaryColor = { secIconColor }
  143. size = { iconSize } />
  144. );
  145. case NOTIFICATION_TYPE.WARNING:
  146. return (
  147. <WarningIcon
  148. label = { appearance }
  149. secondaryColor = { secIconColor }
  150. size = { iconSize } />
  151. );
  152. default:
  153. return (
  154. <EditorInfoIcon
  155. label = { appearance }
  156. secondaryColor = { secIconColor }
  157. size = { iconSize } />
  158. );
  159. }
  160. }
  161. }
  162. export default translate(Notification);