import { useState, useCallback } from 'react'; import { apiUrl, authHeaders, assertOk } from '../lib/api'; export function useLicense() { const [checkingOut, setCheckingOut] = useState(false); const [error, setError] = useState(null); const startCheckout = useCallback(async (referralCode?: string) => { setCheckingOut(true); setError(null); try { const body: Record = {}; if (referralCode) body.referral_code = referralCode; const res = await fetch(apiUrl('checkout'), { method: 'POST', ...authHeaders({ headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }), }); assertOk(res, 'Checkout'); const data = await res.json(); if (data.url) { window.location.href = data.url; } } catch (err) { const msg = err instanceof Error ? err.message : 'Checkout failed'; setError(msg); throw err; } finally { setCheckingOut(false); } }, []); return { startCheckout, checkingOut, error }; }