import { Button } from "@/components/ui/button"; import { useCopyToClipboard } from "@/hooks/useCopyToClipboard"; import { ChevronDown, ChevronUp, Copy } from "lucide-react"; import { useEffect, useRef, useState } from "react"; interface CollapsibleBoxProps { title: string; children: React.ReactNode; collapsedHeight?: number; expandedMaxHeight?: number; onCopy?: () => string; } export default function CollapsibleBox({ title, children, collapsedHeight = 60, expandedMaxHeight = 450, onCopy }: CollapsibleBoxProps) { const [isExpanded, setIsExpanded] = useState(false); const [needsExpansion, setNeedsExpansion] = useState(false); const innerContentRef = useRef(null); const { copy } = useCopyToClipboard(); useEffect(() => { if (!innerContentRef.current) return; const checkHeight = () => { const scrollHeight = innerContentRef.current?.scrollHeight || 0; setNeedsExpansion(scrollHeight > collapsedHeight); }; // Initial check after a small delay to allow content to render const timeoutId = setTimeout(checkHeight, 50); // Observe for resize changes const observer = new ResizeObserver(checkHeight); observer.observe(innerContentRef.current); return () => { clearTimeout(timeoutId); observer.disconnect(); }; }, [children, collapsedHeight]); const handleCopy = () => { if (!onCopy) return; copy(onCopy()); }; return (
{title}
{onCopy && ( )}
{children}
{needsExpansion && ( )}
); }