-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.tsx
More file actions
executable file
·329 lines (300 loc) · 13 KB
/
Copy pathApp.tsx
File metadata and controls
executable file
·329 lines (300 loc) · 13 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import React, { useState, useEffect, useRef } from 'react';
import Header from './components/Header';
import Loader from './components/Loader';
import FeedbackDisplay from './components/FeedbackDisplay';
import { SUPPORTED_LANGUAGES } from './constants';
import { reviewCodeWithGemini } from './services/geminiService';
declare global {
interface Window {
hljs: any;
}
}
const App: React.FC = () => {
const [code, setCode] = useState<string>('');
const [language, setLanguage] = useState<string>('javascript');
const [feedback, setFeedback] = useState<string>('');
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
const [ollamaEndpoint, setOllamaEndpoint] = useState<string>('http://localhost:11434/api/generate');
const [isSendingToOllama, setIsSendingToOllama] = useState<boolean>(false);
const [ollamaStatus, setOllamaStatus] = useState<string | null>(null);
const [highlightedCode, setHighlightedCode] = useState('');
const [copyButtonText, setCopyButtonText] = useState('Copy Code');
const preRef = useRef<HTMLPreElement>(null);
const textAreaRef = useRef<HTMLTextAreaElement>(null);
const detectionTimeoutRef = useRef<number | null>(null);
const cursorRef = useRef<number | null>(null);
const languageUpdateFromAutoDetect = useRef(false);
// Load state from localStorage on initial mount
useEffect(() => {
try {
const savedCode = localStorage.getItem('gemini-code-reviewer-code');
const savedLanguage = localStorage.getItem('gemini-code-reviewer-language');
if (savedCode) {
setCode(savedCode);
}
if (savedLanguage && SUPPORTED_LANGUAGES.some(lang => lang.value === savedLanguage)) {
setLanguage(savedLanguage);
}
} catch (e) {
console.error('Could not load from localStorage', e);
}
}, []);
// Save code to localStorage on change
useEffect(() => {
try {
// Don't save the initial empty state on first load if there's nothing there
if (code || localStorage.getItem('gemini-code-reviewer-code')) {
localStorage.setItem('gemini-code-reviewer-code', code);
}
} catch (e) {
console.error('Could not save code to localStorage', e);
}
}, [code]);
// Save language to localStorage on change
useEffect(() => {
try {
localStorage.setItem('gemini-code-reviewer-language', language);
} catch (e) {
console.error('Could not save language to localStorage', e);
}
}, [language]);
useEffect(() => {
// Initial check for API key on mount
if (!process.env.API_KEY) {
setError("CRITICAL ERROR: API_KEY environment variable not found. The application cannot function without it.");
}
}, []);
// Effect to set cursor position after state updates from keydown handlers
useEffect(() => {
if (textAreaRef.current && cursorRef.current !== null) {
textAreaRef.current.selectionStart = cursorRef.current;
textAreaRef.current.selectionEnd = cursorRef.current;
cursorRef.current = null; // Reset after use
}
}, [highlightedCode]); // Depend on highlightedCode as it re-renders after code state changes
const normalizeLanguage = (lang: string): string => {
const langMap: { [key: string]: string } = {
js: 'javascript',
jsx: 'javascript',
ts: 'typescript',
tsx: 'typescript',
py: 'python',
golang: 'go',
rs: 'rust',
xml: 'html', // hljs detects html as xml
cs: 'csharp',
'c++': 'cpp',
rb: 'ruby',
kt: 'kotlin',
};
return langMap[lang] || lang;
};
// Effect 1: Auto-detect language and highlight on code change (debounced)
useEffect(() => {
if (detectionTimeoutRef.current) clearTimeout(detectionTimeoutRef.current);
detectionTimeoutRef.current = window.setTimeout(() => {
if (code.trim()) {
const result = window.hljs.highlightAuto(code);
const detectedLang = normalizeLanguage(result.language || 'plaintext');
if (SUPPORTED_LANGUAGES.some(l => l.value === detectedLang) && detectedLang !== language) {
languageUpdateFromAutoDetect.current = true;
setLanguage(detectedLang);
}
setHighlightedCode(window.hljs.highlight(code, { language: detectedLang, ignoreIllegals: true }).value);
} else {
setHighlightedCode('');
}
}, 200);
return () => {
if (detectionTimeoutRef.current) {
clearTimeout(detectionTimeoutRef.current);
}
};
}, [code]);
// Re-run highlighting if language is manually changed
useEffect(() => {
if (languageUpdateFromAutoDetect.current) {
languageUpdateFromAutoDetect.current = false;
return;
}
if (code.trim()) {
setHighlightedCode(window.hljs.highlight(code, { language, ignoreIllegals: true }).value);
} else {
setHighlightedCode('');
}
}, [language]);
const handleCodeChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setCode(e.target.value);
};
const handleLanguageChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
setLanguage(e.target.value);
};
const handleReview = async () => {
if (isLoading) return;
setIsLoading(true);
setError(null);
setFeedback('');
try {
const result = await reviewCodeWithGemini(code, language);
setFeedback(result);
} catch (err) {
setError(err instanceof Error ? err.message : 'An unknown error occurred.');
} finally {
setIsLoading(false);
}
};
const handleSendToOllama = async () => {
if (!feedback) {
setOllamaStatus('Please review with Gemini first.');
return;
}
setIsSendingToOllama(true);
setOllamaStatus('Sending to Ollama...');
try {
const response = await fetch(ollamaEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'llama2', // or your desired model
prompt: `Here is a code review from Gemini. Please summarize it and provide any additional feedback you have:\n\n${feedback}`,
stream: false
}),
});
if (!response.ok) {
throw new Error(`Ollama API error: ${response.statusText}`);
}
const data = await response.json();
setOllamaStatus(`Ollama Response:\n${data.response}`);
} catch (err) {
setOllamaStatus(`Error sending to Ollama: ${err instanceof Error ? err.message : 'Unknown error'}`);
} finally {
setIsSendingToOllama(false);
}
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
const { value, selectionStart, selectionEnd } = e.currentTarget;
if (e.key === 'Tab') {
e.preventDefault();
const tab = ' '; // 2 spaces for a tab
const newValue = value.substring(0, selectionStart) + tab + value.substring(selectionEnd);
setCode(newValue);
cursorRef.current = selectionStart + tab.length;
}
};
const handleCopyCode = () => {
if (!code) return;
navigator.clipboard.writeText(code).then(() => {
setCopyButtonText('Copied!');
setTimeout(() => setCopyButtonText('Copy Code'), 2000);
}).catch(err => {
console.error('Failed to copy code: ', err);
setCopyButtonText('Copy Failed');
setTimeout(() => setCopyButtonText('Copy Code'), 2000);
});
};
return (
<div className="min-h-screen bg-gray-900 text-gray-100 flex flex-col font-sans">
<Header />
<main className="flex-1 flex flex-col p-4 pt-24 container mx-auto">
<div className="flex-1 grid grid-cols-1 md:grid-cols-2 gap-4 min-h-[60vh]">
{/* Code Editor Panel */}
<div className="flex flex-col h-full">
<div className="flex justify-between items-center p-2 bg-gray-800 border-b border-gray-700 rounded-t-lg">
<span className="font-mono text-sm text-gray-400">Your Code</span>
<div className="flex items-center gap-4">
<button
onClick={handleCopyCode}
disabled={!code.trim()}
className={`bg-gray-700 hover:bg-gray-600 disabled:bg-gray-800 disabled:text-gray-500 disabled:cursor-not-allowed text-gray-300 text-xs font-mono py-1 px-3 rounded transition-all duration-200 ${copyButtonText === 'Copied!' ? '!bg-green-600 text-white' : ''}`}
aria-label="Copy code to clipboard"
>
{copyButtonText}
</button>
<select
value={language}
onChange={handleLanguageChange}
className="bg-gray-700 border border-gray-600 rounded px-2 py-1 text-sm text-white focus:outline-none focus:ring-2 focus:ring-cyan-500"
aria-label="Select programming language"
>
{SUPPORTED_LANGUAGES.map((lang) => (
<option key={lang.value} value={lang.value}>
{lang.label}
</option>
))}
</select>
</div>
</div>
<div className="relative flex-1 bg-gray-900 ring-1 ring-gray-700 rounded-b-lg">
<textarea
ref={textAreaRef}
value={code}
onChange={handleCodeChange}
onKeyDown={handleKeyDown}
className="absolute inset-0 w-full h-full p-4 font-mono text-base bg-transparent text-transparent caret-white resize-none z-10 focus:outline-none"
spellCheck="false"
aria-label="Code Input"
/>
<pre
ref={preRef}
className="absolute inset-0 w-full h-full p-4 font-mono text-base pointer-events-none overflow-auto"
aria-hidden="true"
>
<code dangerouslySetInnerHTML={{ __html: highlightedCode }} />
</pre>
</div>
</div>
{/* Feedback Panel */}
<div className="flex flex-col h-full">
<div className="flex justify-between items-center p-2 bg-gray-800 border-b border-gray-700 rounded-t-lg">
<span className="font-mono text-sm text-gray-400">Gemini's Feedback</span>
</div>
<div className="relative flex-1 bg-gray-900 ring-1 ring-gray-700 rounded-b-lg">
{isLoading && <Loader />}
<FeedbackDisplay feedback={feedback} error={error} />
</div>
</div>
</div>
{/* Action Bar */}
<div className="py-4 flex flex-col sm:flex-row items-center justify-center gap-4">
<button
onClick={handleReview}
disabled={isLoading || !code.trim()}
className="w-full sm:w-auto bg-cyan-600 hover:bg-cyan-500 disabled:bg-gray-600 disabled:cursor-not-allowed text-white font-bold py-3 px-8 rounded-lg shadow-lg transform hover:scale-105 transition-all duration-300 ease-in-out"
>
{isLoading ? 'Reviewing...' : 'Review Code'}
</button>
</div>
{/* Ollama Panel */}
<div className="bg-gray-800/60 p-4 rounded-lg ring-1 ring-gray-700">
<h3 className="text-lg font-semibold mb-3 text-white">Forward to Ollama</h3>
<div className="flex flex-col sm:flex-row items-center gap-4 mb-2">
<input
type="text"
value={ollamaEndpoint}
onChange={(e) => setOllamaEndpoint(e.target.value)}
className="flex-grow bg-gray-700 border border-gray-600 rounded px-3 py-2 text-sm text-white focus:outline-none focus:ring-2 focus:ring-cyan-500 w-full sm:w-auto"
placeholder="Ollama API Endpoint"
aria-label="Ollama API Endpoint"
/>
<button
onClick={handleSendToOllama}
disabled={isSendingToOllama || !feedback}
className="w-full sm:w-auto bg-purple-600 hover:bg-purple-500 disabled:bg-gray-600 disabled:cursor-not-allowed text-white font-bold py-2 px-6 rounded transition-colors duration-200"
>
{isSendingToOllama ? 'Sending...' : 'Send to Ollama'}
</button>
</div>
{ollamaStatus && (
<div className="mt-3 p-3 bg-gray-900 rounded max-h-40 overflow-y-auto">
<pre className="whitespace-pre-wrap font-mono text-xs text-gray-300">{ollamaStatus}</pre>
</div>
)}
</div>
</main>
</div>
);
};
export default App;