forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseDashboard.ts
More file actions
57 lines (52 loc) · 1.58 KB
/
Copy pathuseDashboard.ts
File metadata and controls
57 lines (52 loc) · 1.58 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
import { useQuery } from "@tanstack/react-query";
import { useWallet } from "./useWallet";
import { browserStellarConfig } from "@/lib/stellar/browserConfig";
import { getPromptsByCreator, getPromptsByBuyer } from "@/lib/stellar/promptHashClient";
export const useDashboard = () => {
const { address } = useWallet();
const createdQuery = useQuery({
queryKey: ["created-prompts", address],
queryFn: async () => {
if (!address) return [];
return getPromptsByCreator(browserStellarConfig, address);
},
enabled: Boolean(address),
retry: 1,
});
const purchasedQuery = useQuery({
queryKey: ["purchased-prompts", address],
queryFn: async () => {
if (!address) return [];
return getPromptsByBuyer(browserStellarConfig, address);
},
enabled: Boolean(address),
retry: 1,
});
const isLoading = createdQuery.isLoading || purchasedQuery.isLoading;
const isError = createdQuery.isError || purchasedQuery.isError;
const isPartialError = createdQuery.isError !== purchasedQuery.isError;
return {
address,
created: {
data: createdQuery.data ?? [],
isLoading: createdQuery.isLoading,
isError: createdQuery.isError,
error: createdQuery.error,
},
purchased: {
data: purchasedQuery.data ?? [],
isLoading: purchasedQuery.isLoading,
isError: purchasedQuery.isError,
error: purchasedQuery.error,
},
isLoading,
isError,
isPartialError,
refresh: async () => {
await Promise.all([
createdQuery.refetch(),
purchasedQuery.refetch(),
]);
},
};
};