feat: improve auto scroll ux and edit model title

This commit is contained in:
Yidadaa
2023-08-02 23:35:51 +08:00
parent cbabb9392c
commit b5ef552c25
6 changed files with 27 additions and 16 deletions

View File

@@ -370,18 +370,27 @@ function ChatAction(props: {
function useScrollToBottom() {
// for auto-scroll
const scrollRef = useRef<HTMLDivElement>(null);
const [autoScroll, setAutoScroll] = useState(true);
const autoScroll = useRef(true);
const scrollToBottom = useCallback(() => {
const dom = scrollRef.current;
if (dom) {
requestAnimationFrame(() => dom.scrollTo(0, dom.scrollHeight));
}
}, []);
const setAutoScroll = (enable: boolean) => {
autoScroll.current = enable;
};
// auto scroll
useEffect(() => {
autoScroll && scrollToBottom();
});
const intervalId = setInterval(() => {
if (autoScroll.current) {
scrollToBottom();
}
}, 100);
return () => clearInterval(intervalId);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return {
scrollRef,
@@ -532,7 +541,7 @@ export function EditMessageModal(props: { onClose: () => void }) {
return (
<div className="modal-mask">
<Modal
title={Locale.UI.Edit}
title={Locale.Chat.EditMessage.Title}
onClose={props.onClose}
actions={[
<IconButton
@@ -590,10 +599,7 @@ export function Chat() {
type RenderMessage = ChatMessage & { preview?: boolean };
const chatStore = useChatStore();
const [session, sessionIndex] = useChatStore((state) => [
state.currentSession(),
state.currentSessionIndex,
]);
const session = chatStore.currentSession();
const config = useAppConfig();
const fontSize = config.fontSize;
@@ -608,9 +614,14 @@ export function Chat() {
const isMobileScreen = useMobileScreen();
const navigate = useNavigate();
const lastBodyScroolTop = useRef(0);
const onChatBodyScroll = (e: HTMLElement) => {
const isTouchBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 10;
setHitBottom(isTouchBottom);
// only enable auto scroll when scroll down and touched bottom
setAutoScroll(e.scrollTop >= lastBodyScroolTop.current && isTouchBottom);
lastBodyScroolTop.current = e.scrollTop;
};
// prompt hints
@@ -1036,7 +1047,6 @@ export function Chat() {
ref={scrollRef}
onScroll={(e) => onChatBodyScroll(e.currentTarget)}
onMouseDown={() => inputRef.current?.blur()}
onWheel={(e) => setAutoScroll(hitBottom && e.deltaY > 0)}
onTouchStart={() => {
inputRef.current?.blur();
setAutoScroll(false);
@@ -1148,7 +1158,7 @@ export function Chat() {
}}
fontSize={fontSize}
parentRef={scrollRef}
defaultShow={i >= messages.length - 10}
defaultShow={i >= messages.length - 6}
/>
</div>
@@ -1193,7 +1203,6 @@ export function Chat() {
value={userInput}
onKeyDown={onInputKeyDown}
onFocus={() => setAutoScroll(true)}
onBlur={() => setAutoScroll(false)}
rows={inputRows}
autoFocus={autoFocus}
style={{