| 5 | import "./styles/MessageArea.css"; |
| 6 | |
| 7 | const MessageArea = ({ |
| 8 | messages, |
| 9 | isGenerating, |
| 10 | isUserTyping, |
| 11 | onRegenerate, |
| 12 | }) => { |
| 13 | const messagesEndRef = useRef(null); |
| 14 | const [welcomeMessage, setWelcomeMessage] = useState(""); |
| 15 | const [_expandedErrors, _setExpandedErrors] = useState({}); |
| 16 | |
| 17 | const scrollToBottom = useCallback(() => { |
| 18 | messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); |
| 19 | }, []); |
| 20 | |
| 21 | useEffect(() => { |
| 22 | scrollToBottom(); |
| 23 | }, [messages, scrollToBottom]); |
| 24 | |
| 25 | useEffect(() => { |
| 26 | const welcomeMessages = [ |
| 27 | "What can I help with today?", |
| 28 | "What's on your mind?", |
| 29 | "How can I assist you?", |
| 30 | "What are we creating today?", |
| 31 | "Ask me anything.", |
| 32 | "Ready to explore ideas?", |
| 33 | "What would you like to know?", |
| 34 | "Let's make something amazing!", |
| 35 | "How may I help you?", |
| 36 | "What brings you here today?", |
| 37 | ]; |
| 38 | setWelcomeMessage( |
| 39 | welcomeMessages[Math.floor(Math.random() * welcomeMessages.length)], |
| 40 | ); |
| 41 | }, [messages.length]); |
| 42 | |
| 43 | const copyToClipboard = useCallback((text) => { |
| 44 | if (!text) return; |
| 45 | navigator.clipboard |
| 46 | .writeText(text) |
| 47 | .then(() => { |
| 48 | if (window?.showToast) |
| 49 | window.showToast("Copied to clipboard!", "info"); |
| 50 | }) |
| 51 | .catch((err) => { |
| 52 | if (window?.showToast) |
| 53 | window.showToast(`Failed to copy: ${err.message}`, "error"); |
| 54 | }); |
| 55 | }, []); |
| 56 | |
| 57 | const parseThinkTags = useCallback((text = "", isStreaming = false) => { |
| 58 | if (!text) { |
| 59 | return { |
| 60 | cleanedContent: "", |
| 61 | reasoningBlocks: [], |
| 62 | pendingReasoning: "", |
| 63 | }; |
| 64 | } |