mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
Fix: Disable Auto-scroll when user looks back through historical chat-Bug 9062 (#9107)
### What problem does this PR solve? This code allows user chat to auto-scroll down when entered, but if user scrolls up away from the generative feedback, autoscroll is disabled. Close #9062 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --------- Co-authored-by: Charles Copley <ccopley@ancera.com>
This commit is contained in:
@ -27,6 +27,14 @@ import { useTranslate } from './common-hooks';
|
||||
import { useSetPaginationParams } from './route-hook';
|
||||
import { useFetchTenantInfo, useSaveSetting } from './user-setting-hooks';
|
||||
|
||||
function usePrevious<T>(value: T) {
|
||||
const ref = useRef<T>();
|
||||
useEffect(() => {
|
||||
ref.current = value;
|
||||
}, [value]);
|
||||
return ref.current;
|
||||
}
|
||||
|
||||
export const useSetSelectedRecord = <T = IKnowledgeFile>() => {
|
||||
const [currentRecord, setCurrentRecord] = useState<T>({} as T);
|
||||
|
||||
@ -210,7 +218,6 @@ export const useSendMessageWithSse = (
|
||||
if (x) {
|
||||
const { done, value } = x;
|
||||
if (done) {
|
||||
console.info('done');
|
||||
resetAnswer();
|
||||
break;
|
||||
}
|
||||
@ -218,26 +225,23 @@ export const useSendMessageWithSse = (
|
||||
const val = JSON.parse(value?.data || '');
|
||||
const d = val?.data;
|
||||
if (typeof d !== 'boolean') {
|
||||
console.info('data:', d);
|
||||
setAnswer({
|
||||
...d,
|
||||
conversationId: body?.conversation_id,
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(e);
|
||||
// Swallow parse errors silently
|
||||
}
|
||||
}
|
||||
}
|
||||
console.info('done?');
|
||||
setDone(true);
|
||||
resetAnswer();
|
||||
return { data: await res, response };
|
||||
} catch (e) {
|
||||
setDone(true);
|
||||
resetAnswer();
|
||||
|
||||
console.warn(e);
|
||||
// Swallow fetch errors silently
|
||||
}
|
||||
},
|
||||
[initializeSseRef, url, resetAnswer],
|
||||
@ -267,7 +271,7 @@ export const useSpeechWithSse = (url: string = api.tts) => {
|
||||
message.error(res?.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('🚀 ~ error:', error);
|
||||
// Swallow errors silently
|
||||
}
|
||||
return response;
|
||||
},
|
||||
@ -279,20 +283,55 @@ export const useSpeechWithSse = (url: string = api.tts) => {
|
||||
|
||||
//#region chat hooks
|
||||
|
||||
export const useScrollToBottom = (messages?: unknown) => {
|
||||
export const useScrollToBottom = (
|
||||
messages?: unknown,
|
||||
containerRef?: React.RefObject<HTMLDivElement>,
|
||||
) => {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
|
||||
const scrollToBottom = useCallback(() => {
|
||||
if (messages) {
|
||||
ref.current?.scrollIntoView({ behavior: 'instant' });
|
||||
}
|
||||
}, [messages]); // If the message changes, scroll to the bottom
|
||||
const [isAtBottom, setIsAtBottom] = useState(true);
|
||||
const isAtBottomRef = useRef(true);
|
||||
|
||||
useEffect(() => {
|
||||
scrollToBottom();
|
||||
}, [scrollToBottom]);
|
||||
isAtBottomRef.current = isAtBottom;
|
||||
}, [isAtBottom]);
|
||||
|
||||
return ref;
|
||||
const checkIfUserAtBottom = useCallback(() => {
|
||||
if (!containerRef?.current) return true;
|
||||
const { scrollTop, scrollHeight, clientHeight } = containerRef.current;
|
||||
return Math.abs(scrollTop + clientHeight - scrollHeight) < 25;
|
||||
}, [containerRef]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!containerRef?.current) return;
|
||||
const container = containerRef.current;
|
||||
|
||||
const handleScroll = () => {
|
||||
setIsAtBottom(checkIfUserAtBottom());
|
||||
};
|
||||
|
||||
container.addEventListener('scroll', handleScroll);
|
||||
handleScroll();
|
||||
return () => container.removeEventListener('scroll', handleScroll);
|
||||
}, [containerRef, checkIfUserAtBottom]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!messages) return;
|
||||
if (!containerRef?.current) return;
|
||||
requestAnimationFrame(() => {
|
||||
setTimeout(() => {
|
||||
if (isAtBottomRef.current) {
|
||||
ref.current?.scrollIntoView({ behavior: 'smooth' });
|
||||
}
|
||||
}, 30);
|
||||
});
|
||||
}, [messages, containerRef]);
|
||||
|
||||
// Imperative scroll function
|
||||
const scrollToBottom = useCallback(() => {
|
||||
ref.current?.scrollIntoView({ behavior: 'smooth' });
|
||||
}, []);
|
||||
|
||||
return { scrollRef: ref, isAtBottom, scrollToBottom };
|
||||
};
|
||||
|
||||
export const useHandleMessageInputChange = () => {
|
||||
|
||||
Reference in New Issue
Block a user