-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseGetUserEvents.tsx
More file actions
77 lines (67 loc) · 2.24 KB
/
useGetUserEvents.tsx
File metadata and controls
77 lines (67 loc) · 2.24 KB
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
import getEvents from "@/constants/subgraph";
import { chain, client } from "@/constants/thirdweb";
import { Event } from "@/types/event";
import { useQuery } from "@tanstack/react-query";
import { getContract, readContract } from "thirdweb";
import { useActiveAccount } from "thirdweb/react";
export interface EventWithTickets {
event: Event;
userTickets: readonly bigint[];
hasTickets: boolean;
ticketCount: number;
}
const useGetUserEvents = () => {
const account = useActiveAccount();
const { data, isLoading, error, refetch } = useQuery({
queryKey: ["userEvents", account?.address],
queryFn: async () => {
if (!account) {
throw new Error("No wallet connected");
}
const events = await getEvents();
const eventsWithTickets: EventWithTickets[] = [];
for (const event of events.eventCreateds) {
try {
const eventContract = getContract({
client: client,
address: event.eventAddress,
chain: chain,
});
const userTickets = await readContract({
contract: eventContract,
method:
"function getUserTickets(address user) external view returns (uint256[] memory)",
params: [account.address],
});
const ticketCount = userTickets ? userTickets.length : 0;
const hasTickets = ticketCount > 0;
// Only add events where user has tickets
if (hasTickets) {
eventsWithTickets.push({
event,
userTickets: userTickets || [],
hasTickets,
ticketCount,
});
}
} catch (error) {
console.error(
`Error fetching tickets for event ${event.eventAddress}:`,
error
);
// Don't add events with errors since they have no tickets
}
}
console.log("events with tickets", eventsWithTickets.length);
return {
events: eventsWithTickets,
totalEvents: eventsWithTickets.length,
};
},
enabled: !!account?.address,
staleTime: 30 * 1000, // 30 seconds
gcTime: 5 * 60 * 1000, // 5 minutes
});
return { data, isLoading, error, refetch };
};
export default useGetUserEvents;