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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. description,
  127. descriptionArguments,
  128. descriptionKey,
  129. hideErrorSupportLink,
  130. isDismissAllowed,
  131. onDismissed,
  132. t,
  133. title,
  134. titleArguments,
  135. titleKey,
  136. uid
  137. } = this.props;
  138. return (
  139. <Flag
  140. actions = { this._mapAppearanceToButtons(hideErrorSupportLink) }
  141. appearance = { appearance }
  142. description = { this._renderDescription() }
  143. icon = { this._mapAppearanceToIcon() }
  144. id = { uid }
  145. isDismissAllowed = { isDismissAllowed }
  146. onDismissed = { onDismissed }
  147. title = { title || t(titleKey, titleArguments) } />
  148. );
  149. }
  150. _onDismissed: () => void;
  151. /**
  152. * Creates a {@code ReactElement} for displaying the contents of the
  153. * notification.
  154. *
  155. * @private
  156. * @returns {ReactElement}
  157. */
  158. _renderDescription() {
  159. const {
  160. description,
  161. descriptionArguments,
  162. descriptionKey,
  163. t
  164. } = this.props;
  165. return (
  166. <div>
  167. { descriptionKey
  168. ? t(descriptionKey, descriptionArguments) : null }
  169. { description || null }
  170. </div>
  171. );
  172. }
  173. /**
  174. * Calls back into {@code FlagGroup} to dismiss the notification.
  175. *
  176. * @private
  177. * @returns {void}
  178. */
  179. _onDismissed() {
  180. this.props.onDismissed(this.props.uid);
  181. }
  182. /**
  183. * Opens the support page.
  184. *
  185. * @returns {void}
  186. * @private
  187. */
  188. _onOpenSupportLink() {
  189. window.open(interfaceConfig.SUPPORT_URL, '_blank', 'noopener');
  190. }
  191. /**
  192. * Creates action button configurations for the notification based on
  193. * notification appearance.
  194. *
  195. * @param {boolean} hideErrorSupportLink - Indicates if the support link
  196. * should be hidden in the error messages.
  197. * @private
  198. * @returns {Object[]}
  199. */
  200. _mapAppearanceToButtons(hideErrorSupportLink) {
  201. switch (this.props.appearance) {
  202. case NOTIFICATION_TYPE.ERROR: {
  203. const buttons = [
  204. {
  205. content: this.props.t('dialog.dismiss'),
  206. onClick: this._onDismissed
  207. }
  208. ];
  209. if (!hideErrorSupportLink) {
  210. buttons.push({
  211. content: this.props.t('dialog.contactSupport'),
  212. onClick: this._onOpenSupportLink
  213. });
  214. }
  215. return buttons;
  216. }
  217. case NOTIFICATION_TYPE.WARNING:
  218. return [
  219. {
  220. content: this.props.t('dialog.Ok'),
  221. onClick: this._onDismissed
  222. }
  223. ];
  224. default:
  225. return [];
  226. }
  227. }
  228. /**
  229. * Creates an icon component depending on the configured notification
  230. * appearance.
  231. *
  232. * @private
  233. * @returns {ReactElement}
  234. */
  235. _mapAppearanceToIcon() {
  236. const appearance = this.props.appearance;
  237. const secIconColor = ICON_COLOR[this.props.appearance];
  238. const iconSize = 'medium';
  239. switch (appearance) {
  240. case NOTIFICATION_TYPE.ERROR:
  241. return (
  242. <ErrorIcon
  243. label = { appearance }
  244. secondaryColor = { secIconColor }
  245. size = { iconSize } />
  246. );
  247. case NOTIFICATION_TYPE.WARNING:
  248. return (
  249. <WarningIcon
  250. label = { appearance }
  251. secondaryColor = { secIconColor }
  252. size = { iconSize } />
  253. );
  254. default:
  255. return (
  256. <EditorInfoIcon
  257. label = { appearance }
  258. secondaryColor = { secIconColor }
  259. size = { iconSize } />
  260. );
  261. }
  262. }
  263. }
  264. export default translate(Notification);