diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index f65afe90..4c31689b 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -91,7 +91,8 @@ export const ChatPane = forwardRef(({ const queryClient = useQueryClient(); const [showScrollToBottom, setShowScrollToBottom] = useState(false); const [isDragOver, setIsDragOver] = useState(false); - const [scrolledProjects] = useState(() => new Set()); + const scrolledProjectsRef = useRef(new Set()); + const shouldScrollToBottomRef = useRef(false); const [showOnboarding, setShowOnboarding] = useState(false); const [isStuck, setIsStuck] = useState(false); const [aiError, setAIError] = useState(null); @@ -489,30 +490,46 @@ export const ChatPane = forwardRef(({ useEffect(() => { if (scrollAreaRef.current && (messages || streamingMessage)) { - // Check if user was already at or near the bottom (within 100px threshold) const threshold = 100; const container = scrollAreaRef.current; const isNearBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - threshold; - // Only auto-scroll if user was already near the bottom - if (isNearBottom) { - container.scrollTop = container.scrollHeight; + // Auto-scroll if: + // 1. User just sent a message (shouldScrollToBottomRef is true), OR + // 2. User was already near the bottom + if (shouldScrollToBottomRef.current || isNearBottom) { + // Double requestAnimationFrame ensures scroll happens after layout and paint + requestAnimationFrame(() => { + requestAnimationFrame(() => { + if (scrollAreaRef.current) { + scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight; + } + }); + }); + shouldScrollToBottomRef.current = false; // Reset the flag } } }, [messages, streamingMessage, isLoading]); + // Remove project from scrolled set when loading starts (so it can scroll again after loading) + useEffect(() => { + if (isLoadingHistory) { + scrolledProjectsRef.current.delete(projectId); + } + }, [projectId, isLoadingHistory]); + // Scroll to bottom when first visiting a project (including page refresh) useEffect(() => { - if (projectId && !scrolledProjects.has(projectId)) { - // Small delay to ensure messages have loaded - const timer = setTimeout(() => { - scrollToBottom(); - scrolledProjects.add(projectId); - }, 100); - - return () => clearTimeout(timer); + if (projectId && !scrolledProjectsRef.current.has(projectId) && !isLoadingHistory && messages.length > 0) { + // Use double RAF to ensure DOM is fully rendered after isLoadingHistory becomes false + requestAnimationFrame(() => { + requestAnimationFrame(() => { + scrollToBottom(); + scrolledProjectsRef.current.add(projectId); + }); + }); } - }, [projectId, scrolledProjects, scrollToBottom]); + }, [projectId, isLoadingHistory, messages.length, scrollToBottom]); // Drag and drop handlers const handleDragEnter = useCallback((e: React.DragEvent) => { @@ -577,6 +594,9 @@ export const ChatPane = forwardRef(({ // Add model to recently used when sending a message addRecentlyUsedModel(modelToUse); + // Mark that we should scroll to bottom when the message is added + shouldScrollToBottomRef.current = true; + await sendMessage(messageContent, modelToUse); }, [addRecentlyUsedModel, fs, isConfigured, isLoading, providerModel, sendMessage, tmpPath]); diff --git a/src/hooks/useAIChat.ts b/src/hooks/useAIChat.ts index 8e5031b8..e8d64c51 100644 --- a/src/hooks/useAIChat.ts +++ b/src/hooks/useAIChat.ts @@ -46,8 +46,10 @@ export function useAIChat({ const [lastFinishReason, setLastFinishReason] = useState(null); const [isLoadingHistory, setIsLoadingHistory] = useState(true); - const initSession = useCallback(async () => { - setIsLoadingHistory(true); + const initSession = useCallback(async (showLoading = true) => { + if (showLoading) { + setIsLoadingHistory(true); + } try { // Load or update session const session = await sessionManager.loadSession(projectId, tools, customTools, maxSteps); @@ -59,7 +61,9 @@ export function useAIChat({ setLastInputTokens(session.lastInputTokens || 0); setLastFinishReason(session.lastFinishReason ?? null); } finally { - setIsLoadingHistory(false); + if (showLoading) { + setIsLoadingHistory(false); + } } }, [sessionManager, projectId, tools, customTools, maxSteps]); @@ -129,7 +133,8 @@ export function useAIChat({ providerModel: string ) => { try { - await initSession(); + // Ensure session is loaded (without showing loading state) + await initSession(false); await sessionManager.sendMessage(projectId, content, providerModel); } catch (error) { if (onAIError && error instanceof Error) { @@ -142,7 +147,8 @@ export function useAIChat({ const startGeneration = useCallback(async (providerModel: string) => { try { - await initSession(); + // Ensure session is loaded (without showing loading state) + await initSession(false); await sessionManager.startGeneration(projectId, providerModel); } catch (error) { if (onAIError && error instanceof Error) {