File size: 4,010 Bytes
ce7e38e
 
 
 
 
7db2a6d
ce7e38e
7db2a6d
ce7e38e
 
 
 
6c0e5ab
5f97286
90c6542
1ccdda2
 
5dabbcf
 
1ccdda2
 
 
 
 
 
 
 
 
 
6c0e5ab
ce7e38e
 
 
6c0e5ab
ce7e38e
 
 
 
24e44fe
f326566
ce7e38e
 
 
 
f326566
 
ce7e38e
80e61b9
 
c70ad0c
80e61b9
ce7e38e
 
 
 
70cfb15
7ec2600
ce7e38e
 
 
70cfb15
 
ce7e38e
 
7db2a6d
 
 
 
 
 
 
 
ce7e38e
7db2a6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce7e38e
ed876e2
 
 
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
import { useState } from "react";
import ChatBox from "./components/ChatBox";
import ChatInput from "./components/ChatInput";
import FileUpload from "./components/FileUpload";
import { streamChat } from "./utils/streamChat";
import { Button, Box, Typography } from "@mui/material";
import "./index.css";
import "./App.css";

export default function App() {
  const [messages, setMessages] = useState([]);
  const [lastCode, setLastCode] = useState("");
  const [loading, setLoading] = useState(false);
  const [resetKey, setResetKey] = useState(0);
  const [fileUploaded, setFileUploaded] = useState(false);

  const detectLanguage = (filename) => {
  if (!filename || typeof filename !== "string") return "";
  const ext = filename.split('.').pop().toLowerCase();
  switch (ext) {
    case "js": return "javascript";
    case "py": return "python";
    case "java": return "java";
    case "cpp": return "cpp";
    case "ts": return "typescript";
    case "cs": return "csharp";
    default: return "";
  }
};
  
  const handleSend = async (msg) => {
    const newHistory = [...messages, { role: "user", content: msg }];
    setMessages(newHistory);
    setLoading(true);


    if (msg.includes("```")) setLastCode(msg);

    let fullContent = "";

    await streamChat({
      message: msg,
      history: messages,
      onChunk: (chunk) => {
        fullContent += chunk;
       setMessages([...newHistory, { role: "assistant", content: fullContent }]);
      },
      onDone: () => {
        setLoading(false); 
        setFileUploaded(false);
      },
    });
  };

  const handleExplain = () => {
    //if (lastCode) handleSend(`Explain this code:\n\n${lastCode}`);
    if (lastCode)handleSend("Explain this code");
  };

  const handleFixBug = () => {
   // if (lastCode) handleSend(`Fix the bug in this code and explain:\n\n${lastCode}`);
   if (lastCode) handleSend("Fix the bug in this code and explain");
  };

   const handleClear = () => {
    setMessages([]);
    setLastCode("");
    setFileUploaded(false);
    setResetKey((k) => k + 1);
  };

 return (
    <div className="app">
       
      
      <div className="chat-container">
        <div className="header-area">
              <Box display="flex" alignItems="center" gap={1} >
         <Typography variant="h4" component="h1" gutterBottom>
                🧑‍💻 DevMate
              </Typography>
        </Box>
              </div>
        <ChatBox messages={messages} loading={loading} />

        <Box className="buttons" 
        sx={{
    display: "flex",
    justifyContent: "space-between",
    alignItems: "center",
    mt: 1,
    ml: 1,
    flexWrap: "wrap",
    gap: 2,
  }}
        >
          <FileUpload
            reset={resetKey}
            onUpload={() => setFileUploaded(true)}
            onCodeParsed={(code, filename) => {
              const lang = detectLanguage(filename);
              const formatted = `\`\`\`${lang}\n${code}\n\`\`\``;
              setLastCode(formatted);
              setMessages((prev) => [
                ...prev,
                { role: "user", content: `Here's my code:\n\n${formatted}` },
              ]);
              setFileUploaded(true);
            }}
          />

          <Box sx={{ display: 'flex', gap: 1 ,mr:1}}>
            <Button
              variant="contained"
              onClick={handleExplain}
              disabled={!fileUploaded || loading}
            >
              Explain Code
            </Button>
            <Button
              variant="contained"
              onClick={handleFixBug}
              disabled={!fileUploaded || loading}
              color="warning"
            >
              Fix Bug
            </Button>
            <Button
              variant="outlined"
              onClick={handleClear}
              color="error"
            >
              Clear Chat
            </Button>
          </Box>
        </Box>

        <ChatInput onSend={handleSend} fileUploaded={fileUploaded} />
      </div>
    </div>
  );
}