'use client' import { useState } from 'react' import { Button } from '@/components/ui/button' import { useToast } from '@/components/ui/use-toast' import type { BillingPlan } from '@/lib/stripe/client' /** * Client CTA for the billing page. Active subscribers get the Stripe Customer * Portal (manage/cancel); everyone else gets a plan toggle + Checkout. Both * POST to a route that returns a hosted Stripe URL we redirect to. */ export function BillingActions({ isActive, configured }: { isActive: boolean; configured: boolean }) { const { toast } = useToast() const [loading, setLoading] = useState(false) const [plan, setPlan] = useState('yearly') async function go(endpoint: string, payload?: Record) { setLoading(true) try { const res = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload ?? {}), }) const data = (await res.json().catch(() => ({}))) as { url?: string; error?: string } if (!res.ok || !data.url) throw new Error(data.error || 'Något gick fel') window.location.href = data.url } catch (e) { toast({ title: 'Kunde inte öppna betalningen', description: e instanceof Error ? e.message : undefined, variant: 'destructive', }) setLoading(false) } } if (isActive) { return ( ) } if (!configured) { return ( ) } return (
{(['monthly', 'yearly'] as BillingPlan[]).map((p) => ( ))}
) }