"use client"; import Link from "next/link"; import { useState } from "react"; import { apiFetch } from "@/lib/api"; type PricingActionProps = { plan: "free" | "pro" | "elite"; label: string; primary: boolean; }; export function PricingAction({ plan, label, primary }: PricingActionProps) { const [status, setStatus] = useState(""); const [loading, setLoading] = useState(false); const className = `mt-8 block w-full text-center ${primary ? "btn-primary" : "btn-secondary"} disabled:opacity-60`; if (plan === "free") { return ( {label} ); } const startCheckout = async () => { setLoading(true); setStatus("Opening checkout..."); const appUrl = window.location.origin; const response = await apiFetch<{ url: string }>("/api/billing/checkout", { method: "POST", body: JSON.stringify({ plan, successUrl: `${appUrl}/settings/subscription?upgraded=1`, cancelUrl: `${appUrl}/pricing`, }), }); setLoading(false); if (response.error || !response.data?.url) { setStatus(response.error?.message ?? "Sign in before choosing a paid plan."); return; } window.location.href = response.data.url; }; return ( <> {status ?

{status}

: null} ); }