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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. 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. 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. const description = this._getDescription();
  169. const notificationStyles = description?.length
  170. ? styles.notificationWithDescription
  171. : styles.notification;
  172. return (
  173. <Animated.View
  174. pointerEvents = 'box-none'
  175. style = { [
  176. notificationStyles,
  177. {
  178. // @ts-ignore
  179. opacity: this.state.notificationContainerAnimation
  180. }
  181. ] }>
  182. <View style = { contentColumnStyles }>
  183. { this._mapAppearanceToIcon() }
  184. <View
  185. pointerEvents = 'box-none'
  186. style = { styles.contentContainer }>
  187. { this._renderContent() }
  188. </View>
  189. <View style = { styles.btnContainer }>
  190. { this._mapAppearanceToButtons() }
  191. </View>
  192. </View>
  193. <IconButton
  194. color = { BaseTheme.palette.icon04 }
  195. onPress = { this._onDismissed }
  196. src = { IconCloseLarge }
  197. type = { BUTTON_TYPES.TERTIARY } />
  198. </Animated.View>
  199. );
  200. }
  201. /**
  202. * Renders the notification's content. If the title or title key is present
  203. * it will be just the title. Otherwise it will fallback to description.
  204. *
  205. * @returns {Array<ReactElement>}
  206. * @private
  207. */
  208. _renderContent() {
  209. // @ts-ignore
  210. const { t, title, titleArguments, titleKey } = this.props;
  211. const titleText = title || (titleKey && t(titleKey, titleArguments));
  212. const description = this._getDescription();
  213. if (description?.length) {
  214. return (
  215. <>
  216. <Text style = { styles.contentTextTitle }>
  217. { titleText }
  218. </Text>
  219. {
  220. description.map((line, index) => (
  221. <Text
  222. key = { index }
  223. style = { styles.contentText }>
  224. { replaceNonUnicodeEmojis(line) }
  225. </Text>
  226. ))
  227. }
  228. </>
  229. );
  230. }
  231. return (
  232. <Text style = { styles.contentTextTitle }>
  233. { titleText }
  234. </Text>
  235. );
  236. }
  237. _getDescription: () => Array<string>;
  238. _onDismissed: () => void;
  239. }
  240. // @ts-ignore
  241. export default translate(Notification);