import { useBilling } from '@flowglad/react'
import { useState } from 'react'
export function UpgradeButton({ priceId }: { priceId: string }) {
const { createCheckoutSession } = useBilling()
const [isLoading, setIsLoading] = useState(false)
const handleUpgrade = async () => {
setIsLoading(true)
try {
await createCheckoutSession({
priceId,
successUrl: `${window.location.origin}/billing/success`,
cancelUrl: window.location.href,
autoRedirect: true,
})
} catch (error) {
console.error('Upgrade failed:', error)
setIsLoading(false)
}
}
return (
<button onClick={handleUpgrade} disabled={isLoading}>
{isLoading ? 'Loading...' : 'Upgrade'}
</button>
)
}