Spaces:
Running
Running
Commit ·
1ccdda2
1
Parent(s): 24e44fe
commit 014
Browse files- src/App.js +26 -8
- src/components/CodeBlock.jsx +12 -1
src/App.js
CHANGED
|
@@ -9,14 +9,25 @@ export default function App() {
|
|
| 9 |
const [messages, setMessages] = useState([]);
|
| 10 |
const [lastCode, setLastCode] = useState("");
|
| 11 |
const [loading, setLoading] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
const handleSend = async (msg) => {
|
| 14 |
const newHistory = [...messages, { role: "user", content: msg }];
|
| 15 |
setMessages(newHistory);
|
| 16 |
setLoading(true);
|
| 17 |
|
| 18 |
-
// let botMsg = { role: "assistant", content: "" };
|
| 19 |
-
//setMessages((prev) => [...prev, botMsg]);
|
| 20 |
|
| 21 |
if (msg.includes("```")) setLastCode(msg);
|
| 22 |
|
|
@@ -27,7 +38,6 @@ export default function App() {
|
|
| 27 |
history: messages,
|
| 28 |
onChunk: (chunk) => {
|
| 29 |
fullContent += chunk;
|
| 30 |
-
// setMessages((prev) => [...prev.slice(0, -1), { ...botMsg }]);
|
| 31 |
setMessages([...newHistory, { role: "assistant", content: fullContent }]);
|
| 32 |
},
|
| 33 |
onDone: () => {
|
|
@@ -54,14 +64,22 @@ export default function App() {
|
|
| 54 |
<div className="main">
|
| 55 |
<ChatBox messages={messages} loading={loading}/>
|
| 56 |
<div className="buttons">
|
| 57 |
-
<FileUpload onCodeParsed={(code) => {
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
<button onClick={handleExplain}>Explain Code</button>
|
| 63 |
<button onClick={handleFixBug}>Fix Bug</button>
|
| 64 |
{/* <button onClick={handleStackOverflow}>StackOverflow</button> */}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
</div>
|
| 66 |
<ChatInput onSend={handleSend} />
|
| 67 |
</div>
|
|
|
|
| 9 |
const [messages, setMessages] = useState([]);
|
| 10 |
const [lastCode, setLastCode] = useState("");
|
| 11 |
const [loading, setLoading] = useState(false);
|
| 12 |
+
|
| 13 |
+
const detectLanguage = (filename) => {
|
| 14 |
+
const ext = filename.split('.').pop();
|
| 15 |
+
switch (ext) {
|
| 16 |
+
case "js": return "javascript";
|
| 17 |
+
case "py": return "python";
|
| 18 |
+
case "java": return "java";
|
| 19 |
+
case "cpp": return "cpp";
|
| 20 |
+
case "ts": return "typescript";
|
| 21 |
+
case "cs": return "csharp";
|
| 22 |
+
default: return "";
|
| 23 |
+
}
|
| 24 |
+
};
|
| 25 |
|
| 26 |
const handleSend = async (msg) => {
|
| 27 |
const newHistory = [...messages, { role: "user", content: msg }];
|
| 28 |
setMessages(newHistory);
|
| 29 |
setLoading(true);
|
| 30 |
|
|
|
|
|
|
|
| 31 |
|
| 32 |
if (msg.includes("```")) setLastCode(msg);
|
| 33 |
|
|
|
|
| 38 |
history: messages,
|
| 39 |
onChunk: (chunk) => {
|
| 40 |
fullContent += chunk;
|
|
|
|
| 41 |
setMessages([...newHistory, { role: "assistant", content: fullContent }]);
|
| 42 |
},
|
| 43 |
onDone: () => {
|
|
|
|
| 64 |
<div className="main">
|
| 65 |
<ChatBox messages={messages} loading={loading}/>
|
| 66 |
<div className="buttons">
|
| 67 |
+
<FileUpload onCodeParsed={(code, filename) => {
|
| 68 |
+
const lang = detectLanguage(filename);
|
| 69 |
+
const formatted = `\`\`\`${lang}\n${code}\n\`\`\``;
|
| 70 |
+
setLastCode(formatted);
|
| 71 |
+
setMessages((prev) => [
|
| 72 |
+
...prev,
|
| 73 |
+
{ role: "user", content: `Here's my code:\n\n${formatted}` },
|
| 74 |
+
]);
|
| 75 |
+
}} />
|
| 76 |
<button onClick={handleExplain}>Explain Code</button>
|
| 77 |
<button onClick={handleFixBug}>Fix Bug</button>
|
| 78 |
{/* <button onClick={handleStackOverflow}>StackOverflow</button> */}
|
| 79 |
+
<button onClick={() => {
|
| 80 |
+
setMessages([]);
|
| 81 |
+
setLastCode("");
|
| 82 |
+
}}>Clear Chat</button>
|
| 83 |
</div>
|
| 84 |
<ChatInput onSend={handleSend} />
|
| 85 |
</div>
|
src/components/CodeBlock.jsx
CHANGED
|
@@ -1,9 +1,20 @@
|
|
| 1 |
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
|
|
|
|
| 2 |
import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism";
|
| 3 |
|
| 4 |
export default function CodeBlock({ code }) {
|
| 5 |
return (
|
| 6 |
-
<SyntaxHighlighter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
{code}
|
| 8 |
</SyntaxHighlighter>
|
| 9 |
);
|
|
|
|
| 1 |
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
|
| 2 |
+
import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism";
|
| 3 |
import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism";
|
| 4 |
|
| 5 |
export default function CodeBlock({ code }) {
|
| 6 |
return (
|
| 7 |
+
<SyntaxHighlighter
|
| 8 |
+
language="javascript" // fallback default
|
| 9 |
+
style={oneDark}
|
| 10 |
+
showLineNumbers
|
| 11 |
+
wrapLines
|
| 12 |
+
customStyle={{
|
| 13 |
+
fontSize: "0.9rem",
|
| 14 |
+
borderRadius: "8px",
|
| 15 |
+
padding: "1em",
|
| 16 |
+
}}
|
| 17 |
+
>
|
| 18 |
{code}
|
| 19 |
</SyntaxHighlighter>
|
| 20 |
);
|