{"repo": "ayushghosh02/studyaceapp-frontend", "files": {"src/UserComponent/ChatGemini.jsx": {"cc2f85faeb9ed961382790674d0b4e30a7cd47c5": {"t": 1785674563.0218167, "n": 4698, "h": "32ad45e6c01c35fa", "c": "import { useState, useRef, useEffect } from \"react\";\nimport axios from \"axios\";\nimport ReactMarkdown from \"react-markdown\";\nimport { Container, Row, Col, Form, Button, Card, Spinner } from \"react-bootstrap\";\n\nfunction ChatGemini() {\n const [chatHistory, setChatHistory] = useState([]);\n const [question, setQuestion] = useState(\"\");\n const [answer, setAnswer] = useState(\"\");\n const [generatingAnswer, setGeneratingAnswer] = useState(false);\n\n const chatContainerRef = useRef(null);\n\n useEffect(() => {\n if (chatContainerRef.current) {\n chatContainerRef.current.scrollTop = chatContainerRef.current.scrollHeight;\n }\n }, [chatHistory, generatingAnswer]);\n\n async function generateAnswer(e) {\n e.preventDefault();\n if (!question.trim()) return;\n\n setGeneratingAnswer(true);\n const currentQuestion = question;\n setQuestion(\"\"); // Clear input immediately after sending\n\n // Add user question to chat history\n setChatHistory((prev) => [...prev, { type: \"question\", content: currentQuestion }]);\n\n try {\n const response = await axios({\n url: `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=AIzaSyCEY1wH4krUFnR3ea2gsf2LseB4oCE0YXU`,\n method: \"post\",\n data: {\n contents: [{ parts: [{ text: question }] }],\n },\n });\n\n const aiResponse = response.data.candidates[0].content.parts[0].text;\n setChatHistory((prev) => [...prev, { type: \"answer\", content: aiResponse }]);\n setAnswer(aiResponse);\n } catch (error) {\n console.error(error);\n setAnswer(\"Sorry - Something went wrong. Please try again!\");\n }\n setGeneratingAnswer(false);\n }\n\n return (\n \n \n {/* Header */}\n \n

Learn with Chat AI

\n
\n\n {/* Chat History */}\n \n {chatHistory.length === 0 ? (\n \n \n Welcome to Chat AI! 👋\n \n Ask me about general knowledge, technical questions, writing assistance, or problem-solving.\n Type your question below and click Send!\n \n \n \n ) : (\n chatHistory.map((chat, index) => (\n \n \n {chat.content}\n
\n \n ))\n )}\n {generatingAnswer && (\n
\n \n Thinking...\n \n
\n )}\n \n\n {/* Input Form */}\n \n
\n setQuestion(e.target.value)}\n className=\"flex-grow-1\"\n required\n />\n \n {generatingAnswer ? (\n <>\n \n Sending...\n \n ) : (\n \"Send\"\n )}\n \n \n
\n \n
\n );\n}\n\nexport default ChatGemini;\n"}}}}