'use client'; import { useEffect, useState } from 'react'; import { Button } from "@/components/ui/button"; import { PlanComparison } from "@/components/billing/plan-comparison"; import { isLocalMode } from "@/lib/config"; import { getSubscription, createPortalSession, SubscriptionStatus } from "@/lib/api"; import { useAuth } from "@/components/AuthProvider"; import { Skeleton } from "@/components/ui/skeleton"; type Props = { accountId: string; returnUrl: string; } export default function AccountBillingStatus({ accountId, returnUrl }: Props) { 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 (authLoading || !session) return; try { 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(); }, [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); } }; // In local development mode, show a simplified component if (isLocalMode()) { return (

Billing Status

Running in local development mode - billing features are disabled

Agent usage limits are not enforced in this environment

); } // Show loading state if (isLoading || authLoading) { return (

Billing Status

); } // Show error state if (error) { return (

Billing Status

Error loading billing status: {error}

); } const isPlan = (planId?: string) => { return subscriptionData?.plan_name === planId; }; const planName = isPlan('free') ? "Free" : isPlan('base') ? "Pro" : isPlan('extra') ? "Enterprise" : "Unknown"; return (

Billing Status

{subscriptionData ? ( <>
Agent Usage This Month {subscriptionData.current_usage?.toFixed(2) || '0'} / {subscriptionData.minutes_limit || '0'} minutes
{/* Plans Comparison */} {/* Manage Subscription Button */} ) : ( <>
Current Plan Free
Agent Usage This Month {subscriptionData?.current_usage?.toFixed(2) || '0'} / {subscriptionData?.minutes_limit || '0'} minutes
{/* Plans Comparison */} {/* Manage Subscription Button */} )}
); }