Skip to content
15 changes: 15 additions & 0 deletions webview-ui/src/components/chat/ChatRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const ChatRowContent = ({
const [editedContent, setEditedContent] = useState("")
const [editMode, setEditMode] = useState<Mode>(mode || "code")
const [editImages, setEditImages] = useState<string[]>([])
const editAreaRef = useRef<HTMLDivElement>(null)

// Handle message events for image selection during edit mode
useEffect(() => {
Expand Down Expand Up @@ -165,6 +166,19 @@ export const ChatRowContent = ({
// No need to notify the backend
}, [message.text, message.images, mode])

// Scroll to edit area when entering edit mode
useEffect(() => {
if (isEditing && editAreaRef.current) {
// Use a small delay to ensure the DOM has updated
setTimeout(() => {
editAreaRef.current?.scrollIntoView({
behavior: "smooth",
block: "center",
})
}, 100)
}
}, [isEditing])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setTimeout should be cleaned up to prevent potential memory leaks and avoid attempting to scroll on an unmounted component. If isEditing changes back to false or the component unmounts before the 100ms timeout completes, the callback will still execute. Add a cleanup function that clears the timeout:

Suggested change
// Scroll to edit area when entering edit mode
useEffect(() => {
if (isEditing && editAreaRef.current) {
// Use a small delay to ensure the DOM has updated
setTimeout(() => {
editAreaRef.current?.scrollIntoView({
behavior: "smooth",
block: "center",
})
}, 100)
}
}, [isEditing])
// Scroll to edit area when entering edit mode
useEffect(() => {
if (isEditing && editAreaRef.current) {
// Use a small delay to ensure the DOM has updated
const timeoutId = setTimeout(() => {
editAreaRef.current?.scrollIntoView({
behavior: "smooth",
block: "center",
})
}, 100)
return () => clearTimeout(timeoutId)
}
}, [isEditing])


// Handle cancel edit
const handleCancelEdit = useCallback(() => {
setIsEditing(false)
Expand Down Expand Up @@ -1110,6 +1124,7 @@ export const ChatRowContent = ({
<span style={{ fontWeight: "bold" }}>{t("chat:feedback.youSaid")}</span>
</div>
<div
ref={editAreaRef}
className={cn(
"ml-6 border rounded-sm overflow-hidden whitespace-pre-wrap",
isEditing
Expand Down
Loading