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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 PropTypes from 'prop-types';
  8. import React, { Component } from 'react';
  9. import { translate } from '../../base/i18n';
  10. import { NOTIFICATION_TYPE } from '../constants';
  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.R400,
  19. info: colors.N500,
  20. normal: colors.N0,
  21. success: colors.G400,
  22. warning: colors.Y200
  23. };
  24. /**
  25. * Implements a React {@link Component} to display a notification.
  26. *
  27. * @extends Component
  28. */
  29. class Notification extends Component<*> {
  30. /**
  31. * Default values for {@code Notification} component's properties.
  32. *
  33. * @static
  34. */
  35. static defaultProps = {
  36. appearance: NOTIFICATION_TYPE.NORMAL
  37. };
  38. /**
  39. * {@code Notification} component's property types.
  40. *
  41. * @static
  42. */
  43. static propTypes = {
  44. /**
  45. * Display appearance for the component, passed directly to
  46. * {@code Flag}.
  47. */
  48. appearance: PropTypes.string,
  49. /**
  50. * The text to display in the body of the notification. If not passed
  51. * in, the passed in descriptionKey will be used.
  52. */
  53. defaultTitleKey: PropTypes.string,
  54. /**
  55. * The description string.
  56. */
  57. description: PropTypes.string,
  58. /**
  59. * The translation arguments that may be necessary for the description.
  60. */
  61. descriptionArguments: PropTypes.object,
  62. /**
  63. * The translation key to use as the body of the notification.
  64. */
  65. descriptionKey: PropTypes.string,
  66. /**
  67. * Whether the support link should be hidden in the case of an error
  68. * message.
  69. */
  70. hideErrorSupportLink: PropTypes.bool,
  71. /**
  72. * Whether or not the dismiss button should be displayed. This is passed
  73. * in by {@code FlagGroup}.
  74. */
  75. isDismissAllowed: PropTypes.bool,
  76. /**
  77. * Callback invoked when the user clicks to dismiss the notification.
  78. * this is passed in by {@code FlagGroup}.
  79. */
  80. onDismissed: PropTypes.func,
  81. /**
  82. * Invoked to obtain translated strings.
  83. */
  84. t: PropTypes.func,
  85. /**
  86. * The text to display at the top of the notification. If not passed in,
  87. * the passed in titleKey will be used.
  88. */
  89. title: PropTypes.string,
  90. /**
  91. * The translation key to display as the title of the notification if
  92. * no title is provided.
  93. */
  94. titleKey: PropTypes.string,
  95. /**
  96. * The unique identifier for the notification. Passed back by the
  97. * {@code Flag} component in the onDismissed callback.
  98. */
  99. uid: PropTypes.number
  100. };
  101. /**
  102. * Initializes a new {@code Notification} instance.
  103. *
  104. * @param {Object} props - The read-only properties with which the new
  105. * instance is to be initialized.
  106. */
  107. constructor(props) {
  108. super(props);
  109. // Bind event handler so it is only bound once for every instance.
  110. this._onDismissed = this._onDismissed.bind(this);
  111. }
  112. /**
  113. * Implements React's {@link Component#render()}.
  114. *
  115. * @inheritdoc
  116. * @returns {ReactElement}
  117. */
  118. render() {
  119. const {
  120. hideErrorSupportLink,
  121. appearance,
  122. titleKey,
  123. descriptionArguments,
  124. descriptionKey,
  125. description,
  126. isDismissAllowed,
  127. onDismissed,
  128. t,
  129. title,
  130. uid
  131. } = this.props;
  132. return (
  133. <Flag
  134. actions = { this._mapAppearanceToButtons(hideErrorSupportLink) }
  135. appearance = { appearance }
  136. description = { description
  137. || t(descriptionKey, descriptionArguments) }
  138. icon = { this._mapAppearanceToIcon() }
  139. id = { uid }
  140. isDismissAllowed = { isDismissAllowed }
  141. onDismissed = { onDismissed }
  142. title = { title || t(titleKey) } />
  143. );
  144. }
  145. _onDismissed: () => void;
  146. /**
  147. * Calls back into {@code FlagGroup} to dismiss the notification.
  148. *
  149. * @private
  150. * @returns {void}
  151. */
  152. _onDismissed() {
  153. this.props.onDismissed(this.props.uid);
  154. }
  155. /**
  156. * Opens the support page.
  157. *
  158. * @returns {void}
  159. * @private
  160. */
  161. _onOpenSupportLink() {
  162. window.open(interfaceConfig.SUPPORT_URL, '_blank', 'noopener');
  163. }
  164. /**
  165. * Creates action button configurations for the notification based on
  166. * notification appearance.
  167. *
  168. * @param {boolean} hideErrorSupportLink - Indicates if the support link
  169. * should be hidden in the error messages.
  170. * @private
  171. * @returns {Object[]}
  172. */
  173. _mapAppearanceToButtons(hideErrorSupportLink) {
  174. switch (this.props.appearance) {
  175. case NOTIFICATION_TYPE.ERROR: {
  176. const buttons = [
  177. {
  178. content: this.props.t('dialog.dismiss'),
  179. onClick: this._onDismissed
  180. }
  181. ];
  182. if (!hideErrorSupportLink) {
  183. buttons.push({
  184. content: this.props.t('dialog.contactSupport'),
  185. onClick: this._onOpenSupportLink
  186. });
  187. }
  188. return buttons;
  189. }
  190. case NOTIFICATION_TYPE.WARNING:
  191. return [
  192. {
  193. content: this.props.t('dialog.Ok'),
  194. onClick: this._onDismissed
  195. }
  196. ];
  197. default:
  198. return [];
  199. }
  200. }
  201. /**
  202. * Creates an icon component depending on the configured notification
  203. * appearance.
  204. *
  205. * @private
  206. * @returns {ReactElement}
  207. */
  208. _mapAppearanceToIcon() {
  209. const appearance = this.props.appearance;
  210. const secIconColor = ICON_COLOR[this.props.appearance];
  211. const iconSize = 'medium';
  212. switch (appearance) {
  213. case NOTIFICATION_TYPE.ERROR:
  214. return (
  215. <ErrorIcon
  216. label = { appearance }
  217. secondaryColor = { secIconColor }
  218. size = { iconSize } />
  219. );
  220. case NOTIFICATION_TYPE.WARNING:
  221. return (
  222. <WarningIcon
  223. label = { appearance }
  224. secondaryColor = { secIconColor }
  225. size = { iconSize } />
  226. );
  227. default:
  228. return (
  229. <EditorInfoIcon
  230. label = { appearance }
  231. secondaryColor = { secIconColor }
  232. size = { iconSize } />
  233. );
  234. }
  235. }
  236. }
  237. export default translate(Notification);