forked from Sefaria/Sefaria-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutocompleteList.js
323 lines (303 loc) · 11.6 KB
/
AutocompleteList.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
'use strict';
import PropTypes from 'prop-types';
import React from 'react';
import {
TouchableOpacity,
Text,
View,
FlatList,
Image,
KeyboardAvoidingView,
Platform,
} from 'react-native';
import ActionSheet from 'react-native-action-sheet';
import {
SText,
} from './Misc';
import styles from './Styles';
import strings from './LocalizedStrings';
import { Topic } from './Topic';
class AutocompleteList extends React.Component {
static propTypes = {
interfaceLanguage: PropTypes.oneOf(["english", "hebrew"]).isRequired,
theme: PropTypes.object.isRequired,
themeStr: PropTypes.string.isRequired,
query: PropTypes.string,
openRef: PropTypes.func.isRequired,
openTextTocDirectly: PropTypes.func.isRequired,
openTopic: PropTypes.func.isRequired,
setCategories: PropTypes.func.isRequired,
search: PropTypes.func.isRequired,
openUri: PropTypes.func.isRequired,
};
constructor(props) {
super(props);
this.state = {
completions: [],
completionsLang: null,
recentQueries: Sefaria.recentQueries.map(q => ({...q})),
}
}
componentDidMount() {
this._isMounted = true;
}
componentWillUnmount() {
this._isMounted = false;
}
componentDidUpdate(prevProps, prevState) {
if (this.props.query !== prevProps.query) {
this.onQueryChange(this.props.query);
}
}
onQueryChange = q => {
if (q.length >= 3) {
Sefaria.api.name(q, true)
.then(results => {
if (this._isMounted) {
const typeToValue = { "ref": 1, "person": 4, "toc": 3, "topic": 2, "user": 5 };
let completions = results.completion_objects.map((c,i) =>
{
c.type = c.type.toLowerCase()
if (i === 0) {typeToValue[c.type] = 0;} // priveledge the first results' type
return c;
})
.stableSort((a,b) => typeToValue[a.type] - typeToValue[b.type])
.concat([{title: `Search for: "${this.props.query}"`, type: "searchFor"}]); // always add a searchFor element at the bottom
if (results.is_ref && results.ref) {
// manually add ref item to list
completions.unshift({
title: results.ref,
key: results.ref,
type: 'ref',
is_primary: true,
});
} else if (results.topic_slug) {
// manually add topic item to list
completions = completions.filter(c => !(c.key == results.topic_slug && c.type == 'topic'));
completions.unshift({
title: q.substring(1),
key: results.topic_slug,
type: 'topic',
});
}
this.setState({completions, completionsLang: results.lang})
}
})
.catch(error => {
})
} else {
this.setState({completions: []});
}
};
close = () => {
this.setState({completions: []});
}
openItem = (item) => {
let recentType;
if ((item.type === 'ref' && !!Sefaria.booksDict[item.key]) || item.type == 'book') {
// actually a book ref so open toc
this.props.openTextTocDirectly(item.key);
recentType = "book";
}
else if (item.type == 'ref') {
this.props.openRef(item.key);
recentType = "ref";
}
else if (item.type == "person" || item.type == "group") {
recentType = item.type;
ActionSheet.showActionSheetWithOptions(
{
options: [`View '${item.title}' on Sefaria site`,strings.cancel],
cancelButtonIndex: 1,
},
(buttonIndex) => {
if (buttonIndex === 0) {
const uri = item.type == 'person' ? `https://www.sefaria.org/person/${item.key}` : `https://www.sefaria.org/groups/${item.key.split(' ').join('-')}`;
this.props.openUri(uri);
}
}
);
} else if (item.type == "toccategory" || item.type == 'toc') {
this.props.setCategories(item.key);
recentType = "toc";
} else if (item.type == "topic") {
recentType = "topic";
this.props.openTopic(new Topic({ slug: item.key }));
} else if (item.type == "user") {
recentType = "user";
this.props.openUri(`https://www.sefaria.org/profile/${item.key}`);
}
Sefaria.saveRecentQuery(item.title, recentType, item.key, item.pic);
};
openItemOLD = (query, index) => {
// Older versions of the app only saved query and type for the recent item
// this meant that the app needed to make an extra api call to get the key to open the relevant item
const [currList, currListKey] = this.state.completions[index] ? [this.state.completions, "completions"] : [this.state.recentQueries, "recentQueries"];
currList[index].loading = true;
this.setState({[currListKey]: currList});
Sefaria.api.name(query, false).then(d => {
currList[index].loading = false;
this.setState({[currListKey]: currList});
// If the query isn't recognized as a ref, but only for reasons of capitalization. Resubmit with recognizable caps.
if (Sefaria.api.isACaseVariant(query, d)) {
this.openRef(Sefaria.api.repairCaseVariant(query, d));
return;
}
let recentType;
if (d.is_book) {
this.props.openTextTocDirectly(d.book);
recentType = "book";
}
else if (d.is_ref) {
this.props.openRef(d.ref);
recentType = "ref";
}
else if (d.type == "Person" || d.type == "Group") {
recentType = d.type.toLowerCase;
ActionSheet.showActionSheetWithOptions(
{
options: [`View '${d.key}' on Sefaria site`,strings.cancel],
cancelButtonIndex: 1,
},
(buttonIndex) => {
if (buttonIndex === 0) {
if (d.type == "Person") {
this.props.openUri(`https://www.sefaria.org/person/${d.key}`);
} else if (d.type == "Group") {
this.props.openUri(`https://www.sefaria.org/groups/${d.key.split(' ').join('-')}`);
}
}
}
);
} else if (d.type == "TocCategory") {
this.props.setCategories(d.key);
recentType = "toc";
} else if (d.type == "Group") {
recentType = "group"
} else if (d.type == "Topic") {
recentType = "topic";
this.props.openTopic(new Topic({slug: d.key}));
} else if (d.type == "User") {
recentType = "user";
this.props.openUri(`https://www.sefaria.org/profile/${d.key}`);
}
Sefaria.saveRecentQuery(query, recentType, d.key, d.pic);
});
};
renderItem = ({ item, index }) => {
if (item.query) { item.title = item.query; }
// toc queries get saved as arrays
const isHeb = this.state.completionsLang === 'he';
let src;
switch (item.type) {
case "searchFor":
case "query":
src = this.props.themeStr === "white" ? require("./img/search.png") : require("./img/search-light.png");
break;
case "ref":
case "book":
src = this.props.themeStr === "white" ? require("./img/book.png") : require("./img/book-light.png");
break;
case "toc":
case "toccategory":
src = this.props.themeStr === "white" ? require("./img/category.png") : require("./img/category-light.png");
break;
case "group":
src = this.props.themeStr === "white" ? require("./img/group.png") : require("./img/group-light.png");
break;
case "person":
src = this.props.themeStr === "white" ? require("./img/quill.png") : require("./img/quill-light.png");
break;
case "topic":
src = this.props.themeStr === "white" ? require("./img/hashtag.png") : require("./img/hashtag-light.png");
break;
case "user":
src = {uri: item.pic};
break;
}
return (
<TouchableOpacity
style={[{flexDirection: isHeb ? 'row-reverse' : 'row'}, styles.autocompleteItem, this.props.theme.bordered]}
onPress={()=>{
if (item.type === 'query') {
this.props.search(item.title || item.query);
} else if (item.type === 'searchFor') {
this.props.search(this.props.query);
} else {
if (item.key) { this.openItem(item); }
else {
// dont have key, need to query for item again
this.openItemOLD(item.query, index);
}
}
}}>
<Image source={src}
style={[styles.menuButtonMargined]}
resizeMode={'contain'}
/>
<SText lang={isHeb ? "hebrew" : "english"} style={[styles.autocompleteItemText, this.props.theme.text, {textAlign: isHeb ? 'right' : 'left', fontFamily: isHeb ? 'Heebo' : 'Amiri', paddingTop: 5, marginTop: isHeb ? 0 : 5}]}>
{ (item.type === 'toc' || item.type === 'toccategory') ? item.title.toUpperCase() : item.title }
</SText>
{item.loading ? (<Text style={[{paddingHorizontal: 10}, this.props.theme.secondaryText, !isHeb ? styles.enInt : styles.heInt]}>
{ strings.loading }
</Text>) : null}
</TouchableOpacity>
)
};
_keyExtractor = (item, pos) => {
return (item.title || item.query) + "|" + pos;
};
render() {
const isheb = this.props.interfaceLanguage === "hebrew";
const langStyle = !isheb ? styles.enInt : styles.heInt;
return (
Platform.OS == "ios" ?
(<KeyboardAvoidingView style={[styles.autocompleteList, this.props.theme.container, this.props.theme.bordered]} behavior="padding">
{!!this.state.completions.length ?
<FlatList
keyExtractor={this._keyExtractor}
contentContainerStyle={{paddingBottom: 20}}
keyboardShouldPersistTaps={'handled'}
data={this.state.completions}
renderItem={this.renderItem}
/>
:
<View style={{flex:1}}>
<View style={[{paddingVertical: 15, paddingHorizontal: 15, borderBottomWidth: 1}, this.props.theme.bordered]}>
<Text style={[this.props.theme.searchResultSummaryText, langStyle, this.props.theme.secondaryText]}>{strings.recentSearches}</Text>
</View>
<FlatList
keyExtractor={this._keyExtractor}
contentContainerStyle={{paddingBottom: 20}}
keyboardShouldPersistTaps={'handled'}
data={this.state.recentQueries}
renderItem={this.renderItem}
/>
</View>
}
</KeyboardAvoidingView>)
: (!!this.state.completions.length ?
<FlatList
keyExtractor={this._keyExtractor}
contentContainerStyle={{paddingBottom: 20}}
keyboardShouldPersistTaps={'handled'}
data={this.state.completions}
renderItem={this.renderItem}
/>
:
<View style={{flex:1}}>
<View style={[{paddingVertical: 15, paddingHorizontal: 15, borderBottomWidth: 1}, this.props.theme.bordered]}>
<Text style={[this.props.theme.searchResultSummaryText, langStyle, this.props.theme.secondaryText]}>{strings.recentSearches}</Text>
</View>
<FlatList
keyExtractor={this._keyExtractor}
contentContainerStyle={{paddingBottom: 20}}
keyboardShouldPersistTaps={'handled'}
data={this.state.recentQueries}
renderItem={this.renderItem}
/>
</View>)
);
}
}
export default AutocompleteList;