-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
119 lines (107 loc) · 3.95 KB
/
Copy pathApp.tsx
File metadata and controls
119 lines (107 loc) · 3.95 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
import React, { useState, useCallback } from 'react';
import { reviewDjangoCode } from './services/geminiService';
import CodeInput from './components/CodeInput';
import ReviewOutput from './components/ReviewOutput';
import DjangoIcon from './components/icons/DjangoIcon';
const App: React.FC = () => {
const [filesContent, setFilesContent] = useState<{ [key: string]: string }>({});
const [review, setReview] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
const handleFilesSelected = async (fileList: FileList | null) => {
if (!fileList) return;
// Set a temporary loading state for file reading
setIsLoading(true);
setError(null);
setReview(null);
setFilesContent({});
const pythonFiles = Array.from(fileList).filter(file => file.name.endsWith('.py'));
if (pythonFiles.length === 0) {
setError("No Python (.py) files found in the selected directory.");
setIsLoading(false);
return;
}
const fileContents: { [key: string]: string } = {};
try {
await Promise.all(
pythonFiles.map(file => {
return new Promise<void>((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => {
const filePath = (file as any).webkitRelativePath || file.name;
fileContents[filePath] = e.target?.result as string;
resolve();
};
reader.onerror = () => {
reject(new Error(`Failed to read file: ${file.name}`));
};
reader.readAsText(file);
});
})
);
setFilesContent(fileContents);
} catch (err) {
setError(err instanceof Error ? err.message : 'An unknown error occurred while reading files.');
} finally {
// End the file-reading loading state
setIsLoading(false);
}
};
const handleReviewCode = useCallback(async () => {
if (Object.keys(filesContent).length === 0) {
setError('Please upload a project folder to review.');
return;
}
setIsLoading(true);
setError(null);
setReview(null);
try {
const result = await reviewDjangoCode(filesContent);
setReview(result);
} catch (err) {
setError(err instanceof Error ? err.message : 'An unknown error occurred.');
} finally {
setIsLoading(false);
}
}, [filesContent]);
const handleClear = useCallback(() => {
setFilesContent({});
setReview(null);
setError(null);
setIsLoading(false);
}, []);
return (
<div className="min-h-screen bg-slate-900 text-slate-200 font-sans">
<header className="bg-slate-800/50 backdrop-blur-sm border-b border-slate-700 p-4 sticky top-0 z-10">
<div className="container mx-auto flex items-center gap-4">
<DjangoIcon className="h-10 w-10 text-green-400" />
<div>
<h1 className="text-2xl font-bold text-white">Django Code Reviewer AI</h1>
<p className="text-sm text-slate-400">Powered by Google Gemini</p>
</div>
</div>
</header>
<main className="container mx-auto p-4 md:p-6 lg:p-8">
<div className="flex flex-col lg:flex-row gap-6 h-full">
<div className="lg:w-1/2 flex flex-col">
<CodeInput
onFilesSelected={handleFilesSelected}
fileCount={Object.keys(filesContent).length}
onReview={handleReviewCode}
onClear={handleClear}
isLoading={isLoading}
/>
</div>
<div className="lg:w-1/2 flex flex-col">
<ReviewOutput
review={review}
isLoading={isLoading}
error={error}
/>
</div>
</div>
</main>
</div>
);
};
export default App;