import { useState } from 'react'; import { supabase } from '@app/auth/supabase'; import { Button } from '@mantine/core'; import { usePlans } from '@app/hooks/usePlans'; interface TrialStatus { isTrialing: boolean; trialEnd: string; daysRemaining: number; hasPaymentMethod: boolean; hasScheduledSub: boolean; } export function ManageBillingButton({ returnUrl = typeof window !== 'undefined' ? window.location.href : '/', children = 'Manage billing', trialStatus, }: { returnUrl?: string; children?: React.ReactNode; trialStatus?: TrialStatus; }) { const [loading, setLoading] = useState(false); const [err, setErr] = useState(null); const { data } = usePlans(); // Hide for free plan users if (!data || data.currentPlan.id === 'free') { return null; } // Hide for trial users who haven't scheduled a subscription yet if (trialStatus?.isTrialing && !trialStatus.hasScheduledSub) { return null; } const onClick = async () => { setLoading(true); setErr(null); try { const { data, error } = await supabase.functions.invoke('manage-billing', { body: { name: 'Functions', return_url: returnUrl}, }) if (error) throw error; if (!data || 'error' in data) throw new Error((data as any)?.error ?? 'No portal URL'); window.location.href = (data as any).url; } catch (e: any) { setErr(e.message ?? 'Could not open billing portal'); } finally { setLoading(false); } }; return (
{err &&
{err}
}
); }