LibreChat/client/src/hooks/useScrollToRef.ts
Danny Avila 7c0379ba51
fix: Allow Mobile Scroll During Message Stream (#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
2023-09-22 16:16:57 -04:00

46 lines
1.1 KiB
TypeScript

import { RefObject, useCallback } from 'react';
import throttle from 'lodash/throttle';
type TUseScrollToRef = {
targetRef: RefObject<HTMLDivElement>;
callback: () => void;
smoothCallback: () => void;
};
export default function useScrollToRef({ targetRef, callback, smoothCallback }: TUseScrollToRef) {
// eslint-disable-next-line react-hooks/exhaustive-deps
const scrollToRef = useCallback(
throttle(
() => {
targetRef.current?.scrollIntoView({ behavior: 'instant' });
callback();
},
450,
{ leading: true },
),
[targetRef],
);
// eslint-disable-next-line react-hooks/exhaustive-deps
const scrollToRefSmooth = useCallback(
throttle(
() => {
targetRef.current?.scrollIntoView({ behavior: 'smooth' });
smoothCallback();
},
750,
{ leading: true },
),
[targetRef],
);
const handleSmoothToRef: React.MouseEventHandler<HTMLButtonElement> = (e) => {
e.preventDefault();
scrollToRefSmooth();
};
return {
scrollToRef,
handleSmoothToRef,
};
}