import React, { useState, useEffect } from "react" import { useRouter } from "next/router" interface CopyPageButtonProps { className?: string } export function CopyPageButton({ className }: CopyPageButtonProps) { const router = useRouter() const [copied, setCopied] = useState(false) const [error, setError] = useState(false) const [isLoading, setIsLoading] = useState(false) // Reset copied/error state after 3 seconds useEffect(() => { if (copied || error) { const timer = setTimeout(() => { setCopied(false) setError(false) }, 3000) return () => clearTimeout(timer) } }, [copied, error]) const handleCopy = async () => { if (copied || error || isLoading) return setIsLoading(true) try { // Fetch the raw markdown file based on current route // The route path maps to pages/.md const path = router.asPath.split("#")[0].split("?")[0] // Remove hash and query params const mdPath = path === "/" ? "/index" : path // Fetch the raw markdown content from the API route const response = await fetch(`/docs/api/raw-markdown?path=${encodeURIComponent(mdPath)}`) if (!response.ok) { throw new Error("Failed to fetch markdown") } const markdown = await response.text() await navigator.clipboard.writeText(markdown) setCopied(true) } catch (err) { console.error("Failed to copy page:", err) setError(true) } finally { setIsLoading(false) } } return ( <> ) } // Copy icon (two overlapping rectangles) function CopyIcon() { return ( ) } // Check icon for copied state function CheckIcon() { return ( ) }