import { Box } from '@chakra-ui/react'; import { debounce } from 'es-toolkit'; import React, { useRef, useEffect, useState, useCallback } from 'react'; const CUT_HEIGHT = 144; const AccountPageDescription = ({ children, allowCut = true }: { children: React.ReactNode; allowCut?: boolean }) => { const ref = useRef(null); const [ needCut, setNeedCut ] = useState(false); const [ expanded, setExpanded ] = useState(false); const calculateCut = useCallback(() => { const textHeight = ref.current?.offsetHeight; if (!needCut && textHeight && textHeight > CUT_HEIGHT) { setNeedCut(true); } else if (needCut && textHeight && textHeight < CUT_HEIGHT) { setNeedCut(false); } }, [ needCut ]); useEffect(() => { if (!allowCut) { return; } calculateCut(); const resizeHandler = debounce(calculateCut, 300); window.addEventListener('resize', resizeHandler); return function cleanup() { window.removeEventListener('resize', resizeHandler); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [ ]); const expand = useCallback(() => { setExpanded(true); }, []); return ( { children } { needCut && !expanded && ( ) } ); }; export default AccountPageDescription;