'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { GitFork, Loader2 } from 'lucide-react'; interface ForkButtonProps { templateId: string; className?: string; } export function ForkButton({ templateId, className = '' }: ForkButtonProps) { const [loading, setLoading] = useState(false); const router = useRouter(); const handleFork = async () => { if (loading) return; setLoading(true); try { const res = await fetch(`/api/marketplace/${templateId}/fork`, { method: 'POST', }); if (!res.ok) { const body = await res.json().catch(() => ({})); throw new Error(body.error || `Fork failed (${res.status})`); } const { data } = await res.json(); router.push(`/projects/${data.id}`); } catch (err) { // Toast notification const message = err instanceof Error ? err.message : 'Failed to fork template'; if (typeof window !== 'undefined') { // Try using a global toast if available, fallback to alert const toastEvent = new CustomEvent('toast', { detail: { message, type: 'error' }, }); window.dispatchEvent(toastEvent); // Fallback for now console.error('[ForkButton]', message); } } finally { setLoading(false); } }; return ( ); }