diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 53ef629b232..e84279cd07f 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -518,6 +518,7 @@ export function ChatActions(props: { showContextPrompts: boolean; toggleContextPrompts: () => void; uploading: boolean; + attachImages: string[]; }) { const config = useAppConfig(); const navigate = useNavigate(); @@ -547,25 +548,38 @@ export function ChatActions(props: { const [showModelSelector, setShowModelSelector] = useState(false); const [showUploadImage, setShowUploadImage] = useState(false); + // this fix memory leak as well, idk why front-end it's so fucking difficult to maintain cause of stupid complex + // for front-end developer you literally fucking retarded, write a complex code useEffect(() => { const show = isVisionModel(currentModel); - setShowUploadImage(show); + if (showUploadImage !== show) { + setShowUploadImage(show); + } + if (!show) { - props.setAttachImages([]); - props.setUploading(false); + // Check if there's really a need to update these states to prevent unnecessary re-renders + if (props.uploading) { + props.setUploading(false); + } + if (props.attachImages.length !== 0) { + props.setAttachImages([]); + } } // if current model is not available // switch to first available model - const isUnavaliableModel = !models.some((m) => m.name === currentModel); - if (isUnavaliableModel && models.length > 0) { + const isUnavailableModel = !models.some((m) => m.name === currentModel); + if (isUnavailableModel && models.length > 0) { const nextModel = models[0].name as ModelType; - chatStore.updateCurrentSession( - (session) => (session.mask.modelConfig.model = nextModel), - ); - showToast(nextModel); + // Only update if the next model is different from the current model + if (currentModel !== nextModel) { + chatStore.updateCurrentSession( + (session) => (session.mask.modelConfig.model = nextModel), + ); + showToast(nextModel); + } } - }, [props, chatStore, currentModel, models]); + }, [props, chatStore, currentModel, models, showUploadImage]); return (