about summary refs log tree commit diff
path: root/src/view/com/composer/text-input/mobile/Autocomplete.tsx
blob: 5e91ef149efcaeab4cbdf6ced433bfed8168b3a4 (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
import {View} from 'react-native'
import Animated, {FadeInDown, FadeOut} from 'react-native-reanimated'
import {Trans} from '@lingui/macro'

import {PressableScale} from '#/lib/custom-animations/PressableScale'
import {sanitizeDisplayName} from '#/lib/strings/display-names'
import {sanitizeHandle} from '#/lib/strings/handles'
import {useActorAutocompleteQuery} from '#/state/queries/actor-autocomplete'
import {UserAvatar} from '#/view/com/util/UserAvatar'
import {atoms as a, useTheme} from '#/alf'
import {Text} from '#/components/Typography'

export function Autocomplete({
  prefix,
  onSelect,
}: {
  prefix: string
  onSelect: (item: string) => void
}) {
  const t = useTheme()

  const isActive = !!prefix
  const {data: suggestions, isFetching} = useActorAutocompleteQuery(
    prefix,
    true,
  )

  if (!isActive) return null

  return (
    <Animated.View
      entering={FadeInDown.duration(200)}
      exiting={FadeOut.duration(100)}
      style={[
        t.atoms.bg,
        a.mt_sm,
        a.border,
        a.rounded_sm,
        t.atoms.border_contrast_high,
        {marginLeft: -62},
      ]}>
      {suggestions?.length ? (
        suggestions.slice(0, 5).map((item, index, arr) => {
          return (
            <View
              style={[
                index !== arr.length - 1 && a.border_b,
                t.atoms.border_contrast_high,
                a.px_sm,
                a.py_md,
              ]}
              key={item.did}>
              <PressableScale
                testID="autocompleteButton"
                style={[
                  a.flex_row,
                  a.gap_sm,
                  a.justify_between,
                  a.align_center,
                ]}
                onPress={() => onSelect(item.handle)}
                accessibilityLabel={`Select ${item.handle}`}
                accessibilityHint="">
                <View style={[a.flex_row, a.gap_sm, a.align_center]}>
                  <UserAvatar
                    avatar={item.avatar ?? null}
                    size={24}
                    type={item.associated?.labeler ? 'labeler' : 'user'}
                  />
                  <Text
                    style={[a.flex_1, a.text_md, a.font_bold]}
                    emoji
                    numberOfLines={1}>
                    {sanitizeDisplayName(
                      item.displayName || sanitizeHandle(item.handle),
                    )}
                  </Text>
                  <Text
                    style={[
                      t.atoms.text_contrast_medium,
                      a.text_right,
                      {maxWidth: '50%'},
                    ]}
                    numberOfLines={1}>
                    {sanitizeHandle(item.handle, '@')}
                  </Text>
                </View>
              </PressableScale>
            </View>
          )
        })
      ) : (
        <Text style={[a.text_md, a.px_sm, a.py_md]}>
          {isFetching ? <Trans>Loading...</Trans> : <Trans>No result</Trans>}
        </Text>
      )}
    </Animated.View>
  )
}