SwipeableBlockedRow.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import React, { PropsWithChildren, useRef } from 'react';
  2. import { Animated, StyleSheet, Text, View } from 'react-native';
  3. import { RectButton, Swipeable } from 'react-native-gesture-handler';
  4. import { Colors } from 'src/theme';
  5. import { getFontSize } from 'src/utils';
  6. import { Blocked } from '../types';
  7. import { usePostSetBlockMutation } from '@api/chat';
  8. import BanIcon from 'assets/icons/messages/ban.svg';
  9. interface AppleStyleSwipeableRowProps extends PropsWithChildren<unknown> {
  10. data: Blocked;
  11. token: string;
  12. onRowOpen: (ref: any) => void;
  13. refetchBlocked: () => void;
  14. }
  15. const SwipeableBlockedRow: React.FC<AppleStyleSwipeableRowProps> = ({
  16. children,
  17. data,
  18. token,
  19. onRowOpen,
  20. refetchBlocked
  21. }) => {
  22. const swipeableRow = useRef<Swipeable>(null);
  23. const { mutateAsync: unBlockUser } = usePostSetBlockMutation();
  24. const close = () => {
  25. swipeableRow.current?.close();
  26. };
  27. const renderRightAction = (
  28. text: string,
  29. color: string,
  30. x: number,
  31. progress: Animated.AnimatedInterpolation<number>
  32. ) => {
  33. const trans = progress.interpolate({
  34. inputRange: [0, 1],
  35. outputRange: [x, 0]
  36. });
  37. const pressHandler = async () => {
  38. close();
  39. await unBlockUser({
  40. token,
  41. value: 0,
  42. conversation_with_user: data.id
  43. });
  44. refetchBlocked();
  45. };
  46. return (
  47. <Animated.View style={{ flex: 1, transform: [{ translateX: trans }] }}>
  48. <RectButton style={[styles.rightAction, { backgroundColor: color }]} onPress={pressHandler}>
  49. <BanIcon height={18} width={18} fill={Colors.WHITE} />
  50. <Text style={styles.actionText}>{text}</Text>
  51. </RectButton>
  52. </Animated.View>
  53. );
  54. };
  55. const renderRightActions = (progress: Animated.AnimatedInterpolation<number>) => (
  56. <View
  57. style={{
  58. width: 84,
  59. flexDirection: 'row'
  60. }}
  61. >
  62. {renderRightAction('Unblock', Colors.RED, 84, progress)}
  63. </View>
  64. );
  65. return (
  66. <Swipeable
  67. ref={swipeableRow}
  68. friction={2}
  69. enableTrackpadTwoFingerGesture
  70. rightThreshold={40}
  71. renderRightActions={renderRightActions}
  72. onSwipeableOpenStartDrag={() => {
  73. onRowOpen(swipeableRow.current);
  74. }}
  75. >
  76. {children}
  77. </Swipeable>
  78. );
  79. };
  80. const styles = StyleSheet.create({
  81. actionText: {
  82. color: Colors.WHITE,
  83. fontSize: getFontSize(12),
  84. fontWeight: '600',
  85. backgroundColor: 'transparent'
  86. },
  87. rightAction: {
  88. alignItems: 'center',
  89. flex: 1,
  90. justifyContent: 'center',
  91. gap: 8
  92. }
  93. });
  94. export default SwipeableBlockedRow;