"use client"; import { useEffect, useState } from "react"; import { AppShell } from "../../../components/app-shell"; import { apiFetch } from "@/lib/api"; type ApiResponse = { data: T; meta: { timestamp: string; version: "v1" }; error: null | { message: string; code?: string }; }; type SubscriptionData = { plan?: string; status?: string; billingCycleAnchor?: number; cancelAtPeriodEnd?: boolean; }; const PLAN_LABELS: Record = { free: "Free", pro: "Pro", elite: "Elite", }; const PLAN_DESCRIPTIONS: Record = { free: "Up to 2 accounts, basic CSV export, 30-day history.", pro: "Unlimited accounts, Google Sheets, 24-month history, priority support.", elite: "Everything in Pro + tax return module, AI rule suggestions, dedicated support.", }; export default function SubscriptionPage() { const [sub, setSub] = useState(null); const [loading, setLoading] = useState(true); const [actionStatus, setActionStatus] = useState(""); const [actionLoading, setActionLoading] = useState(false); useEffect(() => { apiFetch("/api/stripe/subscription") .then((res) => { if (!res.error) setSub(res.data); }) .catch(() => {}) .finally(() => setLoading(false)); }, []); const handleUpgrade = async (plan: string) => { setActionLoading(true); setActionStatus("Redirecting to checkout..."); const appUrl = typeof window !== "undefined" ? window.location.origin : ""; const res = await apiFetch<{ url: string }>("/api/stripe/checkout", { method: "POST", body: JSON.stringify({ plan, successUrl: `${appUrl}/settings/subscription?upgraded=1`, cancelUrl: `${appUrl}/settings/subscription`, }), }); setActionLoading(false); if (res.error || !res.data?.url) { setActionStatus(res.error?.message ?? "Could not start checkout. Check Stripe configuration."); return; } window.location.href = res.data.url; }; const handlePortal = async () => { setActionLoading(true); setActionStatus("Redirecting to billing portal..."); const appUrl = typeof window !== "undefined" ? window.location.origin : ""; const res = await apiFetch<{ url: string }>("/api/stripe/portal", { method: "POST", body: JSON.stringify({ returnUrl: `${appUrl}/settings/subscription` }), }); setActionLoading(false); if (res.error || !res.data?.url) { setActionStatus(res.error?.message ?? "Could not open billing portal. Check Stripe configuration."); return; } window.location.href = res.data.url; }; const currentPlan = sub?.plan ?? "free"; const planLabel = PLAN_LABELS[currentPlan] ?? currentPlan; const planDesc = PLAN_DESCRIPTIONS[currentPlan] ?? ""; return (
{/* Current plan card */}

Current Plan

{loading ? (
) : ( <>
{planLabel} {sub?.status && sub.status !== "active" && sub.status !== "free" && ( {sub.status} )} {(sub?.status === "active" || currentPlan !== "free") && ( Active )}

{planDesc}

{sub?.cancelAtPeriodEnd && (

Cancels at end of billing period.

)} )} {!loading && currentPlan !== "free" && ( )}
{/* Upgrade options */} {currentPlan === "free" && (
{(["pro", "elite"] as const).map((plan) => (

{plan}

{PLAN_DESCRIPTIONS[plan]}

))}
)} {currentPlan === "pro" && (

Elite

{PLAN_DESCRIPTIONS.elite}

)} {actionStatus && (

{actionStatus}

)}
); }