import React, { useState, useEffect } from 'react'; import { FaPaperPlane } from "react-icons/fa"; import { FiMenu } from 'react-icons/fi'; import { SunIcon, MoonIcon } from '@heroicons/react/24/outline'; import chatbotIcon from './assets/chatbot-icon.png'; import './App.css'; import logo from './assets/chatbot-icon.png'; import ReactMarkdown from "react-markdown"; function App() { const [userInput, setUserInput] = useState(''); const [chatLog, setChatLog] = useState([]); const [loading, setLoading] = useState(false); const [darkMode, setDarkMode] = useState(false); // Generate a sessionId for this browser session const [sessionId] = useState(() => `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`); //console.log(sessionId); // Load theme from localStorage on mount useEffect(() => { const savedTheme = localStorage.getItem('theme'); if (savedTheme === 'dark') { setDarkMode(true); document.documentElement.classList.add('dark'); } }, []); // On mount, start with an empty chat window (do NOT load previous messages) useEffect(() => { //localStorage.removeItem('chatLog'); // Optional: ensures no old data setChatLog([]); }, []); // Toggle theme and persist const toggleTheme = () => { const newTheme = darkMode ? 'light' : 'dark'; setDarkMode(!darkMode); localStorage.setItem('theme', newTheme); document.documentElement.classList.toggle('dark', newTheme === 'dark'); }; const handleSubmit = async (event) => { event.preventDefault(); if (!userInput.trim()) return; // Don't send empty messages const userMessage = { type: 'user', text: userInput, ts: Date.now(), sessionId }; const newChatLog = [...chatLog, userMessage]; setChatLog(newChatLog); setUserInput(''); setLoading(true); try { // The API call to our FastAPI backend const response = await fetch('https://nova-chatbot-chatbot-backend.hf.space/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ user_message: userInput }), }); //console.log("response",response); //console.log("response.json()",response.json()) //if (!response.ok) { // Read raw text so HTML errors don’t crash JSON parsing // const text = await response.text(); // throw new Error(`Backend ${response.status}: ${text}`); //} else { // data = await response.json(); // safe now //} if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); //consoloe.log("data",data) const botMessage = { type: 'bot', text: data.bot_response, ts: Date.now(), sessionId }; // Update the chat log with the bot's response const finalChatLog = [...newChatLog, botMessage]; setChatLog(finalChatLog); // Save both messages to history (not loaded on refresh) const history = JSON.parse(localStorage.getItem('chatHistory') || '[]'); const updatedHistory = [...history, userMessage, botMessage]; localStorage.setItem('chatHistory', JSON.stringify(updatedHistory)); } catch (error) { console.error('Error fetching chat response:', error); const errorMessage = { type: 'error', text: 'Sorry, something went wrong. Please try again.' }; setChatLog(prev => [...prev, errorMessage]); // Save error to history if needed const history = JSON.parse(localStorage.getItem('chatHistory') || '[]'); localStorage.setItem('chatHistory', JSON.stringify([...history, errorMessage])); } finally { setLoading(false); } }; return (
Logo

NOVA

{chatLog.map((message, index) => (
{message.text}
))} {loading &&
Loading...
}
setUserInput(e.target.value)} placeholder="Type your message..." disabled={loading} /> {/**/}
); } export default App;