-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnft-collections-search.tsx
96 lines (89 loc) · 2.79 KB
/
nft-collections-search.tsx
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
import { Alert, ScrollView, Text, TextInput, View } from "react-native";
import { useState, useRef, useCallback, useEffect } from "react";
import hgraphClient from "@/src/utils/hgraph-client";
import debounce from "@/src/utils/debounce";
import { SearchNFTCollection } from "@/src/gql";
import { NFTCollectionRecord } from "@/src/types";
import { NFTCollection } from "@/src/components";
export default function NftCollectionsSearch() {
const [query, setQuery] = useState("");
const [loading, setLoading] = useState(false);
const [nftCollections, setNftCollections] = useState<NFTCollectionRecord[]>();
const abortController = useRef<AbortController>();
const searchNftCollections = async (symbol: string) => {
abortController.current?.abort();
abortController.current = new AbortController();
setLoading(true);
try {
const result = await hgraphClient.query<{
collections: NFTCollectionRecord[];
}>(
{
query: SearchNFTCollection,
variables: {
symbol: `${symbol}%`,
},
},
abortController.current.signal,
);
if (result.errors) {
Alert.alert(
"Query error",
result.errors.map((error: Error) => error.message).join("\n"),
);
} else {
setNftCollections(result.data?.collections ?? []);
}
} catch (error) {}
setLoading(false);
};
const debouncedSearch = useCallback(debounce(searchNftCollections, 500), []);
useEffect(() => {
searchNftCollections(query);
}, []);
const handleSearch = (text: string) => {
setQuery(text);
debouncedSearch(text);
};
return (
<ScrollView
style={{
flex: 1,
}}
contentContainerStyle={{
rowGap: 10,
margin: 15,
paddingBottom: 15,
}}
>
<Text>
Example of searching NFT collections by its symbol (сase-insensitive
partial search). Used debounce and abort controller mechanics for
correct search in real time. Also displaying one NFT for each collection
with an image, with simple decoded from ipfs metadata (if available)
with animations supported. GraphQL query placed on
<Text style={{ color: "#0088FF" }}>
{" "}
src/gql/SearchNFTCollection.gql
</Text>
</Text>
<TextInput
style={{
height: 40,
borderColor: "gray",
borderWidth: 1,
marginBottom: 16,
paddingHorizontal: 8,
borderRadius: 4,
}}
placeholder="NFT Symbol..."
value={query}
onChangeText={handleSearch}
/>
{loading && <Text>Loading...</Text>}
{nftCollections?.map((collection, index) => (
<NFTCollection key={index} {...collection} />
))}
</ScrollView>
);
}