SurfSense/surfsense_web/components/chat/ScrollUtils.tsx

82 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-07-27 10:05:37 -07:00
import { type RefObject, useEffect } from "react";
2025-04-07 23:47:06 -07:00
/**
* Function to scroll to the bottom of a container
*/
export const scrollToBottom = (ref: RefObject<HTMLDivElement>) => {
2025-07-27 10:05:37 -07:00
ref.current?.scrollIntoView({ behavior: "smooth" });
2025-04-07 23:47:06 -07:00
};
/**
* Hook to scroll to bottom when messages change
*/
export const useScrollToBottom = (ref: RefObject<HTMLDivElement>, dependencies: any[]) => {
2025-07-27 10:05:37 -07:00
useEffect(() => {
scrollToBottom(ref);
}, dependencies);
2025-04-07 23:47:06 -07:00
};
/**
* Function to check scroll position and update indicators
*/
export const updateScrollIndicators = (
2025-07-27 10:05:37 -07:00
tabsListRef: RefObject<HTMLDivElement>,
setCanScrollLeft: (value: boolean) => void,
setCanScrollRight: (value: boolean) => void
2025-04-07 23:47:06 -07:00
) => {
2025-07-27 10:05:37 -07:00
if (tabsListRef.current) {
const { scrollLeft, scrollWidth, clientWidth } = tabsListRef.current;
setCanScrollLeft(scrollLeft > 0);
setCanScrollRight(scrollLeft + clientWidth < scrollWidth - 10); // 10px buffer
}
2025-04-07 23:47:06 -07:00
};
/**
* Hook to initialize scroll indicators and add resize listener
*/
export const useScrollIndicators = (
2025-07-27 10:05:37 -07:00
tabsListRef: RefObject<HTMLDivElement>,
setCanScrollLeft: (value: boolean) => void,
setCanScrollRight: (value: boolean) => void
2025-04-07 23:47:06 -07:00
) => {
2025-07-27 10:05:37 -07:00
const updateIndicators = () =>
updateScrollIndicators(tabsListRef, setCanScrollLeft, setCanScrollRight);
useEffect(() => {
updateIndicators();
// Add resize listener to update indicators when window size changes
window.addEventListener("resize", updateIndicators);
return () => window.removeEventListener("resize", updateIndicators);
2025-07-27 10:41:15 -07:00
}, [updateIndicators]);
2025-07-27 10:05:37 -07:00
return updateIndicators;
2025-04-07 23:47:06 -07:00
};
/**
* Function to scroll tabs list left
*/
export const scrollTabsLeft = (
2025-07-27 10:05:37 -07:00
tabsListRef: RefObject<HTMLDivElement>,
updateIndicators: () => void
2025-04-07 23:47:06 -07:00
) => {
2025-07-27 10:05:37 -07:00
if (tabsListRef.current) {
tabsListRef.current.scrollBy({ left: -200, behavior: "smooth" });
// Update indicators after scrolling
setTimeout(updateIndicators, 300);
}
2025-04-07 23:47:06 -07:00
};
/**
* Function to scroll tabs list right
*/
export const scrollTabsRight = (
2025-07-27 10:05:37 -07:00
tabsListRef: RefObject<HTMLDivElement>,
updateIndicators: () => void
2025-04-07 23:47:06 -07:00
) => {
2025-07-27 10:05:37 -07:00
if (tabsListRef.current) {
tabsListRef.current.scrollBy({ left: 200, behavior: "smooth" });
// Update indicators after scrolling
setTimeout(updateIndicators, 300);
}
};