diff --git a/packages/frontend/src/components/VariablesList/index.tsx b/packages/frontend/src/components/VariablesList/index.tsx index 87e63c2d30..0021fc5de5 100644 --- a/packages/frontend/src/components/VariablesList/index.tsx +++ b/packages/frontend/src/components/VariablesList/index.tsx @@ -1,6 +1,6 @@ import { TDataOutMetadatumType } from '@plumber/types' -import { useMemo, useRef } from 'react' +import { useEffect, useMemo, useRef, useState } from 'react' import { IconType } from 'react-icons/lib' import { Accordion, @@ -92,12 +92,29 @@ export function VariableItem({ isLast?: boolean withIcon?: IconType }): JSX.Element { + const [isExpanded, setIsExpanded] = useState(false) + const [isOverflowing, setIsOverflowing] = useState(false) + const textRef = useRef(null) const shouldShowBottomBorder = !withIcon && (onClick || isLast) const displayValue = variable.displayedValue ?? variable.value?.toString() ?? '' const isSuggestionVariable = onClick && !withIcon + + // Check if text is overflowing after render + useEffect(() => { + if (variable.type === 'ai_response' && textRef.current && !isExpanded) { + const element = textRef.current + const isTextOverflowing = + element.scrollHeight > element.clientHeight || + element.scrollWidth > element.clientWidth + setIsOverflowing(isTextOverflowing) + } + }, [variable.type, isExpanded]) + + const shouldShowToggle = variable.type === 'ai_response' && isOverflowing + return ( {variable.label ?? variable.name} - - - {displayValue.length ? ( - displayValue - ) : ( - empty - )} - - {withIcon && } + + + + {displayValue.length ? ( + displayValue + ) : ( + empty + )} + + {withIcon && } + + {shouldShowToggle && ( + { + e.stopPropagation() + setIsExpanded(!isExpanded) + }} + onMouseDown={(e) => { + e.stopPropagation() + }} + > + {isExpanded ? 'Show less' : 'Show more'} + + )} ) @@ -207,6 +248,7 @@ export default function VariablesList(props: VariablesListProps) { getScrollElement: () => parentRef.current, estimateSize: () => onClick ? SUGGESTION_VARIABLE_ITEM_HEIGHT : VARIABLE_ITEM_HEIGHT, + measureElement: (element) => element.getBoundingClientRect().height, overscan: 50, }) @@ -232,6 +274,7 @@ export default function VariablesList(props: VariablesListProps) {