|
| 1 | +import React, { useState } from "react"; |
| 2 | +import ChatBubble from "./ChatBubble"; |
| 3 | +import ChatInput from "./ChatInput"; |
| 4 | +import { askCodeMate } from "./chatApi"; |
| 5 | +import "./chatbot.css"; |
| 6 | + |
| 7 | +const LANGUAGES = ["javascript", "python", "cpp", "java"]; // add more if needed |
| 8 | + |
| 9 | +export default function CodeMate() { |
| 10 | + const [open, setOpen] = useState(false); |
| 11 | + const [fullScreen, setFullScreen] = useState(false); |
| 12 | + const [selectedLang, setSelectedLang] = useState("javascript"); |
| 13 | + |
| 14 | + const [messages, setMessages] = useState([ |
| 15 | + { |
| 16 | + role: "system", |
| 17 | + content: ` |
| 18 | +You are CodeMate, an AI coding assistant inside an online compiler. |
| 19 | +
|
| 20 | +Strict rules: |
| 21 | +1. Always provide **three approaches** for every logic question: |
| 22 | + 1. Brute Force |
| 23 | + 2. Better Approach |
| 24 | + 3. Optimal Approach |
| 25 | +2. Always explain **time and space complexity** for each approach. |
| 26 | +3. Always write code in the **programming language selected by the user** (use the variable "selectedLang"). |
| 27 | +4. Respond in simple, beginner-friendly language. |
| 28 | +5. Use Markdown formatting with numbered lists and code blocks. |
| 29 | +
|
| 30 | +Example: |
| 31 | +1. Brute Force |
| 32 | +\`\`\`${selectedLang} |
| 33 | +// code here |
| 34 | +\`\`\` |
| 35 | +Time Complexity: O(...) |
| 36 | +Space Complexity: O(...) |
| 37 | +
|
| 38 | +2. Better Approach |
| 39 | +\`\`\`${selectedLang} |
| 40 | +// code here |
| 41 | +\`\`\` |
| 42 | +Time Complexity: O(...) |
| 43 | +Space Complexity: O(...) |
| 44 | +
|
| 45 | +3. Optimal Approach |
| 46 | +\`\`\`${selectedLang} |
| 47 | +// code here |
| 48 | +\`\`\` |
| 49 | +Time Complexity: O(...) |
| 50 | +Space Complexity: O(...) |
| 51 | +` |
| 52 | + }, |
| 53 | + { |
| 54 | + role: "assistant", |
| 55 | + content: "Hi 👋 I'm CodeMate. Ask me about logic, optimization, or errors." |
| 56 | + } |
| 57 | + ]); |
| 58 | + |
| 59 | + const sendMessage = async (text) => { |
| 60 | + if (!text.trim()) return; |
| 61 | + |
| 62 | + const userMsg = { role: "user", content: text }; |
| 63 | + const updated = [...messages, userMsg]; |
| 64 | + setMessages(updated); |
| 65 | + |
| 66 | + try { |
| 67 | + // Always pass the language to the prompt |
| 68 | + const reply = await askCodeMate([ |
| 69 | + ...updated, |
| 70 | + { role: "system", content: `Use ${selectedLang} for all code.` } |
| 71 | + ]); |
| 72 | + |
| 73 | + setMessages((prev) => [...prev, { role: "assistant", content: reply }]); |
| 74 | + } catch (err) { |
| 75 | + setMessages((prev) => [ |
| 76 | + ...prev, |
| 77 | + { role: "assistant", content: "⚠️ Error connecting to Groq API." }, |
| 78 | + ]); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + return ( |
| 83 | + <> |
| 84 | + <button className="chat-toggle" onClick={() => setOpen(!open)}> |
| 85 | + 💬 |
| 86 | + </button> |
| 87 | + |
| 88 | + {open && ( |
| 89 | + <div className={`chat-container ${fullScreen ? "fullscreen" : ""}`}> |
| 90 | + <div className="chat-header"> |
| 91 | + <span>CodeMate AI</span> |
| 92 | + <div className="chat-header-buttons"> |
| 93 | + <select |
| 94 | + value={selectedLang} |
| 95 | + onChange={(e) => setSelectedLang(e.target.value)} |
| 96 | + className="language-select" |
| 97 | + > |
| 98 | + {LANGUAGES.map((lang) => ( |
| 99 | + <option key={lang} value={lang}> |
| 100 | + {lang.toUpperCase()} |
| 101 | + </option> |
| 102 | + ))} |
| 103 | + </select> |
| 104 | + <button onClick={() => setFullScreen(!fullScreen)}> |
| 105 | + {fullScreen ? "🗗" : "🗖"} |
| 106 | + </button> |
| 107 | + <button onClick={() => setOpen(false)}>✖</button> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + |
| 111 | + <div className="chat-body"> |
| 112 | + {messages |
| 113 | + .filter((m) => m.role !== "system") |
| 114 | + .map((msg, i) => ( |
| 115 | + <ChatBubble key={i} message={msg} /> |
| 116 | + ))} |
| 117 | + </div> |
| 118 | + |
| 119 | + <ChatInput onSend={sendMessage} /> |
| 120 | + </div> |
| 121 | + )} |
| 122 | + </> |
| 123 | + ); |
| 124 | +} |
0 commit comments