Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Notification.tsx 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 Props 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. 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. type = { customActionType[index] } />
  104. ));
  105. }
  106. return [];
  107. }
  108. /**
  109. * Returns the Icon type component to be used, based on icon or appearance.
  110. *
  111. * @returns {ReactElement}
  112. */
  113. _getIcon() {
  114. const {
  115. appearance,
  116. icon
  117. // @ts-ignore
  118. } = this.props;
  119. let src;
  120. switch (icon || appearance) {
  121. case NOTIFICATION_ICON.PARTICIPANT:
  122. src = IconInfoCircle;
  123. break;
  124. case NOTIFICATION_ICON.PARTICIPANTS:
  125. src = IconUsers;
  126. break;
  127. case NOTIFICATION_ICON.WARNING:
  128. src = IconWarning;
  129. break;
  130. default:
  131. src = IconInfoCircle;
  132. break;
  133. }
  134. return src;
  135. }
  136. /**
  137. * Creates an icon component depending on the configured notification
  138. * appearance.
  139. *
  140. * @private
  141. * @returns {ReactElement}
  142. */
  143. _mapAppearanceToIcon() {
  144. // @ts-ignore
  145. const { appearance } = this.props;
  146. // @ts-ignore
  147. const color = ICON_COLOR[appearance];
  148. return (
  149. <View style = { styles.iconContainer }>
  150. <Icon
  151. color = { color }
  152. size = { 24 }
  153. src = { this._getIcon() } />
  154. </View>
  155. );
  156. }
  157. /**
  158. * Implements React's {@link Component#render()}.
  159. *
  160. * @inheritdoc
  161. * @returns {ReactElement}
  162. */
  163. render() {
  164. // @ts-ignore
  165. const { icon } = this.props;
  166. const contentColumnStyles = icon === NOTIFICATION_ICON.PARTICIPANTS
  167. ? styles.contentColumn : styles.interactiveContentColumn;
  168. return (
  169. <Animated.View
  170. pointerEvents = 'box-none'
  171. style = { [
  172. styles.notification,
  173. {
  174. // @ts-ignore
  175. opacity: this.state.notificationContainerAnimation
  176. }
  177. ] }>
  178. <View style = { contentColumnStyles }>
  179. { this._mapAppearanceToIcon() }
  180. <View
  181. pointerEvents = 'box-none'
  182. style = { styles.contentContainer }>
  183. { this._renderContent() }
  184. </View>
  185. <View style = { styles.btnContainer }>
  186. { this._mapAppearanceToButtons() }
  187. </View>
  188. </View>
  189. <IconButton
  190. color = { BaseTheme.palette.icon04 }
  191. onPress = { this._onDismissed }
  192. src = { IconCloseLarge }
  193. type = { BUTTON_TYPES.TERTIARY } />
  194. </Animated.View>
  195. );
  196. }
  197. /**
  198. * Renders the notification's content. If the title or title key is present
  199. * it will be just the title. Otherwise it will fallback to description.
  200. *
  201. * @returns {Array<ReactElement>}
  202. * @private
  203. */
  204. _renderContent() {
  205. // @ts-ignore
  206. const { icon, t, title, titleArguments, titleKey } = this.props;
  207. const titleText = title || (titleKey && t(titleKey, titleArguments));
  208. const description = this._getDescription();
  209. const descriptionStyles = icon === NOTIFICATION_ICON.PARTICIPANTS
  210. ? styles.contentTextInteractive : styles.contentText;
  211. if (description?.length) {
  212. return (
  213. <>
  214. <Text style = { styles.contentTextTitle }>
  215. { titleText }
  216. </Text>
  217. {
  218. description.map((line, index) => (
  219. <Text
  220. key = { index }
  221. style = { descriptionStyles }>
  222. { replaceNonUnicodeEmojis(line) }
  223. </Text>
  224. ))
  225. }
  226. </>
  227. );
  228. }
  229. return (
  230. <Text style = { styles.contentTextTitle }>
  231. { titleText }
  232. </Text>
  233. );
  234. }
  235. _getDescription: () => Array<string>;
  236. _onDismissed: () => void;
  237. }
  238. // @ts-ignore
  239. export default translate(Notification);