import { useState, useEffect, useRef } from 'react'; import messageDatabase from './data.json'; import './App.css'; export default function ChatDisplay() { const [isActive, setIsActive] = useState(false); const [showModal, setShowModal] = useState(true); const [currentMessageIndex, setCurrentMessageIndex] = useState( Math.floor(Math.random() * messageDatabase.length) ); const [showFeedback, setShowFeedback] = useState(false); const [message, setMessage] = useState(() => messageDatabase[currentMessageIndex].question); const [messagesSent, setMessagesSent] = useState(false); const [sentMessage, setSentMessage] = useState(''); const [isThinking, setIsThinking] = useState(false); const [userGuessCorrect, setUserGuessCorrect] = useState(false); const [showAnswer, setShowAnswer] = useState(false); const [showTopAnimation, setShowTopAnimation] = useState(false); const [animatedMessages, setAnimatedMessages] = useState({ userMessage: false, thinking: false, feedback: false }); const [reasoningButtonActive, setReasoningButtonActive] = useState(false); const [searchButtonActive, setSearchButtonActive] = useState(false); const getRandomMessageIndex = () => { let newIndex; do { newIndex = Math.floor(Math.random() * messageDatabase.length); } while (newIndex === currentMessageIndex && messageDatabase.length > 1); return newIndex; }; const loadNewQuestion = () => { const newIndex = getRandomMessageIndex(); setCurrentMessageIndex(newIndex); setMessage(messageDatabase[newIndex].question); setMessagesSent(false); setShowFeedback(false); setShowAnswer(false); setSentMessage(''); }; const isMessageTypeMatching = () => { const currentType = messageDatabase[currentMessageIndex].type; // Match reasoning type with reasoning button if (currentType === "reasoning" && reasoningButtonActive) return true; // Match search/standard type with search button if ((currentType === "search" || currentType === "standard") && searchButtonActive) return true; // Match default/standard type with no button selection if (currentType === "standard" && !reasoningButtonActive && !searchButtonActive) return true; return false; }; const handleUserGuess = () => { const isCorrect = isMessageTypeMatching(); setUserGuessCorrect(isCorrect); setShowFeedback(true); }; const handleSubmit = (e) => { e.preventDefault(); if (!message.trim()) return; // When "Send" is clicked (not in Next state) if (!messagesSent) { setSentMessage(message); setMessagesSent(true); setIsThinking(true); setMessage(''); // Clear input immediately when Send is clicked setTimeout(() => { setIsThinking(false); handleUserGuess(); }, 1500); } }; const handleKeyPress = (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSubmit(e); } }; useEffect(() => { if (!messagesSent) { setAnimatedMessages({ userMessage: false, thinking: false, feedback: false }); } }, [messagesSent]); function UserMessage({ text }) { const shouldAnimate = !animatedMessages.userMessage; useEffect(() => { if (shouldAnimate) { setAnimatedMessages(prev => ({ ...prev, userMessage: true })); } }, [shouldAnimate]); return (

{text}

); } function FeedbackMessage() { const currentType = messageDatabase[currentMessageIndex].type; const shouldBeReasoning = currentType === "reasoning"; const shouldAnimate = !animatedMessages.feedback; useEffect(() => { if (shouldAnimate) { setAnimatedMessages(prev => ({ ...prev, feedback: true })); } }, [shouldAnimate]); if (userGuessCorrect) { return (

🎉 Congratulations! You correctly identified this as a{shouldBeReasoning ? ' reasoning' : ' standard'} prompt.

); } else { return (

❌ Incorrect. This is a{shouldBeReasoning ? ' reasoning' : ' standard'} prompt.

{shouldBeReasoning ? "This prompt requires detailed explanation and step-by-step reasoning to solve the problem." : "This prompt requires a direct, concise answer without detailed explanation."}

); } } function WelcomeModal() { return ( showModal && (

🌿 Did you know?

Every prompt counts! Choosing the right AI model saves energy and protects our planet. 🌎✨

Play smart, pick wisely, and reduce your digital footprint! 🌱💡

) ); } function ChatInput() { const [isNewPrompt, setIsNewPrompt] = useState(false); const initialLoadComplete = useRef(false); // Only run once when component mounts useEffect(() => { // Mark initial load as complete after a short delay const timer = setTimeout(() => { initialLoadComplete.current = true; }, 500); return () => clearTimeout(timer); }, []); const handleButtonClick = (button) => { // Allow toggling between active/inactive states if (button === 'reasoning') { // If already active, deselect it if (reasoningButtonActive) { setReasoningButtonActive(false); } else { // Otherwise, activate it and deactivate the other button setReasoningButtonActive(true); setSearchButtonActive(false); } } else if (button === 'search') { // If already active, deselect it if (searchButtonActive) { setSearchButtonActive(false); } else { // Otherwise, activate it and deactivate the other button setSearchButtonActive(true); setReasoningButtonActive(false); } } }; const handleNextClick = () => { // Only set new prompt flag if initial load is complete if (initialLoadComplete.current) { setIsNewPrompt(true); } // Show the energy animation setShowTopAnimation(true); // Hide animation after a delay setTimeout(() => { setShowTopAnimation(false); }, 3000); loadNewQuestion(); // Reset the new prompt flag after animation if (initialLoadComplete.current) { setTimeout(() => setIsNewPrompt(false), 800); } }; // Simplify class name logic - no complex conditions const getInputClassName = () => { const classes = ['chat-input']; // Only apply new-prompt class if initial load is complete if (initialLoadComplete.current && isNewPrompt) { classes.push('new-prompt'); } return classes.join(' '); }; return (
Use for prompts that require detailed explanations and step-by-step reasoning
Use for prompts that need simple, direct answers without detailed explanation