'use client'; import { useEffect, useState } from 'react'; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { PricingSection } from '@/components/home/sections/pricing-section'; import { isLocalMode } from '@/lib/config'; import { getSubscription, createPortalSession, SubscriptionStatus, } from '@/lib/api'; import { useAuth } from '@/components/AuthProvider'; import { Skeleton } from '@/components/ui/skeleton'; import { X } from 'lucide-react'; interface BillingModalProps { open: boolean; onOpenChange: (open: boolean) => void; returnUrl?: string; } export function BillingModal({ open, onOpenChange, returnUrl = window?.location?.href || '/' }: BillingModalProps) { const { session, isLoading: authLoading } = useAuth(); const [subscriptionData, setSubscriptionData] = useState(null); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [isManaging, setIsManaging] = useState(false); useEffect(() => { async function fetchSubscription() { if (!open || authLoading || !session) return; try { setIsLoading(true); const data = await getSubscription(); setSubscriptionData(data); setError(null); } catch (err) { console.error('Failed to get subscription:', err); setError(err instanceof Error ? err.message : 'Failed to load subscription data'); } finally { setIsLoading(false); } } fetchSubscription(); }, [open, session, authLoading]); const handleManageSubscription = async () => { try { setIsManaging(true); const { url } = await createPortalSession({ return_url: returnUrl }); window.location.href = url; } catch (err) { console.error('Failed to create portal session:', err); setError(err instanceof Error ? err.message : 'Failed to create portal session'); } finally { setIsManaging(false); } }; // Local mode content if (isLocalMode()) { return ( Billing & Subscription

Running in local development mode - billing features are disabled

All premium features are available in this environment

); } return ( Upgrade Your Plan {isLoading || authLoading ? (
) : error ? (

Error loading billing status: {error}

) : ( <> {subscriptionData && (
Agent Usage This Month {subscriptionData.current_usage?.toFixed(2) || '0'} /{' '} {subscriptionData.minutes_limit || '0'} minutes
)} {subscriptionData && ( )} )}
); }