Files
openhuman/lander/app/hooks/useCoinbaseCheckout.ts
T
Steven Enamakel c613a391cf feat: initialize Next.js landing page with essential configurations and components
- Added .gitignore to exclude unnecessary files and directories.
- Created ESLint configuration for code quality and consistency.
- Set up Next.js configuration file for project settings.
- Initialized package.json with dependencies and scripts for development.
- Configured PostCSS for Tailwind CSS integration.
- Added global styles and layout components for the landing page.
- Implemented core components including AnimatedBackground, AnimatedLogo, and Feedback features.
- Established feedback management system with modals and kanban board for user interactions.
- Included README for project setup and usage instructions.
2026-03-19 10:36:44 -07:00

69 lines
1.8 KiB
TypeScript

"use client";
import { useState, useCallback } from "react";
import { createCoinbaseCharge } from "@/lib/payments";
import type { CoinbasePlanType } from "@/lib/types";
interface UseCoinbaseCheckoutReturn {
handleCheckout: (planType: "basic" | "pro") => Promise<void>;
loading: boolean;
error: string | null;
}
/**
* Hook for handling Coinbase Commerce checkout flow.
*
* Provides a callback function to initiate Coinbase checkout for a given plan type.
* Manages loading state and error handling.
*
* @returns Object containing checkout handler, loading state, and error state
*/
export function useCoinbaseCheckout(): UseCoinbaseCheckoutReturn {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const handleCheckout = useCallback(async (planType: "basic" | "pro") => {
setLoading(true);
setError(null);
try {
// Map UI plan types to API plan names
const planMap: Record<"basic" | "pro", CoinbasePlanType> = {
basic: "BASIC",
pro: "PRO",
};
const planName = planMap[planType];
const response = await createCoinbaseCharge({
plan: planName,
currency: "USD",
});
if (response.success && response.data?.hostedUrl) {
window.location.href = response.data.hostedUrl;
} else {
throw new Error("No hosted URL received from server");
}
} catch (err) {
const errorMessage =
err instanceof Error
? err.message
: "Unable to start crypto checkout. Please try again later.";
setError(errorMessage);
console.error("Failed to create Coinbase charge:", err);
// Show user-friendly error message
alert(errorMessage);
} finally {
setLoading(false);
}
}, []);
return {
handleCheckout,
loading,
error,
};
}