Skip to content

Commit

Permalink
feat(ai-chat-log): story for user cancel scroll to end
Browse files Browse the repository at this point in the history
  • Loading branch information
krisantrobus committed Jan 16, 2025
1 parent 869544f commit d62b979
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ export const SidePanelScroll: StoryFn = () => {
);
const [message, setMessage] = React.useState("");
const [mounted, setMounted] = React.useState(false);
const loggerRef = React.useRef(null);
const scrollerRef = React.useRef(null);
const [userInterctedScroll, setUserInteractedScroll] = React.useState(false);
const loggerRef = React.useRef<HTMLDivElement>(null);
const scrollerRef = React.useRef<HTMLDivElement>(null);

React.useEffect(() => {
setMounted(true);
Expand All @@ -222,20 +223,27 @@ export const SidePanelScroll: StoryFn = () => {

const onAnimationEnd = (): void => {
setIsAnimating(false);
scrollToChatEnd();
setUserInteractedScroll(false);
};

const onAnimationStart = (): void => {
setUserInteractedScroll(false);
setIsAnimating(true);
};

const userScrolled = (): void => setUserInteractedScroll(true);

React.useEffect(() => {
const interval = setInterval(() => isAnimating && scrollToChatEnd(), 30);
scrollerRef.current?.addEventListener("wheel", userScrolled);
scrollerRef.current?.addEventListener("touchmove", userScrolled);

const interval = setInterval(() => isAnimating && !userInterctedScroll && scrollToChatEnd(), 5);
return () => {
if (interval) clearInterval(interval);
scrollerRef.current?.removeEventListener("wheel", userScrolled);
scrollerRef.current?.removeEventListener("touchmove", userScrolled);
};
}, [isAnimating]);
}, [isAnimating, userInterctedScroll]);

// eslint-disable-next-line storybook/prefer-pascal-case
const createNewMessage = (newMessage: any, forceBot?: boolean): Omit<AIChat, "id"> => {
Expand Down
15 changes: 12 additions & 3 deletions packages/paste-website/src/component-examples/AIChatLogExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ const exampleAIResponseText =
const AnimatedBotScrollable = () => {
const [isAnimating, setIsAnimating] = React.useState(false);
const [userInterctedScroll, setUserInteractedScroll] = React.useState(false);
const loggerRef = React.useRef(null);
const scrollerRef = React.useRef(null);
Expand All @@ -789,22 +790,30 @@ const AnimatedBotScrollable = () => {
scrollPosition?.scrollTo({ top: scrollHeight.scrollHeight, behavior: "smooth" });
};
const userScrolled = () => setUserInteractedScroll(true);
const onAnimationEnd = () => {
setIsAnimating(false);
scrollToChatEnd();
setUserInteractedScroll(false);
};
const onAnimationStart = () => {
setUserInteractedScroll(false);
setIsAnimating(true);
};
React.useEffect(() => {
const interval = setInterval(() => isAnimating && scrollToChatEnd(), 30);
scrollerRef.current?.addEventListener("wheel", userScrolled);
scrollerRef.current?.addEventListener("touchmove", userScrolled);
const interval = setInterval(() => isAnimating && !userInterctedScroll && scrollToChatEnd(), 5);
return () => {
if (interval) clearInterval(interval);
scrollerRef.current?.removeEventListener("wheel", userScrolled);
scrollerRef.current?.removeEventListener("touchmove", userScrolled);
};
}, [isAnimating]);
}, [isAnimating, userInterctedScroll, scrollerRef]);
const pushLargeBotMessage = () => {
push({
Expand Down

0 comments on commit d62b979

Please sign in to comment.