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.

ReactionEmoji.tsx 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import React, { useEffect, useMemo, useRef, useState } from 'react';
  2. import { Animated } from 'react-native';
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { IReduxState } from '../../../app/types';
  5. import ColorSchemeRegistry from '../../../base/color-scheme/ColorSchemeRegistry';
  6. import { removeReaction } from '../../actions.any';
  7. import { IReactionEmojiProps, REACTIONS } from '../../constants';
  8. interface IProps extends IReactionEmojiProps {
  9. /**
  10. * Index of reaction on the queue.
  11. * Used to differentiate between first and other animations.
  12. */
  13. index: number;
  14. }
  15. /**
  16. * Animated reaction emoji.
  17. *
  18. * @returns {ReactElement}
  19. */
  20. function ReactionEmoji({ reaction, uid, index }: IProps) {
  21. const _styles: any = useSelector((state: IReduxState) => ColorSchemeRegistry.get(state, 'Toolbox'));
  22. const _height = useSelector((state: IReduxState) => state['features/base/responsive-ui'].clientHeight);
  23. const dispatch = useDispatch();
  24. const animationVal = useRef(new Animated.Value(0)).current;
  25. const vh = useState(_height / 100)[0];
  26. const randomInt = (min: number, max: number) => Math.floor((Math.random() * (max - min + 1)) + min);
  27. const animationIndex = useMemo(() => index % 21, [ index ]);
  28. const coordinates = useState({
  29. topX: animationIndex === 0 ? 40 : randomInt(-100, 100),
  30. topY: animationIndex === 0 ? -70 : randomInt(-65, -75),
  31. bottomX: animationIndex === 0 ? 140 : randomInt(150, 200),
  32. bottomY: animationIndex === 0 ? -50 : randomInt(-40, -50)
  33. })[0];
  34. useEffect(() => {
  35. setTimeout(() => dispatch(removeReaction(uid)), 5000);
  36. }, []);
  37. useEffect(() => {
  38. Animated.timing(
  39. animationVal,
  40. {
  41. toValue: 1,
  42. duration: 5000,
  43. useNativeDriver: true
  44. }
  45. ).start();
  46. }, [ animationVal ]);
  47. return (
  48. <Animated.Text
  49. style = {{
  50. ..._styles.emojiAnimation,
  51. transform: [
  52. { translateY: animationVal.interpolate({
  53. inputRange: [ 0, 0.70, 0.75, 1 ],
  54. outputRange: [ 0, coordinates.topY * vh, coordinates.topY * vh, coordinates.bottomY * vh ]
  55. })
  56. }, {
  57. translateX: animationVal.interpolate({
  58. inputRange: [ 0, 0.70, 0.75, 1 ],
  59. outputRange: [ 0, coordinates.topX, coordinates.topX,
  60. coordinates.topX < 0 ? -coordinates.bottomX : coordinates.bottomX ]
  61. })
  62. }, {
  63. scale: animationVal.interpolate({
  64. inputRange: [ 0, 0.70, 0.75, 1 ],
  65. outputRange: [ 0.6, 1.5, 1.5, 1 ]
  66. })
  67. }
  68. ],
  69. opacity: animationVal.interpolate({
  70. inputRange: [ 0, 0.7, 0.75, 1 ],
  71. outputRange: [ 1, 1, 1, 0 ]
  72. })
  73. }}>
  74. {REACTIONS[reaction].emoji}
  75. </Animated.Text>
  76. );
  77. }
  78. export default ReactionEmoji;