11import { useOrg } from "../../components/layout/org/organizationContext" ;
22import { $JAWN_API } from "../../lib/clients/jawn" ;
3+ import { useQuery } from "@tanstack/react-query" ;
34
45// Types matching the backend
56export interface PurchasedCredits {
@@ -12,8 +13,8 @@ export interface PurchasedCredits {
1213export interface PaginatedPurchasedCredits {
1314 purchases : PurchasedCredits [ ] ;
1415 total : number ;
15- page : number ;
16- pageSize : number ;
16+ nextPage : string | null ;
17+ hasMore : boolean ;
1718}
1819
1920export interface CreditBalanceResponse {
@@ -24,14 +25,14 @@ export interface CreditBalanceResponse {
2425// A hook for the user's credit balance in cents
2526export const useCredits = ( ) => {
2627 const org = useOrg ( ) ;
27-
28+
2829 const result = $JAWN_API . useQuery (
2930 "get" ,
3031 "/v1/credits/balance" ,
3132 { } ,
3233 {
3334 enabled : ! ! org ?. currentOrg ?. id ,
34- }
35+ } ,
3536 ) ;
3637
3738 return {
@@ -40,36 +41,73 @@ export const useCredits = () => {
4041 } ;
4142} ;
4243
44+ // Helper function to fetch payment intents from Stripe API
45+ const fetchStripePaymentIntents = async (
46+ limit : number ,
47+ page ?: string | null ,
48+ ) => {
49+ const queryParams = new URLSearchParams ( ) ;
50+ queryParams . set ( "limit" , limit . toString ( ) ) ;
51+ if ( page ) {
52+ queryParams . set ( "page" , page ) ;
53+ }
54+
55+ const url = `/api/stripe/payment-intents/search?${ queryParams . toString ( ) } ` ;
56+ const response = await fetch ( url ) ;
57+
58+ if ( ! response . ok ) {
59+ const errorText = await response . text ( ) ;
60+ throw new Error ( `Failed to fetch payment intents: ${ response . status } ${ errorText } ` ) ;
61+ }
62+
63+ return response . json ( ) ;
64+ } ;
65+
4366// A hook for fetching credit balance transactions with pagination
4467export const useCreditTransactions = ( params ?: {
45- page ?: number ;
46- pageSize ?: number ;
68+ limit ?: number ;
69+ page ?: string | null ;
4770} ) => {
4871 const org = useOrg ( ) ;
49-
50- const result = $JAWN_API . useQuery (
51- "get" ,
52- "/v1/credits/payments" ,
53- {
54- params : {
55- query : {
56- page : params ?. page ?? 0 ,
57- pageSize : params ?. pageSize ?? 10 ,
58- } ,
59- } ,
72+
73+ const result = useQuery ( {
74+ queryKey : [
75+ "credit-transactions" ,
76+ org ?. currentOrg ?. id ,
77+ params ?. limit ,
78+ params ?. page ,
79+ ] ,
80+ queryFn : async ( ) => {
81+ const data = await fetchStripePaymentIntents (
82+ params ?. limit ?? 10 ,
83+ params ?. page ,
84+ ) ;
85+
86+ // Transform Stripe payment intents to our format
87+ const purchases : PurchasedCredits [ ] = data . data . map ( ( intent : any ) => ( {
88+ id : intent . id ,
89+ createdAt : intent . created * 1000 , // Convert from seconds to milliseconds
90+ credits : intent . amount , // Amount is in cents
91+ referenceId : intent . id ,
92+ } ) ) ;
93+
94+ return {
95+ purchases,
96+ total : data . count || 0 ,
97+ nextPage : data . next_page || null ,
98+ hasMore : data . has_more || false ,
99+ } ;
60100 } ,
61- {
62- enabled : ! ! org ?. currentOrg ?. id ,
63- }
64- ) ;
101+ enabled : ! ! org ?. currentOrg ?. id ,
102+ } ) ;
65103
66104 return {
67105 ...result ,
68- data : result . data ?. data || {
106+ data : result . data || {
69107 purchases : [ ] ,
70108 total : 0 ,
71- page : 0 ,
72- pageSize : 10 ,
109+ nextPage : null ,
110+ hasMore : false ,
73111 } ,
74112 } ;
75113} ;
@@ -86,4 +124,4 @@ export const useCreateCheckoutSession = () => {
86124 console . error ( "Failed to create checkout session:" , error ) ;
87125 } ,
88126 } ) ;
89- } ;
127+ } ;
0 commit comments