'use client'; import { useState } from 'react'; import { useParams } from 'next/navigation'; import { tools } from '@/lib/tools'; import ToolForm from '@/components/ToolForm'; import OutputPanel from '@/components/OutputPanel'; import { ToolName } from '@/lib/types'; export default function ToolPage() { const params = useParams(); const slug = params.tool as string; const tool = tools.find((t) => t.slug === slug); const [sessionId, setSessionId] = useState(null); const [preview, setPreview] = useState(null); const [fullOutput, setFullOutput] = useState(null); const [isPaid, setIsPaid] = useState(false); const [generating, setGenerating] = useState(false); const [paymentLoading, setPaymentLoading] = useState(false); const [email, setEmail] = useState(''); const [error, setError] = useState(null); if (!tool) { return (

Tool not found

); } const handleGenerate = async (input: Record) => { setGenerating(true); setPreview(null); setSessionId(null); setError(null); try { const res = await fetch(`/api/${slug}/generate-draft`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ input, email: email || undefined }), }); const data = await res.json(); if (!res.ok) { setError(data.error || `Server error: ${res.status}`); return; } setSessionId(data.session_id); setPreview(data.preview); setIsPaid(data.is_paid); if (data.full_output) { setFullOutput(data.full_output); } } catch (e) { console.error('Generate failed:', e); setError(e instanceof Error ? e.message : 'An unexpected error occurred. Please try again.'); } finally { setGenerating(false); } }; const handlePayment = async () => { if (!sessionId) return; setPaymentLoading(true); try { const res = await fetch('/api/payment/create-checkout', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ session_id: sessionId, email }), }); const data = await res.json(); if (data.checkout_url) { window.location.href = data.checkout_url; } } catch (e) { console.error('Payment failed:', e); } finally { setPaymentLoading(false); } }; // Check if returning from payment (poll session status) const checkPaymentStatus = async () => { if (!sessionId) return; const res = await fetch(`/api/${slug}/validate`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ session_id: sessionId }), }); if (res.status !== 402) { setIsPaid(true); // Fetch full output const exportRes = await fetch('/api/export/copy', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ session_id: sessionId }), }); const data = await exportRes.json(); if (data.text) setFullOutput(data.text); } }; return (

{tool.icon} {tool.name}

{tool.description}

{/* Left: Form */}
setEmail(e.target.value)} placeholder="your@email.com" className="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-accent/50 focus:border-accent" />
{/* Right: Output */}
{error && (
Error: {error}
)} {sessionId && !isPaid && ( )}
); }