import { Button } from '@/components/ui/button' import { cn } from '@/lib/utils' import { useEffect, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' export default function Collapsible({ alwaysExpand = false, children, className, threshold = 1000, collapsedHeight = 600, ...props }: { alwaysExpand?: boolean threshold?: number collapsedHeight?: number } & React.HTMLProps) { const { t } = useTranslation() const containerRef = useRef(null) const [expanded, setExpanded] = useState(false) const [shouldCollapse, setShouldCollapse] = useState(false) useEffect(() => { if (alwaysExpand || shouldCollapse) return const contentEl = containerRef.current if (!contentEl) return const checkHeight = () => { const fullHeight = contentEl.scrollHeight if (fullHeight > threshold) { setShouldCollapse(true) } } checkHeight() const observer = new ResizeObserver(() => { checkHeight() }) observer.observe(contentEl) return () => { observer.disconnect() } }, [alwaysExpand, shouldCollapse]) return (
{children} {shouldCollapse && !expanded && (
)}
) }