Skip to content

Commit e139f19

Browse files
committed
minor refactor, fix display on expand
1 parent f036744 commit e139f19

File tree

1 file changed

+20
-6
lines changed
  • packages/frontend/src/components/VariablesList

1 file changed

+20
-6
lines changed

packages/frontend/src/components/VariablesList/index.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TDataOutMetadatumType } from '@plumber/types'
22

3-
import { useMemo, useRef, useState } from 'react'
3+
import { useEffect, useMemo, useRef, useState } from 'react'
44
import { IconType } from 'react-icons/lib'
55
import {
66
Accordion,
@@ -25,7 +25,6 @@ import TableVariableItem from './TableVariableItem'
2525

2626
const VARIABLE_ITEM_HEIGHT = 77
2727
const SUGGESTION_VARIABLE_ITEM_HEIGHT = 61
28-
const CHARACTER_LIMIT = 100
2928

3029
function VariableTag({
3130
type,
@@ -94,15 +93,27 @@ export function VariableItem({
9493
withIcon?: IconType
9594
}): JSX.Element {
9695
const [isExpanded, setIsExpanded] = useState(false)
96+
const [isOverflowing, setIsOverflowing] = useState(false)
97+
const textRef = useRef<HTMLParagraphElement>(null)
9798
const shouldShowBottomBorder = !withIcon && (onClick || isLast)
9899

99100
const displayValue =
100101
variable.displayedValue ?? variable.value?.toString() ?? ''
101102

102103
const isSuggestionVariable = onClick && !withIcon
103104

104-
const shouldShowToggle =
105-
variable.type === 'ai_response' && displayValue.length > CHARACTER_LIMIT
105+
// Check if text is overflowing after render
106+
useEffect(() => {
107+
if (variable.type === 'ai_response' && textRef.current && !isExpanded) {
108+
const element = textRef.current
109+
const isTextOverflowing =
110+
element.scrollHeight > element.clientHeight ||
111+
element.scrollWidth > element.clientWidth
112+
setIsOverflowing(isTextOverflowing)
113+
}
114+
}, [variable.type, isExpanded])
115+
116+
const shouldShowToggle = variable.type === 'ai_response' && isOverflowing
106117

107118
return (
108119
<Box
@@ -119,10 +130,10 @@ export function VariableItem({
119130
isSuggestionVariable
120131
? SUGGESTION_VARIABLE_ITEM_HEIGHT
121132
: shouldShowToggle
122-
? 'auto'
133+
? undefined
123134
: VARIABLE_ITEM_HEIGHT
124135
}
125-
overflowY="hidden"
136+
overflowY={shouldShowToggle && isExpanded ? 'visible' : 'hidden'}
126137
padding={isSuggestionVariable ? '0.5rem 1rem' : '1rem'}
127138
borderBottom={shouldShowBottomBorder ? undefined : '1px solid #EDEDED'}
128139
_hover={
@@ -162,6 +173,7 @@ export function VariableItem({
162173
<Flex flexDirection="column" gap={1}>
163174
<Flex alignItems="center" gap={2}>
164175
<Text
176+
ref={textRef}
165177
textStyle="body-2"
166178
color="base.content.medium"
167179
whiteSpace={isExpanded ? 'pre-wrap' : 'nowrap'}
@@ -236,6 +248,7 @@ export default function VariablesList(props: VariablesListProps) {
236248
getScrollElement: () => parentRef.current,
237249
estimateSize: () =>
238250
onClick ? SUGGESTION_VARIABLE_ITEM_HEIGHT : VARIABLE_ITEM_HEIGHT,
251+
measureElement: (element) => element.getBoundingClientRect().height,
239252
overscan: 50,
240253
})
241254

@@ -261,6 +274,7 @@ export default function VariablesList(props: VariablesListProps) {
261274
<Box
262275
key={virtualItem.key}
263276
data-index={virtualItem.index}
277+
ref={virtualizer.measureElement}
264278
position="absolute"
265279
top={0}
266280
left={0}

0 commit comments

Comments
 (0)