import { memo, useRef, useEffect, useCallback } from 'react' import { useMessages } from '@/hooks/useOpenCode' import { useSettings } from '@/hooks/useSettings' import { MessagePart } from './MessagePart' import type { MessageWithParts } from '@/api/types' function getMessageTextContent(msg: MessageWithParts): string { return msg.parts .filter(p => p.type === 'text') .map(p => p.text || '') .join('\n\n') .trim() } interface MessageThreadProps { opcodeUrl: string sessionID: string directory?: string onFileClick?: (filePath: string, lineNumber?: number) => void containerRef?: React.RefObject onScrollStateChange?: (isScrolledUp: boolean) => void } const isMessageStreaming = (msg: MessageWithParts): boolean => { if (msg.info.role !== 'assistant') return false return !('completed' in msg.info.time && msg.info.time.completed) } const isMessageThinking = (msg: MessageWithParts): boolean => { if (msg.info.role !== 'assistant') return false return msg.parts.length === 0 && isMessageStreaming(msg) } const SCROLL_THRESHOLD = 150 const SCROLL_DEBOUNCE_MS = 50 export const MessageThread = memo(function MessageThread({ opcodeUrl, sessionID, directory, onFileClick, containerRef, onScrollStateChange }: MessageThreadProps) { const { data: messages, isLoading, error } = useMessages(opcodeUrl, sessionID, directory) const { preferences } = useSettings() const lastMessageCountRef = useRef(0) const userScrolledUpRef = useRef(false) const hasInitialScrolledRef = useRef(false) const scrollRAFRef = useRef(null) const lastScrollTimeRef = useRef(0) const scrollToBottom = useCallback((force = false) => { if (!containerRef?.current) return const now = Date.now() if (!force && now - lastScrollTimeRef.current < SCROLL_DEBOUNCE_MS) { return } lastScrollTimeRef.current = now if (scrollRAFRef.current) { cancelAnimationFrame(scrollRAFRef.current) } scrollRAFRef.current = requestAnimationFrame(() => { if (containerRef?.current) { containerRef.current.scrollTop = containerRef.current.scrollHeight userScrolledUpRef.current = false onScrollStateChange?.(false) } scrollRAFRef.current = null }) }, [containerRef, onScrollStateChange]) useEffect(() => { if (!containerRef?.current) return const container = containerRef.current const handleScroll = () => { const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight const isScrolledUp = distanceFromBottom > SCROLL_THRESHOLD if (userScrolledUpRef.current !== isScrolledUp) { userScrolledUpRef.current = isScrolledUp onScrollStateChange?.(isScrolledUp) } } container.addEventListener('scroll', handleScroll, { passive: true }) return () => container.removeEventListener('scroll', handleScroll) }, [containerRef, onScrollStateChange]) useEffect(() => { if (!containerRef?.current || !messages) return const currentMessageCount = messages.length const previousMessageCount = lastMessageCountRef.current if (!hasInitialScrolledRef.current && currentMessageCount > 0) { hasInitialScrolledRef.current = true scrollToBottom(true) lastMessageCountRef.current = currentMessageCount return } const messageAdded = currentMessageCount > previousMessageCount lastMessageCountRef.current = currentMessageCount const lastMessage = messages[messages.length - 1] const isUserMessage = lastMessage?.info.role === 'user' if (messageAdded && isUserMessage) { userScrolledUpRef.current = false scrollToBottom(true) return } if (!preferences?.autoScroll) return if (!userScrolledUpRef.current) { scrollToBottom() } }, [messages, preferences?.autoScroll, containerRef, scrollToBottom]) useEffect(() => { return () => { if (scrollRAFRef.current) { cancelAnimationFrame(scrollRAFRef.current) } } }, []) if (isLoading) { return (
Loading messages...
) } if (error) { return (
Error loading messages: {error instanceof Error ? error.message : 'Unknown error'}
) } if (!messages || messages.length === 0) { return (
No messages yet. Start a conversation below.
) } return (
{messages.map((msg) => { const streaming = isMessageStreaming(msg) const thinking = isMessageThinking(msg) return (
{msg.info.role === 'user' ? 'You' : (msg.info.role === 'assistant' && 'modelID' in msg.info ? msg.info.modelID : 'Assistant')} {msg.info.time && ( {new Date(msg.info.time.created).toLocaleTimeString()} )} {streaming && ( ● Generating... )}
{thinking ? (
▋ Thinking...
) : (
{msg.parts.map((part, index) => (
))}
)}
) })}
) })