diff --git a/src/components/CreditsDialog.tsx b/src/components/CreditsDialog.tsx index 37425803..1bf6dc49 100644 --- a/src/components/CreditsDialog.tsx +++ b/src/components/CreditsDialog.tsx @@ -1,7 +1,7 @@ import { useState, useEffect } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import QRCode from 'qrcode'; -import { CreditCard, Zap, ExternalLink, RefreshCw, Check, X, Clock, AlertCircle, Copy } from 'lucide-react'; +import { CreditCard, Zap, ExternalLink, RefreshCw, Check, X, Clock, AlertCircle, Copy, RotateCcw } from 'lucide-react'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; @@ -204,6 +204,7 @@ export function CreditsDialog({ open, onOpenChange, providerId, connection }: Cr const [amount, setAmount] = useState(10); const [paymentMethod, setPaymentMethod] = useState<'stripe' | 'lightning'>('stripe'); const [lightningInvoice, setLightningInvoice] = useState(null); + const [refreshingPayments, setRefreshingPayments] = useState>(new Set()); // Query for payment history const { data: payments, isLoading: isLoadingPayments } = useQuery({ @@ -259,6 +260,75 @@ export function CreditsDialog({ open, onOpenChange, providerId, connection }: Cr }, }); + // Mutation for checking payment status + const checkPaymentMutation = useMutation({ + mutationFn: async (paymentId: string): Promise => { + const ai = createAIClient(connection, user); + return await ai.get(`/credits/payments/${paymentId}`) as Payment; + }, + onMutate: async (paymentId: string) => { + // Add this payment to the refreshing set + setRefreshingPayments(prev => new Set(prev).add(paymentId)); + }, + onSuccess: (updatedPayment) => { + // Remove from refreshing set + setRefreshingPayments(prev => { + const newSet = new Set(prev); + newSet.delete(updatedPayment.id); + return newSet; + }); + + // Update the specific payment in the cache instead of refetching + queryClient.setQueryData( + ['ai-payments', connection.nostr ? user?.pubkey ?? '' : '', providerId], + (oldPayments) => { + if (!oldPayments) return oldPayments; + return oldPayments.map(payment => + payment.id === updatedPayment.id ? updatedPayment : payment + ); + } + ); + + // If payment completed, refresh credits balance + if (updatedPayment.status === 'completed') { + queryClient.invalidateQueries({ + queryKey: ['ai-credits', connection.nostr ? user?.pubkey ?? '' : '', providerId], + }); + toast({ + title: 'Payment completed!', + description: `${formatCurrency(updatedPayment.amount)} has been added to your account.`, + }); + } else if (updatedPayment.status === 'failed') { + toast({ + title: 'Payment failed', + description: 'The payment has failed. Please try again.', + variant: 'destructive', + }); + } else if (updatedPayment.status === 'expired') { + toast({ + title: 'Payment expired', + description: 'The payment has expired. Please create a new payment.', + variant: 'destructive', + }); + } + }, + onError: (error: Error, paymentId: string) => { + // Remove from refreshing set on error + setRefreshingPayments(prev => { + const newSet = new Set(prev); + newSet.delete(paymentId); + return newSet; + }); + + const errorMessage = error?.message || 'Failed to check payment status'; + toast({ + title: 'Refresh failed', + description: errorMessage, + variant: 'destructive', + }); + }, + }); + const handleAddCredits = () => { if (!user) { toast({ @@ -450,7 +520,23 @@ export function CreditsDialog({ open, onOpenChange, providerId, connection }: Cr {/* Transaction History */}
-

Recent Transactions

+
+

Recent Transactions

+ +
{isLoadingPayments ? (
@@ -500,22 +586,36 @@ export function CreditsDialog({ open, onOpenChange, providerId, connection }: Cr
{formatDate(payment.created_at)} - {payment.status === 'pending' && payment.url && ( - + {payment.status === 'pending' && ( +
+ + {payment.url && ( + + )} +
)}