Skip to content

Commit c305d0b

Browse files
authored
fix: Allow Mobile Scroll During Message Stream (danny-avila#984)
* fix(Icon/types): pick types from TMessage and TConversation * refactor: make abortScroll a global recoil state and change props/types for useScrollToRef * refactor(Message): invoke abort setter onTouchMove and onWheel, refactor(Messages): remove redundancy, reset abortScroll when scroll button is clicked
1 parent 05deb63 commit c305d0b

File tree

7 files changed

+55
-50
lines changed

7 files changed

+55
-50
lines changed

client/src/common/types.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,11 @@ export type TAuthConfig = {
179179
loginRedirect: string;
180180
};
181181

182-
export type IconProps = {
183-
size?: number;
184-
isCreatedByUser?: boolean;
185-
button?: boolean;
186-
model?: string;
187-
message?: boolean;
188-
className?: string;
189-
endpoint?: string | null;
190-
error?: boolean;
191-
chatGptLabel?: string;
192-
modelLabel?: string;
193-
jailbreak?: boolean;
194-
};
182+
export type IconProps = Pick<TMessage, 'isCreatedByUser' | 'model' | 'error'> &
183+
Pick<TConversation, 'chatGptLabel' | 'modelLabel' | 'jailbreak'> & {
184+
size?: number;
185+
button?: boolean;
186+
message?: boolean;
187+
className?: string;
188+
endpoint?: string | null;
189+
};

client/src/components/Endpoints/Icon.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Icon: React.FC<IconProps> = (props) => {
1919
width: size,
2020
height: size,
2121
}}
22-
className={`relative flex items-center justify-center ${props.className || ''}`}
22+
className={`relative flex items-center justify-center ${props.className ?? ''}`}
2323
>
2424
<img
2525
className="rounded-sm"
@@ -73,7 +73,7 @@ const Icon: React.FC<IconProps> = (props) => {
7373
default: { icon: <GPTIcon size={size * 0.7} />, bg: 'grey', name: 'UNKNOWN' },
7474
};
7575

76-
const { icon, bg, name } = endpointIcons[endpoint] || endpointIcons.default;
76+
const { icon, bg, name } = endpointIcons[endpoint ?? ''] ?? endpointIcons.default;
7777

7878
return (
7979
<div

client/src/components/Messages/Message.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable react-hooks/exhaustive-deps */
22
import { useGetConversationByIdQuery } from 'librechat-data-provider';
3-
import { useState, useEffect } from 'react';
4-
import { useSetRecoilState } from 'recoil';
3+
import { useEffect } from 'react';
4+
import { useSetRecoilState, useRecoilState } from 'recoil';
55
import copy from 'copy-to-clipboard';
66
import { SubRow, Plugin, MessageContent } from './Content';
77
// eslint-disable-next-line import/no-cycle
@@ -25,7 +25,7 @@ export default function Message({
2525
setSiblingIdx,
2626
}: TMessageProps) {
2727
const setLatestMessage = useSetRecoilState(store.latestMessage);
28-
const [abortScroll, setAbort] = useState(false);
28+
const [abortScroll, setAbortScroll] = useRecoilState(store.abortScroll);
2929
const { isSubmitting, ask, regenerate, handleContinue } = useMessageHandler();
3030
const { switchToConversation } = useConversation();
3131
const {
@@ -71,11 +71,11 @@ export default function Message({
7171
const enterEdit = (cancel?: boolean) =>
7272
setCurrentEditId && setCurrentEditId(cancel ? -1 : messageId);
7373

74-
const handleWheel = () => {
74+
const handleScroll = () => {
7575
if (blinker) {
76-
setAbort(true);
76+
setAbortScroll(true);
7777
} else {
78-
setAbort(false);
78+
setAbortScroll(false);
7979
}
8080
};
8181

@@ -133,7 +133,7 @@ export default function Message({
133133

134134
return (
135135
<>
136-
<div {...props} onWheel={handleWheel}>
136+
<div {...props} onWheel={handleScroll} onTouchMove={handleScroll}>
137137
<div className="relative m-auto flex gap-4 p-4 text-base md:max-w-2xl md:gap-6 md:py-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
138138
<div className="relative flex h-[30px] w-[30px] flex-col items-end text-right text-xs md:text-sm">
139139
{typeof icon === 'string' && /[^\\x00-\\x7F]+/.test(icon as string) ? (

client/src/components/Messages/Messages.tsx

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { useEffect, useState, useRef } from 'react';
1+
import { useEffect, useState, useRef, useCallback } from 'react';
2+
import { useRecoilValue, useSetRecoilState } from 'recoil';
23
import { CSSTransition } from 'react-transition-group';
3-
import { useRecoilValue } from 'recoil';
44

55
import ScrollToBottom from './ScrollToBottom';
66
import MessageHeader from './MessageHeader';
@@ -18,6 +18,7 @@ export default function Messages({ isSearchView = false }) {
1818

1919
const messagesTree = useRecoilValue(store.messagesTree);
2020
const showPopover = useRecoilValue(store.showPopover);
21+
const setAbortScroll = useSetRecoilState(store.abortScroll);
2122
const searchResultMessagesTree = useRecoilValue(store.searchResultMessagesTree);
2223

2324
const _messagesTree = isSearchView ? searchResultMessagesTree : messagesTree;
@@ -27,50 +28,47 @@ export default function Messages({ isSearchView = false }) {
2728

2829
const { screenshotTargetRef } = useScreenshot();
2930

30-
const handleScroll = () => {
31+
const checkIfAtBottom = useCallback(() => {
3132
if (!scrollableRef.current) {
3233
return;
3334
}
35+
3436
const { scrollTop, scrollHeight, clientHeight } = scrollableRef.current;
3537
const diff = Math.abs(scrollHeight - scrollTop);
3638
const percent = Math.abs(clientHeight - diff) / clientHeight;
37-
if (percent <= 0.2) {
38-
setShowScrollButton(false);
39-
} else {
40-
setShowScrollButton(true);
41-
}
42-
};
39+
const hasScrollbar = scrollHeight > clientHeight && percent >= 0.15;
40+
setShowScrollButton(hasScrollbar);
41+
}, [scrollableRef]);
4342

4443
useEffect(() => {
4544
const timeoutId = setTimeout(() => {
46-
if (!scrollableRef.current) {
47-
return;
48-
}
49-
const { scrollTop, scrollHeight, clientHeight } = scrollableRef.current;
50-
const diff = Math.abs(scrollHeight - scrollTop);
51-
const percent = Math.abs(clientHeight - diff) / clientHeight;
52-
const hasScrollbar = scrollHeight > clientHeight && percent > 0.2;
53-
setShowScrollButton(hasScrollbar);
45+
checkIfAtBottom();
5446
}, 650);
5547

5648
// Add a listener on the window object
57-
window.addEventListener('scroll', handleScroll);
49+
window.addEventListener('scroll', checkIfAtBottom);
5850

5951
return () => {
6052
clearTimeout(timeoutId);
61-
window.removeEventListener('scroll', handleScroll);
53+
window.removeEventListener('scroll', checkIfAtBottom);
6254
};
63-
}, [_messagesTree]);
55+
}, [_messagesTree, checkIfAtBottom]);
6456

6557
let timeoutId: ReturnType<typeof setTimeout> | undefined;
6658
const debouncedHandleScroll = () => {
6759
clearTimeout(timeoutId);
68-
timeoutId = setTimeout(handleScroll, 100);
60+
timeoutId = setTimeout(checkIfAtBottom, 100);
6961
};
7062

71-
const { scrollToRef: scrollToBottom, handleSmoothToRef } = useScrollToRef(messagesEndRef, () =>
72-
setShowScrollButton(false),
73-
);
63+
const scrollCallback = () => setShowScrollButton(false);
64+
const { scrollToRef: scrollToBottom, handleSmoothToRef } = useScrollToRef({
65+
targetRef: messagesEndRef,
66+
callback: scrollCallback,
67+
smoothCallback: () => {
68+
scrollCallback();
69+
setAbortScroll(false);
70+
},
71+
});
7472

7573
return (
7674
<div

client/src/hooks/useScrollToRef.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { RefObject, useCallback } from 'react';
22
import throttle from 'lodash/throttle';
33

4-
export default function useScrollToRef(targetRef: RefObject<HTMLDivElement>, callback: () => void) {
4+
type TUseScrollToRef = {
5+
targetRef: RefObject<HTMLDivElement>;
6+
callback: () => void;
7+
smoothCallback: () => void;
8+
};
9+
10+
export default function useScrollToRef({ targetRef, callback, smoothCallback }: TUseScrollToRef) {
511
// eslint-disable-next-line react-hooks/exhaustive-deps
612
const scrollToRef = useCallback(
713
throttle(
@@ -20,7 +26,7 @@ export default function useScrollToRef(targetRef: RefObject<HTMLDivElement>, cal
2026
throttle(
2127
() => {
2228
targetRef.current?.scrollIntoView({ behavior: 'smooth' });
23-
callback();
29+
smoothCallback();
2430
},
2531
750,
2632
{ leading: true },

client/src/store/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import submission from './submission';
88
import search from './search';
99
import preset from './preset';
1010
import lang from './language';
11-
import optionSettings from './optionSettings';
11+
import settings from './settings';
1212

1313
export default {
1414
...conversation,
@@ -21,5 +21,5 @@ export default {
2121
...search,
2222
...preset,
2323
...lang,
24-
...optionSettings,
24+
...settings,
2525
};

client/src/store/optionSettings.ts renamed to client/src/store/settings.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ type TOptionSettings = {
55
isCodeChat?: boolean;
66
};
77

8+
const abortScroll = atom<boolean>({
9+
key: 'abortScroll',
10+
default: false,
11+
});
12+
813
const optionSettings = atom<TOptionSettings>({
914
key: 'optionSettings',
1015
default: {},
@@ -31,6 +36,7 @@ const showPopover = atom<boolean>({
3136
});
3237

3338
export default {
39+
abortScroll,
3440
optionSettings,
3541
showPluginStoreDialog,
3642
showAgentSettings,

0 commit comments

Comments
 (0)