about summary refs log tree commit diff
path: root/src/components/dms/ActionsWrapper.tsx
blob: b77516e7b7f0c08746ab323f1da07ef5e7731e9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React from 'react'
import {Keyboard} from 'react-native'
import {Gesture, GestureDetector} from 'react-native-gesture-handler'
import Animated, {
  cancelAnimation,
  runOnJS,
  useAnimatedStyle,
  useSharedValue,
  withTiming,
} from 'react-native-reanimated'
import {ChatBskyConvoDefs} from '@atproto/api'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {HITSLOP_10} from '#/lib/constants'
import {useHaptics} from '#/lib/haptics'
import {atoms as a} from '#/alf'
import {MessageMenu} from '#/components/dms/MessageMenu'
import {useMenuControl} from '#/components/Menu'

export function ActionsWrapper({
  message,
  isFromSelf,
  children,
}: {
  message: ChatBskyConvoDefs.MessageView
  isFromSelf: boolean
  children: React.ReactNode
}) {
  const {_} = useLingui()
  const playHaptic = useHaptics()
  const menuControl = useMenuControl()

  const scale = useSharedValue(1)

  const animatedStyle = useAnimatedStyle(() => ({
    transform: [{scale: scale.value}],
  }))

  const open = React.useCallback(() => {
    playHaptic()
    Keyboard.dismiss()
    menuControl.open()
  }, [menuControl, playHaptic])

  const shrink = React.useCallback(() => {
    'worklet'
    cancelAnimation(scale)
    scale.value = withTiming(1, {duration: 200})
  }, [scale])

  const doubleTapGesture = Gesture.Tap()
    .numberOfTaps(2)
    .hitSlop(HITSLOP_10)
    .onEnd(open)
    .runOnJS(true)

  const pressAndHoldGesture = Gesture.LongPress()
    .onStart(() => {
      'worklet'
      scale.value = withTiming(1.05, {duration: 200}, finished => {
        if (!finished) return
        runOnJS(open)()
        shrink()
      })
    })
    .onTouchesUp(shrink)
    .onTouchesMove(shrink)
    .cancelsTouchesInView(false)

  const composedGestures = Gesture.Exclusive(
    doubleTapGesture,
    pressAndHoldGesture,
  )

  return (
    <GestureDetector gesture={composedGestures}>
      <Animated.View
        style={[
          {
            maxWidth: '80%',
          },
          isFromSelf ? a.self_end : a.self_start,
          animatedStyle,
        ]}
        accessible={true}
        accessibilityActions={[
          {name: 'activate', label: _(msg`Open message options`)},
        ]}
        onAccessibilityAction={open}>
        {children}
        <MessageMenu message={message} control={menuControl} />
      </Animated.View>
    </GestureDetector>
  )
}