2024-08-21 18:18:45 -04:00
|
|
|
import throttle from 'lodash/throttle';
|
2024-07-20 01:51:59 -04:00
|
|
|
import { Constants } from 'librechat-data-provider';
|
2025-12-25 01:43:54 -05:00
|
|
|
import { useEffect, useRef, useCallback, useMemo } from 'react';
|
2024-06-25 03:02:38 -04:00
|
|
|
import type { TMessage } from 'librechat-data-provider';
|
2025-10-10 11:22:16 +03:00
|
|
|
import { getTextKey, TEXT_KEY_DIVIDER, logger } from '~/utils';
|
2025-09-17 13:07:56 -04:00
|
|
|
import { useMessagesViewContext } from '~/Providers';
|
2024-06-25 03:02:38 -04:00
|
|
|
|
|
|
|
|
export default function useMessageProcess({ message }: { message?: TMessage | null }) {
|
|
|
|
|
const latestText = useRef<string | number>('');
|
2024-08-21 18:18:45 -04:00
|
|
|
const hasNoChildren = useMemo(() => (message?.children?.length ?? 0) === 0, [message]);
|
2024-06-25 03:02:38 -04:00
|
|
|
|
2025-12-25 01:43:54 -05:00
|
|
|
const { conversation, setAbortScroll, setLatestMessage, isSubmitting } = useMessagesViewContext();
|
2024-06-25 03:02:38 -04:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-07-20 01:51:59 -04:00
|
|
|
const convoId = conversation?.conversationId;
|
|
|
|
|
if (convoId === Constants.NEW_CONVO) {
|
2024-06-25 03:02:38 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!message) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!hasNoChildren) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-20 01:51:59 -04:00
|
|
|
const textKey = getTextKey(message, convoId);
|
2024-06-25 03:02:38 -04:00
|
|
|
|
2024-07-20 01:51:59 -04:00
|
|
|
// Check for text/conversation change
|
|
|
|
|
const logInfo = {
|
|
|
|
|
textKey,
|
|
|
|
|
'latestText.current': latestText.current,
|
2024-08-21 18:18:45 -04:00
|
|
|
messageId: message.messageId,
|
2024-07-20 01:51:59 -04:00
|
|
|
convoId,
|
|
|
|
|
};
|
2025-10-10 11:22:16 +03:00
|
|
|
|
|
|
|
|
/* Extracted convoId from previous textKey (format: messageId|||length|||lastChars|||convoId) */
|
|
|
|
|
let previousConvoId: string | null = null;
|
|
|
|
|
if (
|
|
|
|
|
latestText.current &&
|
|
|
|
|
typeof latestText.current === 'string' &&
|
|
|
|
|
latestText.current.length > 0
|
|
|
|
|
) {
|
|
|
|
|
const parts = latestText.current.split(TEXT_KEY_DIVIDER);
|
|
|
|
|
previousConvoId = parts[parts.length - 1] || null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-20 01:51:59 -04:00
|
|
|
if (
|
|
|
|
|
textKey !== latestText.current ||
|
2025-10-10 11:22:16 +03:00
|
|
|
(convoId != null && previousConvoId != null && convoId !== previousConvoId)
|
2024-07-20 01:51:59 -04:00
|
|
|
) {
|
2025-01-09 15:40:10 -05:00
|
|
|
logger.log('latest_message', '[useMessageProcess] Setting latest message; logInfo:', logInfo);
|
2024-07-20 01:51:59 -04:00
|
|
|
latestText.current = textKey;
|
|
|
|
|
setLatestMessage({ ...message });
|
|
|
|
|
} else {
|
2025-01-09 15:40:10 -05:00
|
|
|
logger.log('latest_message', 'No change in latest message; logInfo', logInfo);
|
2024-06-25 03:02:38 -04:00
|
|
|
}
|
|
|
|
|
}, [hasNoChildren, message, setLatestMessage, conversation?.conversationId]);
|
|
|
|
|
|
2024-08-21 18:18:45 -04:00
|
|
|
const handleScroll = useCallback(
|
|
|
|
|
(event: unknown | TouchEvent | WheelEvent) => {
|
|
|
|
|
throttle(() => {
|
|
|
|
|
logger.log(
|
|
|
|
|
'message_scrolling',
|
2025-12-25 01:43:54 -05:00
|
|
|
`useMessageProcess: setting abort scroll to ${isSubmitting}, handleScroll event`,
|
2024-08-21 18:18:45 -04:00
|
|
|
event,
|
|
|
|
|
);
|
2025-12-25 01:43:54 -05:00
|
|
|
if (isSubmitting) {
|
2024-08-21 18:18:45 -04:00
|
|
|
setAbortScroll(true);
|
|
|
|
|
} else {
|
|
|
|
|
setAbortScroll(false);
|
|
|
|
|
}
|
|
|
|
|
}, 500)();
|
|
|
|
|
},
|
2025-12-25 01:43:54 -05:00
|
|
|
[isSubmitting, setAbortScroll],
|
2024-06-25 03:02:38 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
handleScroll,
|
2025-12-25 01:43:54 -05:00
|
|
|
isSubmitting,
|
2024-06-25 03:02:38 -04:00
|
|
|
conversation,
|
|
|
|
|
};
|
|
|
|
|
}
|