Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Notification.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import { Theme } from '@mui/material';
  2. import React, { isValidElement, useCallback, useContext } from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import { useSelector } from 'react-redux';
  5. import { keyframes } from 'tss-react';
  6. import { makeStyles } from 'tss-react/mui';
  7. import Icon from '../../../base/icons/components/Icon';
  8. import {
  9. IconCheck,
  10. IconCloseLarge,
  11. IconInfo,
  12. IconMessage,
  13. IconUser,
  14. IconUsers,
  15. IconWarningCircle
  16. } from '../../../base/icons/svg';
  17. import Message from '../../../base/react/components/web/Message';
  18. import { getSupportUrl } from '../../../base/react/functions';
  19. import { withPixelLineHeight } from '../../../base/styles/functions.web';
  20. import { NOTIFICATION_ICON, NOTIFICATION_TYPE } from '../../constants';
  21. import { INotificationProps } from '../../types';
  22. import { NotificationsTransitionContext } from '../NotificationsTransition';
  23. interface IProps extends INotificationProps {
  24. /**
  25. * Callback invoked when the user clicks to dismiss the notification.
  26. */
  27. onDismissed: Function;
  28. }
  29. /**
  30. * Secondary colors for notification icons.
  31. *
  32. * @type {{error, info, normal, success, warning}}
  33. */
  34. const useStyles = makeStyles()((theme: Theme) => {
  35. return {
  36. container: {
  37. backgroundColor: theme.palette.ui10,
  38. padding: '8px 16px 8px 20px',
  39. display: 'flex',
  40. position: 'relative' as const,
  41. borderRadius: `${theme.shape.borderRadius}px`,
  42. boxShadow: '0px 6px 20px rgba(0, 0, 0, 0.25)',
  43. marginBottom: theme.spacing(2),
  44. '&:last-of-type': {
  45. marginBottom: 0
  46. },
  47. animation: `${keyframes`
  48. 0% {
  49. opacity: 0;
  50. transform: translateX(-80%);
  51. }
  52. 100% {
  53. opacity: 1;
  54. transform: translateX(0);
  55. }
  56. `} 0.2s forwards ease`,
  57. '&.unmount': {
  58. animation: `${keyframes`
  59. 0% {
  60. opacity: 1;
  61. transform: translateX(0);
  62. }
  63. 100% {
  64. opacity: 0;
  65. transform: translateX(-80%);
  66. }
  67. `} 0.2s forwards ease`
  68. }
  69. },
  70. ribbon: {
  71. width: '4px',
  72. height: 'calc(100% - 16px)',
  73. position: 'absolute' as const,
  74. left: 0,
  75. top: '8px',
  76. borderRadius: '4px',
  77. '&.normal': {
  78. backgroundColor: theme.palette.action01
  79. },
  80. '&.error': {
  81. backgroundColor: theme.palette.iconError
  82. },
  83. '&.success': {
  84. backgroundColor: theme.palette.success01
  85. },
  86. '&.warning': {
  87. backgroundColor: theme.palette.warning01
  88. }
  89. },
  90. content: {
  91. display: 'flex',
  92. alignItems: 'flex-start',
  93. padding: '8px 0',
  94. flex: 1,
  95. maxWidth: '100%'
  96. },
  97. textContainer: {
  98. display: 'flex',
  99. flexDirection: 'column' as const,
  100. justifyContent: 'space-between',
  101. color: theme.palette.text04,
  102. flex: 1,
  103. margin: '0 8px',
  104. // maxWidth: 100% minus the icon on left (20px) minus the close icon on the right (20px) minus the margins
  105. maxWidth: 'calc(100% - 40px - 16px)',
  106. maxHeight: '150px'
  107. },
  108. title: {
  109. ...withPixelLineHeight(theme.typography.bodyShortBold)
  110. },
  111. description: {
  112. ...withPixelLineHeight(theme.typography.bodyShortRegular),
  113. overflow: 'auto',
  114. overflowWrap: 'break-word',
  115. userSelect: 'all',
  116. '&:not(:empty)': {
  117. marginTop: theme.spacing(1)
  118. }
  119. },
  120. actionsContainer: {
  121. display: 'flex',
  122. width: '100%',
  123. '&:not(:empty)': {
  124. marginTop: theme.spacing(2)
  125. }
  126. },
  127. action: {
  128. border: 0,
  129. outline: 0,
  130. backgroundColor: 'transparent',
  131. color: theme.palette.action01,
  132. ...withPixelLineHeight(theme.typography.bodyShortBold),
  133. marginRight: theme.spacing(3),
  134. padding: 0,
  135. cursor: 'pointer',
  136. '&:last-of-type': {
  137. marginRight: 0
  138. },
  139. '&.destructive': {
  140. color: theme.palette.textError
  141. }
  142. },
  143. closeIcon: {
  144. cursor: 'pointer'
  145. }
  146. };
  147. });
  148. const Notification = ({
  149. appearance = NOTIFICATION_TYPE.NORMAL,
  150. customActionHandler,
  151. customActionNameKey,
  152. customActionType,
  153. description,
  154. descriptionArguments,
  155. descriptionKey,
  156. disableClosing,
  157. hideErrorSupportLink,
  158. icon,
  159. onDismissed,
  160. title,
  161. titleArguments,
  162. titleKey,
  163. uid
  164. }: IProps) => {
  165. const { classes, cx, theme } = useStyles();
  166. const { t } = useTranslation();
  167. const { unmounting } = useContext(NotificationsTransitionContext);
  168. const supportUrl = useSelector(getSupportUrl);
  169. const ICON_COLOR = {
  170. error: theme.palette.iconError,
  171. normal: theme.palette.action01,
  172. success: theme.palette.success01,
  173. warning: theme.palette.warning01
  174. };
  175. const onDismiss = useCallback(() => {
  176. onDismissed(uid);
  177. }, [ uid ]);
  178. // eslint-disable-next-line react/no-multi-comp
  179. const renderDescription = useCallback(() => {
  180. const descriptionArray = [];
  181. descriptionKey
  182. && descriptionArray.push(t(descriptionKey, descriptionArguments));
  183. description && typeof description === 'string' && descriptionArray.push(description);
  184. // Keeping in mind that:
  185. // - Notifications that use the `translateToHtml` function get an element-based description array with one entry
  186. // - Message notifications receive string-based description arrays that might need additional parsing
  187. // We look for ready-to-render elements, and if present, we roll with them
  188. // Otherwise, we use the Message component that accepts a string `text` prop
  189. const shouldRenderHtml = descriptionArray.length === 1 && isValidElement(descriptionArray[0]);
  190. // the id is used for testing the UI
  191. return (
  192. <div
  193. className = { classes.description }
  194. data-testid = { descriptionKey } >
  195. {shouldRenderHtml ? descriptionArray : <Message text = { descriptionArray.join(' ') } />}
  196. {typeof description === 'object' && description}
  197. </div>
  198. );
  199. }, [ description, descriptionArguments, descriptionKey, classes ]);
  200. const _onOpenSupportLink = useCallback(() => {
  201. window.open(supportUrl, '_blank', 'noopener');
  202. }, [ supportUrl ]);
  203. const mapAppearanceToButtons = useCallback((): {
  204. content: string; onClick: () => void; testId?: string; type?: string; }[] => {
  205. switch (appearance) {
  206. case NOTIFICATION_TYPE.ERROR: {
  207. const buttons = [
  208. {
  209. content: t('dialog.dismiss'),
  210. onClick: onDismiss
  211. }
  212. ];
  213. if (!hideErrorSupportLink && supportUrl) {
  214. buttons.push({
  215. content: t('dialog.contactSupport'),
  216. onClick: _onOpenSupportLink
  217. });
  218. }
  219. return buttons;
  220. }
  221. case NOTIFICATION_TYPE.WARNING:
  222. return [
  223. {
  224. content: t('dialog.Ok'),
  225. onClick: onDismiss
  226. }
  227. ];
  228. default:
  229. if (customActionNameKey?.length && customActionHandler?.length) {
  230. return customActionNameKey.map((customAction: string, customActionIndex: number) => {
  231. return {
  232. content: t(customAction),
  233. onClick: () => {
  234. if (customActionHandler?.[customActionIndex]()) {
  235. onDismiss();
  236. }
  237. },
  238. type: customActionType?.[customActionIndex],
  239. testId: customAction
  240. };
  241. });
  242. }
  243. return [];
  244. }
  245. }, [ appearance, onDismiss, customActionHandler, customActionNameKey, hideErrorSupportLink, supportUrl ]);
  246. const getIcon = useCallback(() => {
  247. let iconToDisplay;
  248. switch (icon || appearance) {
  249. case NOTIFICATION_ICON.ERROR:
  250. case NOTIFICATION_ICON.WARNING:
  251. iconToDisplay = IconWarningCircle;
  252. break;
  253. case NOTIFICATION_ICON.SUCCESS:
  254. iconToDisplay = IconCheck;
  255. break;
  256. case NOTIFICATION_ICON.MESSAGE:
  257. iconToDisplay = IconMessage;
  258. break;
  259. case NOTIFICATION_ICON.PARTICIPANT:
  260. iconToDisplay = IconUser;
  261. break;
  262. case NOTIFICATION_ICON.PARTICIPANTS:
  263. iconToDisplay = IconUsers;
  264. break;
  265. default:
  266. iconToDisplay = IconInfo;
  267. break;
  268. }
  269. return iconToDisplay;
  270. }, [ icon, appearance ]);
  271. return (
  272. <div
  273. aria-atomic = 'false'
  274. aria-live = 'polite'
  275. className = { cx(classes.container, (unmounting.get(uid ?? '') && 'unmount') as string | undefined) }
  276. data-testid = { titleKey || descriptionKey }
  277. id = { uid }>
  278. <div className = { cx(classes.ribbon, appearance) } />
  279. <div className = { classes.content }>
  280. <div className = { icon }>
  281. <Icon
  282. color = { ICON_COLOR[appearance as keyof typeof ICON_COLOR] }
  283. size = { 20 }
  284. src = { getIcon() } />
  285. </div>
  286. <div className = { classes.textContainer }>
  287. <span className = { classes.title }>{title || t(titleKey ?? '', titleArguments)}</span>
  288. {renderDescription()}
  289. <div className = { classes.actionsContainer }>
  290. {mapAppearanceToButtons().map(({ content, onClick, type, testId }) => (
  291. <button
  292. className = { cx(classes.action, type) }
  293. data-testid = { testId }
  294. key = { content }
  295. onClick = { onClick }>
  296. {content}
  297. </button>
  298. ))}
  299. </div>
  300. </div>
  301. { !disableClosing && (
  302. <Icon
  303. className = { classes.closeIcon }
  304. color = { theme.palette.icon04 }
  305. id = 'close-notification'
  306. onClick = { onDismiss }
  307. size = { 20 }
  308. src = { IconCloseLarge }
  309. testId = { `${titleKey || descriptionKey}-dismiss` } />
  310. )}
  311. </div>
  312. </div>
  313. );
  314. };
  315. export default Notification;