-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens-transfers-feed.tsx
74 lines (69 loc) · 2.01 KB
/
tokens-transfers-feed.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
import { Alert, Button, ScrollView, Text } from "react-native";
import { useState, useEffect } from "react";
import hgraphClient from "@/src/utils/hgraph-client";
import { RecentTokensTransfers } from "@/src/gql";
import { TokenTransfer } from "@/src/components";
import { ObservableSubscription } from "@hgraph.io/sdk";
import { TokenTransferRecord } from "@/src/types";
export default function TokensTransfersFeed() {
const [transfers, setTransfers] = useState<TokenTransferRecord[]>([]);
const [subscription, setSubscription] = useState<ObservableSubscription>();
useEffect(() => {
return () => {
hgraphClient.removeAllSubscriptions();
};
}, []);
const toggleSubscription = () => {
if (subscription) {
subscription.unsubscribe();
return;
}
setSubscription(
hgraphClient.subscribe(RecentTokensTransfers, {
// handle the data
next: ({ data }: any) => {
setTransfers(data.transfers ?? []);
},
error: (errors) => {
Alert.alert(
"Subscription error",
errors.map((error: Error) => error.message).join("\n"),
);
console.error(errors);
setSubscription(undefined);
},
complete: () => {
setSubscription(undefined);
},
}),
);
};
return (
<ScrollView
style={{
flex: 1,
}}
contentContainerStyle={{
rowGap: 10,
margin: 15,
}}
>
<Text>
Example of subscribing and displaying recent transfers for fungible
tokens as a live feed using @hgraph.io/sdk. GraphQL subscription placed
on
<Text style={{ color: "#0088FF" }}>
{" "}
src/gql/RecentTokensTransfers.gql
</Text>
</Text>
<Button
onPress={toggleSubscription}
title={subscription ? "Stop live feed" : "Start live feed"}
/>
{transfers?.map((transfer, index) => (
<TokenTransfer key={index} {...transfer} />
))}
</ScrollView>
);
}