forked from StellarSend/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusePaymentRequests.ts
More file actions
68 lines (54 loc) · 2.75 KB
/
Copy pathusePaymentRequests.ts
File metadata and controls
68 lines (54 loc) · 2.75 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
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { paymentRequestApi } from '@/lib/api'
import { useWallet } from './useWallet'
import type { CreatePaymentRequestPayload, PaymentRequest } from '@/types'
export const paymentRequestKeys = {
all: ['payment-requests'] as const,
list: (pubKey: string) => [...paymentRequestKeys.all, 'list', pubKey] as const,
detail: (id: string) => [...paymentRequestKeys.all, 'detail', id] as const,
}
// ─── List requests created by the current wallet ─────────────────────────────
export function usePaymentRequestList() {
const { publicKey, isConnected } = useWallet()
return useQuery<PaymentRequest[], Error>({
queryKey: paymentRequestKeys.list(publicKey ?? ''),
queryFn: () => paymentRequestApi.list(publicKey!),
enabled: isConnected && !!publicKey,
staleTime: 30_000,
})
}
// ─── Fetch a single request by id (used by the "pay this request" view) ──────
export function usePaymentRequest(requestId: string | undefined) {
return useQuery<PaymentRequest, Error>({
queryKey: paymentRequestKeys.detail(requestId ?? ''),
queryFn: () => paymentRequestApi.get(requestId!),
enabled: !!requestId,
staleTime: 15_000,
retry: 1,
})
}
// ─── Create ───────────────────────────────────────────────────────────────────
export function useCreatePaymentRequest() {
const queryClient = useQueryClient()
return useMutation<PaymentRequest, Error, CreatePaymentRequestPayload>({
mutationFn: (payload) => paymentRequestApi.create(payload),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: paymentRequestKeys.all })
},
})
}
// ─── Cancel ───────────────────────────────────────────────────────────────────
export function useCancelPaymentRequest() {
const queryClient = useQueryClient()
return useMutation<PaymentRequest, Error, string>({
mutationFn: (requestId) => paymentRequestApi.cancel(requestId),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: paymentRequestKeys.all })
},
})
}
// ─── Shareable link helper ────────────────────────────────────────────────────
export function buildPaymentRequestLink(requestId: string): string {
const origin = typeof window !== 'undefined' ? window.location.origin : ''
return `${origin}/pay/${requestId}`
}