"use client"; import React, { useState } from 'react'; import { api } from '@/lib/api-client'; import { Modal } from '@/components/ui/Modal/Modal'; import { Button } from '@/components/ui/Button/Button'; import { Input } from '@/components/ui/Input/Input'; import styles from './BuyCreditsModal.module.css'; interface BuyCreditsModalProps { isOpen: boolean; onClose: () => void; tokenPrice?: number; // Price per token/thread } export const BuyCreditsModal: React.FC = ({ isOpen, onClose, tokenPrice = 0.10 // Default fallback price }) => { const [quantity, setQuantity] = useState(100); const [isCustom, setIsCustom] = useState(false); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const packages = [ { name: 'Starter', threads: 100, price: 10, popular: false }, { name: 'Pro', threads: 500, price: 45, popular: true }, { name: 'Max', threads: 1000, price: 80, popular: false }, ]; const currentPrice = isCustom ? (quantity * tokenPrice).toFixed(2) : (packages.find(p => p.threads === quantity)?.price || (quantity * tokenPrice)).toFixed(2); const handleCheckout = async () => { setIsLoading(true); setError(null); try { const response: any = await api.post('/checkout/create-checkout-session', { body: { quantity, total_amount: parseFloat(currentPrice) } }); if (response && response.url) { window.location.href = response.url; } else { throw new Error('Invalid checkout response'); } } catch (err: any) { console.error('Checkout error:', err); setError(err.message || 'Failed to start checkout'); setIsLoading(false); } }; return (
Total: €{currentPrice}
} >
{error && (
{error}
)}

Purchase additional threads to continue your conversations.

{packages.map((pkg) => (
{ setQuantity(pkg.threads); setIsCustom(false); }} > {pkg.popular && Most Popular}
{pkg.threads}
Threads
€{pkg.price}
))}
{isCustom && ( setQuantity(Math.max(1, parseInt(e.target.value) || 0))} min={1} placeholder={`Approx. €${(quantity * tokenPrice).toFixed(2)}`} fullWidth /> )}
); };