Skip to content

Commit c3f623b

Browse files
committed
New impl to useEffect
1 parent 5734eaa commit c3f623b

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

dist/build/static/js/bundle.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/chatWidget/chatWindow/index.tsx

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Send } from "lucide-react";
22
import { extractMessageFromOutput, getAnimationOrigin, getChatPosition } from "../utils";
3-
import React, { useEffect, useRef, useState, MouseEvent } from "react";
3+
import React, { useEffect, useRef, useState, MouseEvent, useCallback } from "react";
44
import { ChatMessageType } from "../../types/chatWidget";
55
import ChatMessage from "./chatMessage";
66
import { sendMessage } from "../../controllers";
@@ -205,31 +205,8 @@ export default function ChatWindow({
205205
}, 100);
206206
}, [messages, open]);
207207

208-
// Resize event handlers
209-
const handleResizeStart = (e: MouseEvent<HTMLDivElement>) => {
210-
if (!resizable) return;
211-
212-
e.preventDefault();
213-
setIsResizing(true);
214-
215-
// Store initial mouse position
216-
resizeStartPos.current = {
217-
x: e.clientX,
218-
y: e.clientY
219-
};
220-
221-
// Store initial size
222-
initialSize.current = {
223-
width: currentWidth || width,
224-
height: currentHeight || height
225-
};
226-
227-
// Add resize event listeners
228-
document.addEventListener('mousemove', handleResizeMove);
229-
document.addEventListener('mouseup', handleResizeEnd);
230-
};
231-
232-
const handleResizeMove = (e: MouseEvent | any) => {
208+
// Define resize handlers with useCallback to prevent unnecessary re-renders
209+
const handleResizeMove = useCallback((e: MouseEvent | any) => {
233210
if (!isResizing) return;
234211

235212
// Calculate the change in mouse position
@@ -246,24 +223,52 @@ export default function ChatWindow({
246223

247224
setCurrentWidth(newWidth);
248225
setCurrentHeight(newHeight);
249-
};
226+
}, [isResizing, min_width, max_width, min_height, max_height]);
250227

251-
const handleResizeEnd = () => {
228+
const handleResizeEnd = useCallback(() => {
252229
setIsResizing(false);
253230

254231
// Clean up event listeners
255232
document.removeEventListener('mousemove', handleResizeMove);
256233
document.removeEventListener('mouseup', handleResizeEnd);
257-
};
234+
}, [handleResizeMove]);
258235

259-
// Add resize event listeners on mount and remove on unmount
236+
// Add a useEffect to manage event listener cleanup
260237
useEffect(() => {
238+
const currentHandleResizeMove = handleResizeMove;
239+
const currentHandleResizeEnd = handleResizeEnd;
240+
261241
return () => {
262-
document.removeEventListener('mousemove', handleResizeMove);
263-
document.removeEventListener('mouseup', handleResizeEnd);
242+
document.removeEventListener('mousemove', currentHandleResizeMove);
243+
document.removeEventListener('mouseup', currentHandleResizeEnd);
264244
};
265245
}, [handleResizeMove, handleResizeEnd]);
266246

247+
const handleResizeStart = useCallback((e: MouseEvent<HTMLDivElement>) => {
248+
if (!resizable) return;
249+
250+
e.preventDefault();
251+
setIsResizing(true);
252+
253+
// Store initial mouse position
254+
resizeStartPos.current = {
255+
x: e.clientX,
256+
y: e.clientY
257+
};
258+
259+
// Store initial size
260+
initialSize.current = {
261+
width: currentWidth || width,
262+
height: currentHeight || height
263+
};
264+
265+
// Add resize event listeners
266+
document.addEventListener('mousemove', handleResizeMove);
267+
document.addEventListener('mouseup', handleResizeEnd);
268+
}, [resizable, currentWidth, currentHeight, width, height, handleResizeMove, handleResizeEnd]);
269+
270+
// The cleanup for event listeners is now handled in the useEffect above
271+
267272

268273
return (
269274
<div

0 commit comments

Comments
 (0)