mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-12 05:28:51 +01:00
* refactor(DropdownPopup): set MenuButton `as` prop to `div` to prevent React warning: validateDOMNesting(...): <button> cannot appear as a descendant of <button> * refactor: memoize ChatGroupItem and ControlCombobox components * refactor(OpenAIClient): await stream process finish before finalCompletion event handling * refactor: update useSSE.ts typing to handle null and undefined values in data properties * refactor: set abort scroll to false on SSE connection open * refactor: improve logger functionality with filter support * refactor: update handleScroll typing in MessageContainer component * refactor: update logger.dir call in useChatFunctions to log 'message_stream' tag format instead of the entire submission object as first arg * refactor: fix null check for message object in Message component * refactor: throttle handleScroll to help prevent auto-scrolling issues on new message requests; fix type issues within useMessageProcess * refactor: add abortScrollByIndex logging effect * refactor: update MessageIcon and Icon components to use React.memo for performance optimization * refactor: memoize ConvoIconURL component for performance optimization * chore: type issues * chore: update package version to 0.7.414
113 lines
3.4 KiB
TypeScript
113 lines
3.4 KiB
TypeScript
import throttle from 'lodash/throttle';
|
|
import { useRecoilValue } from 'recoil';
|
|
import { Constants } from 'librechat-data-provider';
|
|
import { useEffect, useRef, useCallback, useMemo, useState } from 'react';
|
|
import type { TMessage } from 'librechat-data-provider';
|
|
import { useChatContext, useAddedChatContext } from '~/Providers';
|
|
import { getTextKey, logger } from '~/utils';
|
|
import store from '~/store';
|
|
|
|
export default function useMessageProcess({ message }: { message?: TMessage | null }) {
|
|
const latestText = useRef<string | number>('');
|
|
const [siblingMessage, setSiblingMessage] = useState<TMessage | null>(null);
|
|
const hasNoChildren = useMemo(() => (message?.children?.length ?? 0) === 0, [message]);
|
|
|
|
const {
|
|
index,
|
|
conversation,
|
|
latestMessage,
|
|
setAbortScroll,
|
|
setLatestMessage,
|
|
isSubmitting: isSubmittingRoot,
|
|
} = useChatContext();
|
|
const { isSubmitting: isSubmittingAdditional } = useAddedChatContext();
|
|
const latestMultiMessage = useRecoilValue(store.latestMessageFamily(index + 1));
|
|
const isSubmittingFamily = useMemo(
|
|
() => isSubmittingRoot || isSubmittingAdditional,
|
|
[isSubmittingRoot, isSubmittingAdditional],
|
|
);
|
|
|
|
useEffect(() => {
|
|
const convoId = conversation?.conversationId;
|
|
if (convoId === Constants.NEW_CONVO) {
|
|
return;
|
|
}
|
|
if (!message) {
|
|
return;
|
|
}
|
|
if (!hasNoChildren) {
|
|
return;
|
|
}
|
|
|
|
const textKey = getTextKey(message, convoId);
|
|
|
|
// Check for text/conversation change
|
|
const logInfo = {
|
|
textKey,
|
|
'latestText.current': latestText.current,
|
|
messageId: message.messageId,
|
|
convoId,
|
|
};
|
|
if (
|
|
textKey !== latestText.current ||
|
|
(convoId != null &&
|
|
latestText.current &&
|
|
convoId !== latestText.current.split(Constants.COMMON_DIVIDER)[2])
|
|
) {
|
|
logger.log('[useMessageProcess] Setting latest message: ', logInfo);
|
|
latestText.current = textKey;
|
|
setLatestMessage({ ...message });
|
|
} else {
|
|
logger.log('No change in latest message', logInfo);
|
|
}
|
|
}, [hasNoChildren, message, setLatestMessage, conversation?.conversationId]);
|
|
|
|
const handleScroll = useCallback(
|
|
(event: unknown | TouchEvent | WheelEvent) => {
|
|
throttle(() => {
|
|
logger.log(
|
|
'message_scrolling',
|
|
`useMessageProcess: setting abort scroll to ${isSubmittingFamily}, handleScroll event`,
|
|
event,
|
|
);
|
|
if (isSubmittingFamily) {
|
|
setAbortScroll(true);
|
|
} else {
|
|
setAbortScroll(false);
|
|
}
|
|
}, 500)();
|
|
},
|
|
[isSubmittingFamily, setAbortScroll],
|
|
);
|
|
|
|
const showSibling = useMemo(
|
|
() =>
|
|
(hasNoChildren && latestMultiMessage && (latestMultiMessage.children?.length ?? 0) === 0) ||
|
|
!!siblingMessage,
|
|
[hasNoChildren, latestMultiMessage, siblingMessage],
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (
|
|
hasNoChildren &&
|
|
latestMultiMessage &&
|
|
latestMultiMessage.conversationId === message?.conversationId
|
|
) {
|
|
const newSibling = Object.assign({}, latestMultiMessage, {
|
|
parentMessageId: message.parentMessageId,
|
|
depth: message.depth,
|
|
});
|
|
setSiblingMessage(newSibling);
|
|
}
|
|
}, [hasNoChildren, latestMultiMessage, message, setSiblingMessage, latestMessage]);
|
|
|
|
return {
|
|
showSibling,
|
|
handleScroll,
|
|
conversation,
|
|
siblingMessage,
|
|
setSiblingMessage,
|
|
isSubmittingFamily,
|
|
latestMultiMessage,
|
|
};
|
|
}
|