import React, { useState, useEffect, useCallback } from 'react'; import { Loader2, CreditCard, LogOut, Plus } from 'lucide-react'; import { useAuth } from '../contexts/AuthContext'; import { apiJson } from '../lib/api'; import { track } from '../lib/analytics'; const fmt1 = (n) => Math.round((n || 0) * 10) / 10; // Mirrors cloud/config.BILLING_ATTENTION_STATES: states where the customer has // to do something (fix a card, finish a payment) before minutes come back. const PAYMENT_ISSUE_STATES = ['past_due', 'unpaid', 'incomplete', 'paused']; // Account/billing page: plan, usage meter, top-ups, manage billing, logout. export default function AccountPage() { const { me, refreshMe, logout, plan, minutes } = useAuth(); const [busy, setBusy] = useState(false); const [topups, setTopups] = useState([]); const [activating, setActivating] = useState(false); // After returning from Checkout the webhook may lag — poll /api/me briefly. useEffect(() => { const hash = window.location.hash || ''; if (!hash.includes('checkout=success')) return; setActivating(true); let tries = 0; const t = setInterval(async () => { tries += 1; const data = await refreshMe(); // 'free' is the default plan for any signed-in account, so it does NOT // mean the checkout landed — keep polling until the webhook writes the // paid subscription. const paidPlan = data?.plan && data.plan !== 'free'; if (paidPlan || tries > 15) { clearInterval(t); setActivating(false); // Fire the Subscribed conversion goal. A pending-checkout stash (set in // PricingSection) carries the plan price, so we can attach real revenue; // top-ups don't set it, so they never count as a subscription. if (paidPlan) { let pending = null; try { pending = JSON.parse(localStorage.getItem('os_pending_checkout') || 'null'); } catch (_) { /* ignore */ } if (pending) { // This Plausible is Community Edition, which has no revenue goals — // so the price rides along as plain props (value_usd / plan) that CE // can break the goal down by. The exact MRR still lives in Stripe. track('Subscribed', { props: { plan: pending.plan, interval: pending.interval, value_usd: Math.round((pending.amount || 0) / 100), }, }); try { localStorage.removeItem('os_pending_checkout'); } catch (_) { /* ignore */ } } } // First time a plan activates, take the user straight to connect their // socials. Guard with a flag so top-up checkouts don't re-trigger it. if (paidPlan && !localStorage.getItem('os_socials_prompted')) { localStorage.setItem('os_socials_prompted', '1'); try { const { access_url } = await apiJson('/api/social/connect', { method: 'POST' }); if (access_url) { window.location.href = access_url; return; } } catch (_) { /* fall through to the account page */ } } window.location.hash = '#/account'; } }, 2000); return () => clearInterval(t); }, [refreshMe]); useEffect(() => { apiJson('/api/billing/plans').then((d) => setTopups(d.topups || [])).catch(() => {}); }, []); const openPortal = useCallback(async () => { setBusy(true); try { const { url } = await apiJson('/api/billing/portal', { method: 'POST' }); window.location.href = url; } catch (e) { setBusy(false); alert('Could not open billing portal.'); } }, []); const buyTopup = useCallback(async (price_id) => { setBusy(true); try { const { url } = await apiJson('/api/billing/checkout', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ price_id }), }); window.location.href = url; } catch (e) { setBusy(false); alert(e?.detail || 'Could not start checkout.'); } }, []); if (!me) return
ACCOUNT
{me.user?.email}
Top-ups never expire while your plan is active.