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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. * A description string that can be used in addition to the prop
  56. * descriptionKey.
  57. */
  58. description: PropTypes.string,
  59. /**
  60. * The translation arguments that may be necessary for the description.
  61. */
  62. descriptionArguments: PropTypes.object,
  63. /**
  64. * The translation key to use as the body of the notification.
  65. */
  66. descriptionKey: PropTypes.string,
  67. /**
  68. * Whether the support link should be hidden in the case of an error
  69. * message.
  70. */
  71. hideErrorSupportLink: PropTypes.bool,
  72. /**
  73. * Whether or not the dismiss button should be displayed. This is passed
  74. * in by {@code FlagGroup}.
  75. */
  76. isDismissAllowed: PropTypes.bool,
  77. /**
  78. * Callback invoked when the user clicks to dismiss the notification.
  79. * this is passed in by {@code FlagGroup}.
  80. */
  81. onDismissed: PropTypes.func,
  82. /**
  83. * Invoked to obtain translated strings.
  84. */
  85. t: PropTypes.func,
  86. /**
  87. * The text to display at the top of the notification. If not passed in,
  88. * the passed in titleKey will be used.
  89. */
  90. title: PropTypes.string,
  91. /**
  92. * The translation arguments that may be necessary for the title.
  93. */
  94. titleArguments: PropTypes.object,
  95. /**
  96. * The translation key to display as the title of the notification if
  97. * no title is provided.
  98. */
  99. titleKey: PropTypes.string,
  100. /**
  101. * The unique identifier for the notification. Passed back by the
  102. * {@code Flag} component in the onDismissed callback.
  103. */
  104. uid: PropTypes.number
  105. };
  106. /**
  107. * Initializes a new {@code Notification} instance.
  108. *
  109. * @param {Object} props - The read-only properties with which the new
  110. * instance is to be initialized.
  111. */
  112. constructor(props) {
  113. super(props);
  114. // Bind event handler so it is only bound once for every instance.
  115. this._onDismissed = this._onDismissed.bind(this);
  116. }
  117. /**
  118. * Implements React's {@link Component#render()}.
  119. *
  120. * @inheritdoc
  121. * @returns {ReactElement}
  122. */
  123. render() {
  124. const {
  125. appearance,
  126. hideErrorSupportLink,
  127. isDismissAllowed,
  128. onDismissed,
  129. t,
  130. title,
  131. titleArguments,
  132. titleKey,
  133. uid
  134. } = this.props;
  135. return (
  136. <Flag
  137. actions = { this._mapAppearanceToButtons(hideErrorSupportLink) }
  138. appearance = { appearance }
  139. description = { this._renderDescription() }
  140. icon = { this._mapAppearanceToIcon() }
  141. id = { uid }
  142. isDismissAllowed = { isDismissAllowed }
  143. onDismissed = { onDismissed }
  144. title = { title || t(titleKey, titleArguments) } />
  145. );
  146. }
  147. _onDismissed: () => void;
  148. /**
  149. * Creates a {@code ReactElement} for displaying the contents of the
  150. * notification.
  151. *
  152. * @private
  153. * @returns {ReactElement}
  154. */
  155. _renderDescription() {
  156. const {
  157. description,
  158. descriptionArguments,
  159. descriptionKey,
  160. t
  161. } = this.props;
  162. return (
  163. <div>
  164. { descriptionKey
  165. ? t(descriptionKey, descriptionArguments) : null }
  166. { description || null }
  167. </div>
  168. );
  169. }
  170. /**
  171. * Calls back into {@code FlagGroup} to dismiss the notification.
  172. *
  173. * @private
  174. * @returns {void}
  175. */
  176. _onDismissed() {
  177. this.props.onDismissed(this.props.uid);
  178. }
  179. /**
  180. * Opens the support page.
  181. *
  182. * @returns {void}
  183. * @private
  184. */
  185. _onOpenSupportLink() {
  186. window.open(interfaceConfig.SUPPORT_URL, '_blank', 'noopener');
  187. }
  188. /**
  189. * Creates action button configurations for the notification based on
  190. * notification appearance.
  191. *
  192. * @param {boolean} hideErrorSupportLink - Indicates if the support link
  193. * should be hidden in the error messages.
  194. * @private
  195. * @returns {Object[]}
  196. */
  197. _mapAppearanceToButtons(hideErrorSupportLink) {
  198. switch (this.props.appearance) {
  199. case NOTIFICATION_TYPE.ERROR: {
  200. const buttons = [
  201. {
  202. content: this.props.t('dialog.dismiss'),
  203. onClick: this._onDismissed
  204. }
  205. ];
  206. if (!hideErrorSupportLink) {
  207. buttons.push({
  208. content: this.props.t('dialog.contactSupport'),
  209. onClick: this._onOpenSupportLink
  210. });
  211. }
  212. return buttons;
  213. }
  214. case NOTIFICATION_TYPE.WARNING:
  215. return [
  216. {
  217. content: this.props.t('dialog.Ok'),
  218. onClick: this._onDismissed
  219. }
  220. ];
  221. default:
  222. return [];
  223. }
  224. }
  225. /**
  226. * Creates an icon component depending on the configured notification
  227. * appearance.
  228. *
  229. * @private
  230. * @returns {ReactElement}
  231. */
  232. _mapAppearanceToIcon() {
  233. const appearance = this.props.appearance;
  234. const secIconColor = ICON_COLOR[this.props.appearance];
  235. const iconSize = 'medium';
  236. switch (appearance) {
  237. case NOTIFICATION_TYPE.ERROR:
  238. return (
  239. <ErrorIcon
  240. label = { appearance }
  241. secondaryColor = { secIconColor }
  242. size = { iconSize } />
  243. );
  244. case NOTIFICATION_TYPE.WARNING:
  245. return (
  246. <WarningIcon
  247. label = { appearance }
  248. secondaryColor = { secIconColor }
  249. size = { iconSize } />
  250. );
  251. default:
  252. return (
  253. <EditorInfoIcon
  254. label = { appearance }
  255. secondaryColor = { secIconColor }
  256. size = { iconSize } />
  257. );
  258. }
  259. }
  260. }
  261. export default translate(Notification);