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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
import React from 'react'
import {TextInput, View, StyleSheet, TouchableOpacity, Text} from 'react-native'
import {UserAutocompleteViewModel} from 'state/models/user-autocomplete-view'
import {observer} from 'mobx-react-lite'
import {useStores} from 'state/index'
import {usePalette} from 'lib/hooks/usePalette'
import {MagnifyingGlassIcon} from 'lib/icons'
import {ProfileCard} from '../../com/profile/ProfileCard'
export const DesktopSearch = observer(function DesktopSearch() {
const store = useStores()
const pal = usePalette('default')
const textInput = React.useRef<TextInput>(null)
const [isInputFocused, setIsInputFocused] = React.useState<boolean>(false)
const [query, setQuery] = React.useState<string>('')
const autocompleteView = React.useMemo<UserAutocompleteViewModel>(
() => new UserAutocompleteViewModel(store),
[store],
)
const onChangeQuery = (text: string) => {
setQuery(text)
if (text.length > 0 && isInputFocused) {
autocompleteView.setActive(true)
autocompleteView.setPrefix(text)
} else {
autocompleteView.setActive(false)
}
}
const onPressCancelSearch = () => {
setQuery('')
autocompleteView.setActive(false)
}
return (
<View style={styles.container}>
<View style={[pal.borderDark, pal.view, styles.search]}>
<View style={[styles.inputContainer]}>
<MagnifyingGlassIcon
size={18}
style={[pal.textLight, styles.iconWrapper]}
/>
<TextInput
testID="searchTextInput"
ref={textInput}
placeholder="Search"
placeholderTextColor={pal.colors.textLight}
selectTextOnFocus
returnKeyType="search"
value={query}
style={[pal.textLight, styles.input]}
onFocus={() => setIsInputFocused(true)}
onBlur={() => setIsInputFocused(false)}
onChangeText={onChangeQuery}
/>
{query ? (
<View style={styles.cancelBtn}>
<TouchableOpacity onPress={onPressCancelSearch}>
<Text style={[pal.link]}>Cancel</Text>
</TouchableOpacity>
</View>
) : undefined}
</View>
</View>
{query !== '' && (
<View style={[pal.view, pal.borderDark, styles.resultsContainer]}>
{autocompleteView.searchRes.length ? (
<>
{autocompleteView.searchRes.map((item, i) => (
<ProfileCard
key={item.did}
handle={item.handle}
displayName={item.displayName}
avatar={item.avatar}
noBorder={i === 0}
/>
))}
</>
) : (
<View>
<Text style={[pal.textLight, styles.noResults]}>
No results found for {autocompleteView.prefix}
</Text>
</View>
)}
</View>
)}
</View>
)
})
const styles = StyleSheet.create({
container: {
position: 'relative',
width: 300,
},
search: {
paddingHorizontal: 10,
width: 300,
borderRadius: 20,
borderWidth: 1,
},
inputContainer: {
flexDirection: 'row',
},
iconWrapper: {
paddingVertical: 7,
marginRight: 4,
},
input: {
flex: 1,
fontSize: 16,
width: '100%',
paddingTop: 7,
paddingBottom: 7,
},
cancelBtn: {
paddingRight: 4,
paddingLeft: 10,
paddingVertical: 7,
},
resultsContainer: {
// @ts-ignore supported by web
position: 'fixed',
marginTop: 40,
flexDirection: 'column',
width: 300,
borderWidth: 1,
borderRadius: 6,
paddingVertical: 4,
},
noResults: {
textAlign: 'center',
paddingVertical: 10,
},
})
|