forked from StellarSend/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseTransactions.ts
More file actions
69 lines (58 loc) · 2.84 KB
/
Copy pathuseTransactions.ts
File metadata and controls
69 lines (58 loc) · 2.84 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
import { useQuery, useInfiniteQuery, useQueryClient } from '@tanstack/react-query'
import { fetchTransactionsFromHorizon } from '@/lib/api'
import { useWallet } from './useWallet'
import type { TransactionFilters, TransactionPage } from '@/types'
// ─── Query keys ───────────────────────────────────────────────────────────────
export const txKeys = {
all: ['transactions'] as const,
// `limit` must be part of the key: useRecentTransactions' queryFn depends
// on it, and without it here every call for the same wallet — regardless
// of what limit was requested — collides on one shared cache entry (#51).
list: (pubKey: string, limit: number, filters?: TransactionFilters) =>
[...txKeys.all, 'list', pubKey, limit, filters] as const,
infinite: (pubKey: string, filters?: TransactionFilters) =>
[...txKeys.all, 'infinite', pubKey, filters] as const,
detail: (hash: string) => [...txKeys.all, 'detail', hash] as const,
}
// ─── Recent transactions (first page) ────────────────────────────────────────
export function useRecentTransactions(limit = 5) {
const { publicKey, network, isConnected } = useWallet()
return useQuery<TransactionPage, Error>({
queryKey: txKeys.list(publicKey ?? '', limit, { direction: undefined }),
queryFn: () =>
fetchTransactionsFromHorizon(publicKey!, network, limit),
enabled: isConnected && !!publicKey,
staleTime: 30_000,
refetchInterval: 30_000,
retry: 2,
})
}
// ─── Infinite / paginated transactions ───────────────────────────────────────
export function useTransactions(filters?: TransactionFilters) {
const { publicKey, network, isConnected } = useWallet()
return useInfiniteQuery<TransactionPage, Error>({
queryKey: txKeys.infinite(publicKey ?? '', filters),
queryFn: ({ pageParam }) =>
fetchTransactionsFromHorizon(
publicKey!,
network,
20,
typeof pageParam === 'string' ? pageParam : undefined,
),
getNextPageParam: (lastPage) =>
lastPage.hasMore ? lastPage.cursor : undefined,
initialPageParam: undefined as string | undefined,
enabled: isConnected && !!publicKey,
staleTime: 30_000,
retry: 2,
})
}
// ─── Invalidate / refetch helper ──────────────────────────────────────────────
export function useInvalidateTransactions() {
const queryClient = useQueryClient()
const { publicKey } = useWallet()
return () => {
if (!publicKey) return
queryClient.invalidateQueries({ queryKey: txKeys.all })
}
}