From 79e22e65931211df1190c884ebb215fbcf5afb97 Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 6 Aug 2025 21:32:24 +0800 Subject: [PATCH] feat(ai-chat): store scroll element ref - Integrated AnimatePresence for smoother transitions in the ChatInterface layout. - Refactored scroll area handling to use state instead of ref, improving responsiveness and state management. - Updated WelcomeScreen button animations for better user experience during interactions. These changes enhance the visual appeal and functionality of the AI chat interface. Signed-off-by: Innei --- .../components/layouts/ChatInterface.tsx | 64 ++++++++++--------- .../components/layouts/WelcomeScreen.tsx | 2 + 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx index d08809fb9..7d90eb10b 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx @@ -5,9 +5,10 @@ import { springScrollTo } from "@follow/utils/scroller" import type { BizUIMessage } from "@folo-services/ai-tools" import { ErrorBoundary } from "@sentry/react" import type { EditorState, LexicalEditor } from "lexical" +import { AnimatePresence } from "motion/react" import { nanoid } from "nanoid" import type { FC } from "react" -import { useCallback, useEffect, useRef, useState } from "react" +import { useCallback, useEffect, useState } from "react" import { useEventCallback } from "usehooks-ts" import { @@ -42,7 +43,7 @@ const ChatInterfaceContent = () => { const currentChatId = useCurrentChatId() - const scrollAreaRef = useRef(null) + const [scrollAreaRef, setScrollAreaRef] = useState(null) const [isAtBottom, setIsAtBottom] = useState(true) // Reset handlers when chatId changes @@ -53,7 +54,7 @@ const ChatInterfaceContent = () => { const { isLoading: isLoadingHistory } = useLoadMessages(currentChatId || "", { onLoad: () => { nextFrame(() => { - const $scrollArea = scrollAreaRef.current + const $scrollArea = scrollAreaRef const scrollHeight = $scrollArea?.scrollHeight if (scrollHeight) { @@ -66,10 +67,11 @@ const ChatInterfaceContent = () => { }, }) - const { resetScrollState } = useAutoScroll(scrollAreaRef.current, status === "streaming") + const { resetScrollState } = useAutoScroll(scrollAreaRef, status === "streaming") useEffect(() => { - const scrollElement = scrollAreaRef.current + const scrollElement = scrollAreaRef + if (!scrollElement) return const handleScroll = () => { @@ -86,14 +88,14 @@ const ChatInterfaceContent = () => { return () => { scrollElement.removeEventListener("scroll", handleScroll) } - }, []) + }, [scrollAreaRef]) const scrollToBottom = useCallback(() => { - const scrollElement = scrollAreaRef.current + const scrollElement = scrollAreaRef if (!scrollElement) return springScrollTo(scrollElement.scrollHeight, scrollElement) - }, []) + }, [scrollAreaRef]) const blockActions = useBlockActions() const handleSendMessage = useEventCallback( @@ -145,26 +147,28 @@ const ChatInterfaceContent = () => { return (
- {!hasMessages && !isLoadingHistory ? ( - - ) : ( - - {isLoadingHistory ? ( -
- -
- ) : ( -
- - {status === "submitted" && } -
- )} -
- )} + + {!hasMessages && !isLoadingHistory ? ( + + ) : ( + + {isLoadingHistory ? ( +
+ +
+ ) : ( +
+ + {status === "submitted" && } +
+ )} +
+ )} +
{shouldShowScrollToBottom && ( @@ -180,9 +184,9 @@ const ChatInterfaceContent = () => { {/* Content */}
- + - Scroll to Bottom + Scroll to Bottom
diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/WelcomeScreen.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/WelcomeScreen.tsx index 89bb186b5..95582ea1e 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/WelcomeScreen.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/WelcomeScreen.tsx @@ -67,6 +67,7 @@ export const WelcomeScreen = ({ onSend }: WelcomeScreenProps) => { className="bg-material-medium hover:bg-material-thick border-border text-text-secondary hover:text-text whitespace-nowrap rounded-full border px-4 py-2 text-xs transition-colors" initial={{ opacity: 0, scale: 0.9 }} animate={{ opacity: 1, scale: 1 }} + exit={{ opacity: 0, y: 10 }} transition={{ delay: index * 0.1 }} onClick={() => onSend(shortcut.prompt, null)} title={shortcut.hotkey ? `${shortcut.name} (${shortcut.hotkey})` : shortcut.name} @@ -83,6 +84,7 @@ export const WelcomeScreen = ({ onSend }: WelcomeScreenProps) => { className="bg-material-medium hover:bg-material-thick border-border text-text-secondary hover:text-text whitespace-nowrap rounded-full border px-4 py-2 text-xs transition-colors" initial={{ opacity: 0, scale: 0.9 }} animate={{ opacity: 1, scale: 1 }} + exit={{ opacity: 0, y: 10 }} transition={{ delay: (enabledShortcuts.length + index) * 0.1 }} onClick={() => onSend(suggestion, null)} >