import { cn } from "@/app/_utils/cn"; import { HTMLAttributes, forwardRef, useState } from "react"; export interface TruncatedTextProps extends HTMLAttributes { text: string; maxLength?: number; showTooltip?: boolean; className?: string; } const TruncatedText = forwardRef( ({ className, text, maxLength = 50, showTooltip = true, ...props }, ref) => { const [showTooltipState, setShowTooltipState] = useState(false); const shouldTruncate = text.length > maxLength; const displayText = shouldTruncate ? `${text.slice(0, maxLength)}...` : text; return (
shouldTruncate && showTooltip && setShowTooltipState(true) } onMouseLeave={() => setShowTooltipState(false)} {...props} >
{displayText}
{showTooltip && shouldTruncate && showTooltipState && (
{text}
)}
); } ); TruncatedText.displayName = "TruncatedText"; export { TruncatedText };