about summary refs log tree commit diff
path: root/src/components/dms/ChatEmptyPill.tsx
blob: ffd022f560e35808e459665e1753b2be08e5fcd3 (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
97
98
import React from 'react'
import {Pressable, View} from 'react-native'
import Animated, {
  runOnJS,
  useAnimatedStyle,
  useSharedValue,
  withTiming,
} from 'react-native-reanimated'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {ScaleAndFadeIn} from '#/lib/custom-animations/ScaleAndFade'
import {ShrinkAndPop} from '#/lib/custom-animations/ShrinkAndPop'
import {useHaptics} from '#/lib/haptics'
import {isWeb} from '#/platform/detection'
import {atoms as a, useTheme} from '#/alf'
import {Text} from '#/components/Typography'

const AnimatedPressable = Animated.createAnimatedComponent(Pressable)

let lastIndex = 0

export function ChatEmptyPill() {
  const t = useTheme()
  const {_} = useLingui()
  const playHaptic = useHaptics()
  const [promptIndex, setPromptIndex] = React.useState(lastIndex)

  const scale = useSharedValue(1)

  const prompts = React.useMemo(() => {
    return [
      _(msg`Say hello!`),
      _(msg`Share your favorite feed!`),
      _(msg`Tell a joke!`),
      _(msg`Share a fun fact!`),
      _(msg`Share a cool story!`),
      _(msg`Send a neat website!`),
      _(msg`Clip 🐴 clop 🐴`),
    ]
  }, [_])

  const onPressIn = React.useCallback(() => {
    if (isWeb) return
    scale.value = withTiming(1.075, {duration: 100})
  }, [scale])

  const onPressOut = React.useCallback(() => {
    if (isWeb) return
    scale.value = withTiming(1, {duration: 100})
  }, [scale])

  const onPress = React.useCallback(() => {
    runOnJS(playHaptic)()
    let randomPromptIndex = Math.floor(Math.random() * prompts.length)
    while (randomPromptIndex === lastIndex) {
      randomPromptIndex = Math.floor(Math.random() * prompts.length)
    }
    setPromptIndex(randomPromptIndex)
    lastIndex = randomPromptIndex
  }, [playHaptic, prompts.length])

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

  return (
    <View
      style={[
        a.absolute,
        a.w_full,
        a.z_10,
        a.align_center,
        {
          top: -50,
        },
      ]}>
      <AnimatedPressable
        style={[
          a.px_xl,
          a.py_md,
          a.rounded_full,
          t.atoms.bg_contrast_25,
          a.align_center,
          animatedStyle,
        ]}
        entering={ScaleAndFadeIn}
        exiting={ShrinkAndPop}
        onPress={onPress}
        onPressIn={onPressIn}
        onPressOut={onPressOut}>
        <Text style={[a.font_bold, a.pointer_events_none]} selectable={false}>
          {prompts[promptIndex]}
        </Text>
      </AnimatedPressable>
    </View>
  )
}