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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* eslint-disable lines-around-comment */
  2. import React from 'react';
  3. import { WithTranslation } from 'react-i18next';
  4. import { Animated, Text, View } from 'react-native';
  5. import { translate } from '../../../base/i18n/functions';
  6. import {
  7. Icon,
  8. IconCloseLarge,
  9. IconInfoCircle,
  10. IconUsers,
  11. IconWarning
  12. // @ts-ignore
  13. } from '../../../base/icons';
  14. import { colors } from '../../../base/ui/Tokens';
  15. import BaseTheme from '../../../base/ui/components/BaseTheme.native';
  16. import Button from '../../../base/ui/components/native/Button';
  17. import IconButton from '../../../base/ui/components/native/IconButton';
  18. import { BUTTON_MODES, BUTTON_TYPES } from '../../../base/ui/constants.native';
  19. import { replaceNonUnicodeEmojis } from '../../../chat/functions';
  20. import { NOTIFICATION_ICON } from '../../constants';
  21. import AbstractNotification, {
  22. type IProps as AbstractNotificationProps
  23. // @ts-ignore
  24. } from '../AbstractNotification';
  25. // @ts-ignore
  26. import styles from './styles';
  27. /**
  28. * Secondary colors for notification icons.
  29. *
  30. * @type {{error, info, normal, success, warning}}
  31. */
  32. const ICON_COLOR = {
  33. error: colors.error06,
  34. normal: colors.primary06,
  35. success: colors.success05,
  36. warning: colors.warning05
  37. };
  38. export type Props = AbstractNotificationProps & WithTranslation & {
  39. _participants: ArrayLike<any>;
  40. };
  41. /**
  42. * Implements a React {@link Component} to display a notification.
  43. *
  44. * @augments Component
  45. */
  46. class Notification extends AbstractNotification<Props> {
  47. /**
  48. * Initializes a new {@code Notification} instance.
  49. *
  50. * @inheritdoc
  51. */
  52. constructor(props: Props) {
  53. super(props);
  54. // @ts-ignore
  55. this.state = {
  56. notificationContainerAnimation: new Animated.Value(0)
  57. };
  58. }
  59. /**
  60. * Implements React's {@link Component#componentDidMount()}.
  61. *
  62. * @inheritdoc
  63. */
  64. componentDidMount() {
  65. Animated.timing(
  66. // @ts-ignore
  67. this.state.notificationContainerAnimation,
  68. {
  69. toValue: 1,
  70. duration: 500,
  71. useNativeDriver: true
  72. })
  73. .start();
  74. }
  75. /**
  76. * Creates action button configurations for the notification based on
  77. * notification appearance.
  78. *
  79. * @private
  80. * @returns {Object[]}
  81. */
  82. _mapAppearanceToButtons() {
  83. const {
  84. customActionHandler,
  85. customActionNameKey,
  86. customActionType
  87. // @ts-ignore
  88. } = this.props;
  89. if (customActionNameKey?.length && customActionHandler?.length && customActionType?.length) {
  90. return customActionNameKey?.map((customAction: string, index: number) => (
  91. <Button
  92. accessibilityLabel = { customAction }
  93. key = { index }
  94. labelKey = { customAction }
  95. mode = { BUTTON_MODES.TEXT }
  96. // eslint-disable-next-line react/jsx-no-bind
  97. onClick = { () => {
  98. if (customActionHandler[index]()) {
  99. this._onDismissed();
  100. }
  101. } }
  102. style = { styles.btn }
  103. // @ts-ignore
  104. type = { customActionType[index] } />
  105. ));
  106. }
  107. return [];
  108. }
  109. /**
  110. * Returns the Icon type component to be used, based on icon or appearance.
  111. *
  112. * @returns {ReactElement}
  113. */
  114. _getIcon() {
  115. const {
  116. appearance,
  117. icon
  118. // @ts-ignore
  119. } = this.props;
  120. let src;
  121. switch (icon || appearance) {
  122. case NOTIFICATION_ICON.PARTICIPANT:
  123. src = IconInfoCircle;
  124. break;
  125. case NOTIFICATION_ICON.PARTICIPANTS:
  126. src = IconUsers;
  127. break;
  128. case NOTIFICATION_ICON.WARNING:
  129. src = IconWarning;
  130. break;
  131. default:
  132. src = IconInfoCircle;
  133. break;
  134. }
  135. return src;
  136. }
  137. /**
  138. * Creates an icon component depending on the configured notification
  139. * appearance.
  140. *
  141. * @private
  142. * @returns {ReactElement}
  143. */
  144. _mapAppearanceToIcon() {
  145. // @ts-ignore
  146. const { appearance } = this.props;
  147. // @ts-ignore
  148. const color = ICON_COLOR[appearance];
  149. return (
  150. <View style = { styles.iconContainer }>
  151. <Icon
  152. color = { color }
  153. size = { 24 }
  154. src = { this._getIcon() } />
  155. </View>
  156. );
  157. }
  158. /**
  159. * Implements React's {@link Component#render()}.
  160. *
  161. * @inheritdoc
  162. * @returns {ReactElement}
  163. */
  164. render() {
  165. // @ts-ignore
  166. const { icon } = this.props;
  167. const contentColumnStyles = icon === NOTIFICATION_ICON.PARTICIPANTS
  168. ? styles.contentColumn : styles.interactiveContentColumn;
  169. const description = this._getDescription();
  170. const notificationStyles = description?.length
  171. ? styles.notificationWithDescription
  172. : styles.notification;
  173. return (
  174. <Animated.View
  175. pointerEvents = 'box-none'
  176. style = { [
  177. notificationStyles,
  178. {
  179. // @ts-ignore
  180. opacity: this.state.notificationContainerAnimation
  181. }
  182. ] }>
  183. <View style = { contentColumnStyles }>
  184. { this._mapAppearanceToIcon() }
  185. <View
  186. pointerEvents = 'box-none'
  187. style = { styles.contentContainer }>
  188. { this._renderContent() }
  189. </View>
  190. <View style = { styles.btnContainer }>
  191. { this._mapAppearanceToButtons() }
  192. </View>
  193. </View>
  194. <IconButton
  195. color = { BaseTheme.palette.icon04 }
  196. onPress = { this._onDismissed }
  197. src = { IconCloseLarge }
  198. type = { BUTTON_TYPES.TERTIARY } />
  199. </Animated.View>
  200. );
  201. }
  202. /**
  203. * Renders the notification's content. If the title or title key is present
  204. * it will be just the title. Otherwise it will fallback to description.
  205. *
  206. * @returns {Array<ReactElement>}
  207. * @private
  208. */
  209. _renderContent() {
  210. // @ts-ignore
  211. const { t, title, titleArguments, titleKey } = this.props;
  212. const titleText = title || (titleKey && t(titleKey, titleArguments));
  213. const description = this._getDescription();
  214. if (description?.length) {
  215. return (
  216. <>
  217. <Text style = { styles.contentTextTitle }>
  218. { titleText }
  219. </Text>
  220. {
  221. description.map((line, index) => (
  222. <Text
  223. key = { index }
  224. style = { styles.contentText }>
  225. { replaceNonUnicodeEmojis(line) }
  226. </Text>
  227. ))
  228. }
  229. </>
  230. );
  231. }
  232. return (
  233. <Text style = { styles.contentTextTitle }>
  234. { titleText }
  235. </Text>
  236. );
  237. }
  238. _getDescription: () => Array<string>;
  239. _onDismissed: () => void;
  240. }
  241. // @ts-ignore
  242. export default translate(Notification);