about summary refs log tree commit diff
path: root/src/view/com/composer/text-input/mobile/Autocomplete.tsx
blob: f8335d4b94e9e353bf7419b48153d76e04022647 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React, {useEffect} from 'react'
import {Animated, TouchableOpacity, StyleSheet, View} from 'react-native'
import {observer} from 'mobx-react-lite'
import {UserAutocompleteModel} from 'state/models/discovery/user-autocomplete'
import {useAnimatedValue} from 'lib/hooks/useAnimatedValue'
import {usePalette} from 'lib/hooks/usePalette'
import {Text} from 'view/com/util/text/Text'
import {UserAvatar} from 'view/com/util/UserAvatar'
import {useGrapheme} from '../hooks/useGrapheme'

export const Autocomplete = observer(function AutocompleteImpl({
  view,
  onSelect,
}: {
  view: UserAutocompleteModel
  onSelect: (item: string) => void
}) {
  const pal = usePalette('default')
  const positionInterp = useAnimatedValue(0)
  const {getGraphemeString} = useGrapheme()

  useEffect(() => {
    Animated.timing(positionInterp, {
      toValue: view.isActive ? 1 : 0,
      duration: 200,
      useNativeDriver: true,
    }).start()
  }, [positionInterp, view.isActive])

  const topAnimStyle = {
    transform: [
      {
        translateY: positionInterp.interpolate({
          inputRange: [0, 1],
          outputRange: [200, 0],
        }),
      },
    ],
  }

  return (
    <Animated.View style={topAnimStyle}>
      {view.isActive ? (
        <View style={[pal.view, styles.container, pal.border]}>
          {view.suggestions.length > 0 ? (
            view.suggestions.slice(0, 5).map(item => {
              // Eventually use an average length
              const MAX_CHARS = 40
              const MAX_HANDLE_CHARS = 20

              // Using this approach because styling is not respecting
              // bounding box wrapping (before converting to ellipsis)
              const {name: displayHandle, remainingCharacters} =
                getGraphemeString(item.handle, MAX_HANDLE_CHARS)

              const {name: displayName} = getGraphemeString(
                item.displayName ?? item.handle,
                MAX_CHARS -
                  MAX_HANDLE_CHARS +
                  (remainingCharacters > 0 ? remainingCharacters : 0),
              )

              return (
                <TouchableOpacity
                  testID="autocompleteButton"
                  key={item.handle}
                  style={[pal.border, styles.item]}
                  onPress={() => onSelect(item.handle)}
                  accessibilityLabel={`Select ${item.handle}`}
                  accessibilityHint="">
                  <View style={styles.avatarAndHandle}>
                    <UserAvatar avatar={item.avatar ?? null} size={24} />
                    <Text type="md-medium" style={pal.text}>
                      {displayName}
                    </Text>
                  </View>
                  <Text type="sm" style={pal.textLight} numberOfLines={1}>
                    @{displayHandle}
                  </Text>
                </TouchableOpacity>
              )
            })
          ) : (
            <Text type="sm" style={[pal.text, pal.border, styles.noResults]}>
              No result
            </Text>
          )}
        </View>
      ) : null}
    </Animated.View>
  )
})

const styles = StyleSheet.create({
  container: {
    marginLeft: -50, // Composer avatar width
    top: 10,
    borderTopWidth: 1,
  },
  item: {
    borderBottomWidth: 1,
    paddingVertical: 12,
    display: 'flex',
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    gap: 6,
  },
  avatarAndHandle: {
    display: 'flex',
    flexDirection: 'row',
    gap: 6,
    alignItems: 'center',
  },
  noResults: {
    paddingVertical: 12,
  },
})