mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-11 13:08:51 +01:00
* 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
46 lines
1.1 KiB
TypeScript
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,
|
|
};
|
|
}
|