Spaces:
Running
Running
File size: 12,231 Bytes
979db68 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
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 (
<div className={`message user-message ${shouldAnimate ? 'message-send-animation' : ''}`}>
<p>{text}</p>
</div>
);
}
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 (
<div className={`message feedback-message success ${shouldAnimate ? 'message-send-animation' : ''}`}>
<p>π Congratulations! You correctly identified this as a{shouldBeReasoning ? ' reasoning' : ' standard'} prompt.</p>
</div>
);
} else {
return (
<div className={`message feedback-message error ${shouldAnimate ? 'message-send-animation' : ''}`}>
<p>β Incorrect. This is a{shouldBeReasoning ? ' reasoning' : ' standard'} prompt.</p>
<p className="error-explanation">
{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."}
</p>
</div>
);
}
}
function WelcomeModal() {
return (
showModal && (
<div className="modal-overlay">
<div className="modal">
<h2>πΏ Did you know? </h2>
<p>Every prompt counts! Choosing the right AI model saves energy and protects our planet. πβ¨</p>
<img src="images/cute_world_mini.png" style={{ width: '55%' }} />
<p>Play smart, pick wisely, and reduce your digital footprint! π±π‘</p>
<button onClick={() => setShowModal(false)}>Start</button>
</div>
</div>
)
);
}
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 (
<div className="chat-input-container">
<form className="chat-input-wrapper" onSubmit={handleSubmit}>
<div className="button-group">
<div className="tooltip-wrapper">
<button
onClick={() => handleButtonClick('reasoning')}
className={`standard-button reasoning-button ${reasoningButtonActive ? 'active' : ''}`}
type="button"
disabled={messagesSent}
>
Reason
</button>
<div className="tooltip">
Use for prompts that require detailed explanations and step-by-step reasoning
</div>
</div>
<div className="tooltip-wrapper">
<button
onClick={() => handleButtonClick('search')}
className={`standard-button search-button ${searchButtonActive ? 'active' : ''}`}
type="button"
disabled={messagesSent}
>
Search
</button>
<div className="tooltip">
Use for prompts that need simple, direct answers without detailed explanation
</div>
</div>
</div>
<div className="input-with-button">
<div className="tooltip-wrapper input-tooltip-wrapper">
<textarea
className={getInputClassName()}
value={showModal ? '' : message}
onKeyPress={handleKeyPress}
placeholder="Click next for next prompt"
rows="3"
readOnly={!messagesSent && message === messageDatabase[currentMessageIndex].question}
/>
<div className="tooltip input-tooltip">
First select a model type above, then press Send to submit your answer
</div>
</div>
<button
type={messagesSent && !showFeedback ? "button" : "submit"}
className="answer-button"
onClick={messagesSent && showFeedback ? handleNextClick : undefined}
disabled={messagesSent && !showFeedback}
>
{messagesSent && showFeedback ? 'Next' : 'Send'}
</button>
</div>
</form>
</div>
);
}
function ThinkingAnimation() {
const shouldAnimate = !animatedMessages.thinking;
useEffect(() => {
if (shouldAnimate) {
setAnimatedMessages(prev => ({ ...prev, thinking: true }));
}
}, [shouldAnimate]);
return (
<div className={`thinking ${shouldAnimate ? 'message-send-animation' : ''}`}>
<div className="dot"></div>
<div className="dot"></div>
<div className="dot"></div>
</div>
);
}
// Updated CompanyLogo component with both logos at the bottom
function CompanyLogo() {
return (
<>
<img
src="images/cotec.png"
alt="Company Logo Right"
className="company-logo company-logo-right"
/>
<img
src="images/upm4.png"
alt="Company Logo Left"
className="company-logo company-logo-left"
/>
</>
);
}
function EnergyAnimation() {
return (
<div className={`energy-animation ${showTopAnimation ? 'show' : ''}`}>
<div className="energy-icon">
{userGuessCorrect ? 'π³' : 'ποΈ'}
</div>
<div className="energy-text">
{userGuessCorrect ? 'Energy saved! Good choice!' : 'Choose wisely next time to save energy!'}
</div>
</div>
);
}
return (
<>
<WelcomeModal />
<EnergyAnimation />
<div className="chat-container">
<div className="messages-container">
{messagesSent && <UserMessage text={`"${sentMessage}"`} />}
{messagesSent && isThinking && <ThinkingAnimation />}
{messagesSent && showFeedback && !isThinking && <FeedbackMessage />}
</div>
<ChatInput />
</div>
<CompanyLogo />
</>
);
}
|