'use client' import { useState } from 'react' import { generateSlug } from '@/app/lib/slug' interface ClickableHeadingProps { level: 1 | 2 | 3 | 4 children: React.ReactNode } export function ClickableHeading({ level, children }: ClickableHeadingProps) { const [copied, setCopied] = useState(false) const text = typeof children === 'string' ? children : extractText(children) const id = generateSlug(text) const handleClick = async () => { const url = `${window.location.origin}${window.location.pathname}#${id}` await navigator.clipboard.writeText(url) setCopied(true) // Update URL without scrolling window.history.pushState({}, '', `#${id}`) setTimeout(() => setCopied(false), 2000) } const Tag = `h${level}` as keyof React.JSX.IntrinsicElements const baseClasses = "font-display font-semibold leading-tight tracking-tighter text-base-content group cursor-pointer hover:text-muted transition-colors relative scroll-mt-32" const levelClasses = { 1: "text-3xl sm:text-4xl mb-8", 2: "text-2xl sm:text-3xl mt-12 mb-4", 3: "text-xl sm:text-2xl tracking-tight mt-8 mb-3", 4: "text-lg sm:text-xl tracking-tight mt-6 mb-2" } return ( {children} {copied ? ( ) : ( )} ) } function extractText(node: React.ReactNode): string { if (typeof node === 'string') return node if (typeof node === 'number') return String(node) if (Array.isArray(node)) return node.map(extractText).join('') if (node && typeof node === 'object' && 'props' in node) { return extractText((node as { props: { children: React.ReactNode } }).props.children) } return '' }