File size: 13,730 Bytes
0935676
 
9495107
0935676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56a9143
0935676
 
9495107
0935676
 
 
 
9495107
0935676
 
 
 
 
 
 
 
 
9495107
0935676
 
 
 
 
56a9143
9495107
0935676
56a9143
0935676
 
 
 
 
 
9495107
 
0935676
 
 
 
 
 
 
 
 
 
56a9143
0935676
 
 
 
 
 
 
 
 
56a9143
0935676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9495107
0935676
 
 
 
 
 
 
 
56a9143
 
 
 
 
 
 
 
0935676
 
56a9143
9495107
0935676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9495107
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import { useState, useRef, useEffect } from "react";

const API_URL = import.meta.env.VITE_API_URL || "https://happy4040-mock-technical-interviewer.hf.space";

function Markdown({ text }) {
  const html = (text || "")
    .replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>")
    .replace(/\*(.+?)\*/g, "<em>$1</em>")
    .replace(/`{3}[\w]*\n?([\s\S]*?)`{3}/g, "<pre><code>$1</code></pre>")
    .replace(/`([^`]+)`/g, "<code>$1</code>")
    .replace(/^### (.+)$/gm, "<h3>$1</h3>")
    .replace(/^## (.+)$/gm, "<h2>$1</h2>")
    .replace(/^# (.+)$/gm, "<h1>$1</h1>")
    .replace(/^---$/gm, "<hr/>")
    .replace(/^\*   (.+)$/gm, "<li>$1</li>")
    .replace(/\n\n/g, "</p><p>")
    .replace(/\n/g, "<br/>");
  return <div className="md-body" dangerouslySetInnerHTML={{ __html: `<p>${html}</p>` }} />;
}

function Whiteboard({ onCapture }) {
  const canvasRef = useRef(null);
  const drawing = useRef(false);
  const lastPos = useRef(null);
  const [color, setColor] = useState("#f0f4ff");
  const [lineWidth, setLineWidth] = useState(3);
  const [eraser, setEraser] = useState(false);

  const getPos = (e, canvas) => {
    const rect = canvas.getBoundingClientRect();
    const scaleX = canvas.width / rect.width;
    const scaleY = canvas.height / rect.height;
    if (e.touches) return { x: (e.touches[0].clientX - rect.left) * scaleX, y: (e.touches[0].clientY - rect.top) * scaleY };
    return { x: (e.clientX - rect.left) * scaleX, y: (e.clientY - rect.top) * scaleY };
  };

  const startDraw = (e) => { e.preventDefault(); drawing.current = true; lastPos.current = getPos(e, canvasRef.current); };
  const draw = (e) => {
    e.preventDefault();
    if (!drawing.current) return;
    const canvas = canvasRef.current;
    const ctx = canvas.getContext("2d");
    const pos = getPos(e, canvas);
    ctx.beginPath(); ctx.moveTo(lastPos.current.x, lastPos.current.y); ctx.lineTo(pos.x, pos.y);
    ctx.strokeStyle = eraser ? "#0e1117" : color; ctx.lineWidth = eraser ? 20 : lineWidth;
    ctx.lineCap = "round"; ctx.lineJoin = "round"; ctx.stroke();
    lastPos.current = pos;
  };
  const stopDraw = () => { drawing.current = false; };
  const clear = () => {
    const canvas = canvasRef.current; const ctx = canvas.getContext("2d");
    ctx.fillStyle = "#0e1117"; ctx.fillRect(0, 0, canvas.width, canvas.height);
  };
  useEffect(() => { clear(); }, []);
  const capture = () => { const b64 = canvasRef.current.toDataURL("image/png").split(",")[1]; onCapture(b64); };

  return (
    <div className="whiteboard-wrap">
      <div className="wb-toolbar">
        <span className="wb-label">Whiteboard</span>
        <div className="color-swatches">
          {["#f0f4ff","#7ee8a2","#ffd166","#ef476f","#06d6a0","#ffffff"].map(c => (
            <button key={c} className={`swatch${color === c && !eraser ? " active" : ""}`}
              style={{ background: c }} onClick={() => { setColor(c); setEraser(false); }} />
          ))}
        </div>
        <input type="range" min="1" max="12" value={lineWidth} onChange={e => setLineWidth(+e.target.value)} className="size-slider" title="Brush size" />
        <button className={`wb-btn${eraser ? " active" : ""}`} onClick={() => setEraser(!eraser)}>Erase</button>
        <button className="wb-btn" onClick={clear}>Clear</button>
        <button className="wb-btn send-wb" onClick={capture}>Send to AI</button>
      </div>
      <canvas ref={canvasRef} width={900} height={340} className="wb-canvas"
        onMouseDown={startDraw} onMouseMove={draw} onMouseUp={stopDraw} onMouseLeave={stopDraw}
        onTouchStart={startDraw} onTouchMove={draw} onTouchEnd={stopDraw} />
    </div>
  );
}

function Message({ role, text }) {
  return (
    <div className={`msg ${role}`}>
      <div className="msg-avatar">{role === "ai" ? "AI" : "You"}</div>
      <div className="msg-bubble">{role === "ai" ? <Markdown text={text} /> : <p>{text}</p>}</div>
    </div>
  );
}

function CodeEditor({ value, onChange }) {
  return (
    <div className="code-editor-wrap">
      <div className="code-editor-header">
        <span className="dot red"/><span className="dot yellow"/><span className="dot green"/>
        <span className="code-lang">Python</span>
      </div>
      <textarea className="code-editor" value={value} onChange={e => onChange(e.target.value)}
        spellCheck={false} placeholder="# Write your solution here..." />
    </div>
  );
}

export default function App() {
  const [screen, setScreen] = useState("home");
  const [apiKey, setApiKey] = useState("");
  const [sessionId, setSessionId] = useState(null);
  const [messages, setMessages] = useState([]);
  const [problem, setProblem] = useState("");
  const [code, setCode] = useState("# Your solution here\n");
  const [codeChanged, setCodeChanged] = useState(false);
  const [input, setInput] = useState("");
  const [loading, setLoading] = useState(false);
  const [starting, setStarting] = useState(false);
  const [finished, setFinished] = useState(false);
  const [report, setReport] = useState("");
  const [showWb, setShowWb] = useState(false);
  const [error, setError] = useState("");
  const chatEndRef = useRef(null);

  useEffect(() => { chatEndRef.current?.scrollIntoView({ behavior: "smooth" }); }, [messages]);

  const apiFetch = (path, opts = {}) =>
    fetch(`${API_URL}${path}`, {
      ...opts,
      headers: { "Content-Type": "application/json", ...(opts.headers || {}) },
    });

  const startSession = async () => {
    if (!apiKey.trim()) { setError("Please enter your Google Gemini API key."); return; }
    setError(""); setStarting(true);
    try {
      const res = await apiFetch("/api/session/start", { method: "POST", body: JSON.stringify({ api_key: apiKey.trim() }) });
      if (!res.ok) throw new Error(await res.text());
      const data = await res.json();
      setSessionId(data.session_id);
      setMessages([{ role: "ai", text: data.message }]);
      setScreen("interview");
    } catch (e) {
      setError("Could not connect to the server. Please try again.");
    } finally { setStarting(false); }
  };

  const sendMessage = async (extraImageBase64 = null) => {
    if (!input.trim() && !codeChanged && !extraImageBase64) return;
    const userText = input.trim();
    setInput("");
    const userMsg = userText || (extraImageBase64 ? "[Whiteboard sent]" : "[Code updated]");
    setMessages(m => [...m, { role: "user", text: userMsg }]);
    setLoading(true);
    try {
      const body = { session_id: sessionId, message: userText, code, code_changed: codeChanged, image_base64: extraImageBase64 || null, api_key: apiKey.trim() };
      setCodeChanged(false);
      const res = await apiFetch("/api/chat", { method: "POST", body: JSON.stringify(body) });
      if (!res.ok) throw new Error(await res.text());
      const data = await res.json();
      setMessages(m => [...m, { role: "ai", text: data.message }]);
      if (data.problem && !data.problem.includes("not been selected")) setProblem(data.problem);
      if (data.code && data.code !== "# Your code here") setCode(data.code);
      if (data.finished) { setFinished(true); if (data.report) setReport(data.report); }
    } catch (e) {
      setMessages(m => [...m, { role: "ai", text: `Something went wrong. Please try again in a moment.` }]);
    } finally { setLoading(false); }
  };

  const handleWbCapture = (b64) => { setShowWb(false); sendMessage(b64); };
  const handleKeyDown = (e) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); sendMessage(); } };
  const downloadReport = () => {
    const blob = new Blob([report], { type: "text/markdown" });
    const a = document.createElement("a"); a.href = URL.createObjectURL(blob);
    a.download = "interview_report.md"; a.click();
  };

  if (screen === "home") return (
    <div className="home-screen">
      <div className="home-card">
        <div className="logo-mark">⬑</div>
        <h1 className="home-title">Mock Technologie<br/><span>Interview Suite</span></h1>
        <p className="home-sub">Practice real technical interviews with an AI interviewer powered by Google Gemini. Get hints, use the whiteboard, write code, and receive a full evaluation report.</p>

        <div className="features-row">
          <div className="feat"><span>🧠</span><p>Gemini AI</p></div>
          <div className="feat"><span>πŸ–Š</span><p>Whiteboard</p></div>
          <div className="feat"><span>πŸ’»</span><p>Code Editor</p></div>
          <div className="feat"><span>πŸ“Š</span><p>Report</p></div>
        </div>

        <div className="config-section">
          <label className="input-label">Google Gemini API Key</label>
          <input type="password" className="config-input" placeholder="AIza..."
            value={apiKey} onChange={e => setApiKey(e.target.value)}
            onKeyDown={e => e.key === "Enter" && startSession()} />
          <p className="key-hint">Free key at <a href="https://aistudio.google.com/apikey" target="_blank" rel="noreferrer">aistudio.google.com/apikey</a> β€” your quota, your usage</p>
        </div>

        {error && <p className="err-msg">{error}</p>}

        <button className="start-btn" onClick={startSession} disabled={starting || !apiKey.trim()}>
          {starting ? <span className="spinner"/> : "Start Interview β†’"}
        </button>
      </div>
      <div className="home-bg">
        {Array.from({length:24}).map((_,i)=>(
          <div key={i} className="bg-orb" style={{animationDelay:`${i*0.3}s`, left:`${(i*41)%100}%`, top:`${(i*31)%100}%`}}/>
        ))}
      </div>
    </div>
  );

  return (
    <div className="app">
      <header className="app-header">
        <div className="header-brand"><span className="header-logo">⬑</span><span>Mock Technologie Inc.</span></div>
        <div className="header-status">
          {finished
            ? <span className="badge done">βœ“ Complete</span>
            : <span className="badge live"><span className="pulse"/>Live</span>}
        </div>
        {finished && (
          <div className="header-actions">
            <button className="hdr-btn" onClick={downloadReport}>⬇ Download Report</button>
            <button className="hdr-btn accent" onClick={() => setScreen("report")}>View Report</button>
          </div>
        )}
      </header>

      <div className="layout">
        <div className="left-panel">
          <div className="problem-box">
            <div className="problem-header"><span>πŸ“‹</span><span>Problem Statement</span></div>
            <div className="problem-body">
              {!problem || problem === "No problem selected yet." || problem === "Problem has not been selected yet"
                ? <p className="no-problem">A question will appear here once selected.</p>
                : <Markdown text={problem} />}
            </div>
          </div>

          <div className="chat-box">
            <div className="chat-messages">
              {messages.map((m, i) => <Message key={i} role={m.role} text={m.text} />)}
              {loading && (
                <div className="msg ai">
                  <div className="msg-avatar">AI</div>
                  <div className="msg-bubble typing"><span/><span/><span/></div>
                </div>
              )}
              <div ref={chatEndRef}/>
            </div>
            <div className="chat-input-row">
              <textarea className="chat-input" rows={3}
                placeholder="Type your message… (Enter to send, Shift+Enter for newline)"
                value={input} onChange={e => setInput(e.target.value)}
                onKeyDown={handleKeyDown} disabled={loading || finished} />
              <div className="chat-actions">
                <button className="icon-btn" title="Whiteboard" onClick={() => setShowWb(v => !v)} disabled={finished}>πŸ–Š</button>
                <button className="send-btn" onClick={() => sendMessage()}
                  disabled={loading || finished || (!input.trim() && !codeChanged)}>
                  {loading ? <span className="spinner sm"/> : "Send β†’"}
                </button>
              </div>
            </div>
          </div>
        </div>

        <div className="right-panel">
          <div className="panel-header">
            <span>Code Editor</span>
            {codeChanged && <span className="changed-badge">● unsent changes</span>}
          </div>
          <CodeEditor value={code} onChange={v => { setCode(v); setCodeChanged(true); }}/>
          <button className="submit-code-btn" onClick={() => sendMessage()} disabled={loading || finished || !codeChanged}>
            Submit Code Update
          </button>
        </div>
      </div>

      {showWb && (
        <div className="wb-overlay">
          <div className="wb-modal">
            <div className="wb-modal-header">
              <span>Whiteboard β€” Draw your approach</span>
              <button className="close-btn" onClick={() => setShowWb(false)}>βœ•</button>
            </div>
            <Whiteboard onCapture={handleWbCapture}/>
          </div>
        </div>
      )}

      {screen === "report" && (
        <div className="report-overlay">
          <div className="report-modal">
            <div className="report-modal-header">
              <span>πŸ“Š Interview Evaluation Report</span>
              <div style={{display:"flex",gap:"8px"}}>
                <button className="hdr-btn" onClick={downloadReport}>⬇ Download</button>
                <button className="close-btn" onClick={() => setScreen("interview")}>βœ•</button>
              </div>
            </div>
            <div className="report-body"><Markdown text={report || "Generating report…"} /></div>
          </div>
        </div>
      )}
    </div>
  );
}