-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
413 lines (362 loc) · 14.8 KB
/
script.js
File metadata and controls
413 lines (362 loc) · 14.8 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// API endpoints
const API_URL = ''; // Use relative paths for API calls
const ENDPOINTS = {
PROCESS_REQUIREMENT: `${API_URL}/api/process-requirement`,
GENERATE_CODE: `${API_URL}/api/generate-code`,
PUSH_GITHUB: `${API_URL}/api/push-to-github`,
UPDATE_GITHUB_TOKEN: `${API_URL}/api/update-github-token`
};
// DOM Elements
const initialLoading = document.querySelector('.initial-loading');
const warpStars = document.getElementById('warpStars');
const requirementInput = document.querySelector('.requirement-input');
const submitBtn = document.querySelector('.submit-btn');
const outputSection = document.querySelector('.output-section');
const developmentGoals = document.querySelector('.development-goals');
const loadingIndicator = document.getElementById('loadingIndicator');
const downloadMdBtn = document.querySelector('.download-md');
const generateCodeBtn = document.querySelector('.generate-code');
const pushGithubBtn = document.querySelector('.push-github');
const historyList = document.querySelector('.history-list');
const sidebarToggle = document.getElementById('sidebarToggle');
const sidebar = document.querySelector('.sidebar');
const clearHistory = document.getElementById('clearHistory');
const settingsBtn = document.querySelector('.settings-btn');
const aboutBtn = document.querySelector('.about-btn');
const settingsModal = document.getElementById('settingsModal');
const aboutModal = document.getElementById('aboutModal');
const closeModalBtns = document.querySelectorAll('.close-modal');
const githubBtn = document.querySelector('.github-btn');
const saveSettingsBtn = document.getElementById('saveSettings');
const githubTokenInput = document.getElementById('githubTokenInput');
// State management
let currentTasks = [];
let currentLanguage = 'generic'; // Add variable to store detected language
let sidebarCollapsed = false;
let githubToken = localStorage.getItem('githubToken') || '';
// Initialize GitHub token from storage
if (githubToken) {
githubTokenInput.value = githubToken;
}
// Initial loading animation
function createWarpStars() {
for (let i = 0; i < 100; i++) {
const star = document.createElement('div');
star.classList.add('warp-star');
star.style.left = `${Math.random() * 100}%`;
star.style.top = `${Math.random() * 100}%`;
star.style.animationDuration = `${1 + Math.random() * 2}s`;
star.style.animationDelay = `${Math.random() * 2}s`;
warpStars.appendChild(star);
}
}
function hideInitialLoading() {
initialLoading.style.opacity = '0';
setTimeout(() => {
initialLoading.style.display = 'none';
}, 500);
}
// Initialize loading animation
createWarpStars();
setTimeout(hideInitialLoading, 3000);
// Sidebar toggle
sidebarToggle.addEventListener('click', () => {
sidebarCollapsed = !sidebarCollapsed;
sidebar.classList.toggle('collapsed', sidebarCollapsed);
sidebarToggle.innerHTML = sidebarCollapsed ?
'<i class="fas fa-chevron-right"></i>' :
'<i class="fas fa-chevron-left"></i>';
});
// Modal functionality
function openModal(modal) {
modal.style.display = 'flex';
}
function closeModal(modal) {
modal.style.display = 'none';
}
settingsBtn.addEventListener('click', () => openModal(settingsModal));
aboutBtn.addEventListener('click', () => openModal(aboutModal));
closeModalBtns.forEach(btn => {
btn.addEventListener('click', () => {
const modal = btn.closest('.modal');
closeModal(modal);
});
});
window.addEventListener('click', (e) => {
if (e.target.classList.contains('modal')) {
closeModal(e.target);
}
});
// Settings handling
saveSettingsBtn.addEventListener('click', async () => {
const newToken = githubTokenInput.value.trim();
try {
const response = await fetch(ENDPOINTS.UPDATE_GITHUB_TOKEN, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ token: newToken })
});
const data = await response.json();
if (data.success) {
localStorage.setItem('githubToken', newToken);
alert('Settings saved successfully!');
closeModal(settingsModal);
} else {
throw new Error(data.error || 'Failed to save settings');
}
} catch (error) {
alert('Error saving settings: ' + error.message);
}
});
// Clear history
clearHistory.addEventListener('click', () => {
historyList.innerHTML = '';
});
// Removed redundant submitBtn listener that called Gemini directly from frontend.
// The listener below correctly calls the backend API.
// GitHub button handling
githubBtn.addEventListener('click', async () => {
const githubToken = localStorage.getItem('githubToken');
if (!githubToken) {
alert('Please set your GitHub Access Token in Settings first');
openModal(settingsModal);
return;
}
try {
// Verify token is still valid
const response = await fetch(ENDPOINTS.UPDATE_GITHUB_TOKEN, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ token: githubToken })
});
const data = await response.json();
if (data.success) {
// Token is valid, redirect to GitHub repositories
window.open('https://github.com?tab=repositories', '_blank');
} else {
// Token is invalid
alert('Your GitHub token appears to be invalid. Please update it in Settings.');
openModal(settingsModal);
}
} catch (error) {
alert('Error connecting to GitHub. Please check your token in Settings.');
openModal(settingsModal);
}
});
// Event Listeners
submitBtn.addEventListener('click', async () => {
const requirement = requirementInput.value.trim();
if (!requirement) return;
try {
submitBtn.disabled = true;
submitBtn.textContent = 'Processing...';
// Show output section with loading indicator
outputSection.classList.remove('hidden');
loadingIndicator.style.display = 'flex';
developmentGoals.innerHTML = '';
const response = await fetch(ENDPOINTS.PROCESS_REQUIREMENT, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ requirement })
});
const data = await response.json();
if (!data.success) throw new Error(data.error || 'Failed to process requirement');
if (!data.tasks || !Array.isArray(data.tasks)) {
throw new Error('Invalid response format: tasks array is missing');
}
currentTasks = data.tasks;
currentLanguage = data.techStack?.language || 'generic'; // Store the language
// Hide loading indicator and display analysis
loadingIndicator.style.display = 'none';
displayAnalysis(data);
saveToHistory(requirement, currentTasks);
} catch (error) {
console.error('Error processing requirement:', error);
alert('Error processing requirement: ' + error.message);
loadingIndicator.style.display = 'none';
} finally {
submitBtn.disabled = false;
submitBtn.textContent = 'Generate Plan';
}
});
downloadMdBtn.addEventListener('click', () => {
const content = currentTasks.join('\n');
const blob = new Blob([content], { type: 'text/markdown' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'development-goals.md';
a.click();
URL.revokeObjectURL(url);
});
generateCodeBtn.addEventListener('click', async () => {
if (!currentTasks.length) return;
try {
generateCodeBtn.disabled = true;
generateCodeBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Generating...'; // Use innerHTML for icon
const response = await fetch(ENDPOINTS.GENERATE_CODE, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
// Send tasks and the stored language as project_type
body: JSON.stringify({ tasks: currentTasks, project_type: currentLanguage })
});
if (response.ok && response.headers.get('Content-Type')?.includes('application/zip')) {
// Success: Handle the ZIP file download
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'generated_project.zip'; // Fixed filename
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
a.remove();
pushGithubBtn.classList.remove('hidden'); // Show GitHub button on success
} else {
// Error: Try to parse JSON error message from backend
let errorMsg = 'Failed to generate project files. Unknown error.';
try {
const errorData = await response.json();
if (errorData && errorData.error) {
errorMsg = errorData.error;
} else {
errorMsg = `Server responded with status ${response.status}: ${response.statusText}`;
}
} catch (jsonError) {
errorMsg = `Server responded with status ${response.status}. Failed to parse error message.`;
console.error("Failed to parse error JSON:", jsonError);
console.error("Response status:", response.status);
console.error("Response status text:", response.statusText);
}
throw new Error(errorMsg); // Throw the extracted or generated error message
}
} catch (error) {
// Use the specific alert message format
alert('Error generating code: ' + error.message);
console.error('Error in generateCodeBtn:', error);
} finally {
generateCodeBtn.disabled = false;
generateCodeBtn.innerHTML = '<i class="fas fa-code"></i> Generate Codebase'; // Restore button
}
});
pushGithubBtn.addEventListener('click', async () => {
const repoName = prompt('Enter repository name:');
if (!repoName) return;
try {
pushGithubBtn.disabled = true;
pushGithubBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Pushing...';
const response = await fetch(ENDPOINTS.PUSH_GITHUB, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
repoName,
tasks: currentTasks
})
});
const data = await response.json();
if (!data.success) throw new Error(data.error);
// Create a custom success modal instead of alert
const successModal = document.createElement('div');
successModal.className = 'modal';
successModal.style.display = 'flex';
successModal.innerHTML = `
<div class="modal-content">
<button class="close-modal">×</button>
<h2><i class="fas fa-check-circle" style="color: #4CAF50;"></i> Success!</h2>
<p>Your code has been successfully pushed to GitHub!</p>
<p>Repository URL: <a href="${data.repoUrl}" target="_blank" style="color: #4facfe;">${data.repoUrl}</a></p>
</div>
`;
document.body.appendChild(successModal);
// Add event listeners to close the modal
const closeBtn = successModal.querySelector('.close-modal');
closeBtn.addEventListener('click', () => {
document.body.removeChild(successModal);
});
successModal.addEventListener('click', (e) => {
if (e.target === successModal) {
document.body.removeChild(successModal);
}
});
} catch (error) {
alert('Error pushing to GitHub: ' + error.message);
} finally {
pushGithubBtn.disabled = false;
pushGithubBtn.innerHTML = '<i class="fab fa-github"></i> Push to GitHub';
}
});
// Helper Functions
function displayTasks(tasks) {
developmentGoals.innerHTML = '';
// Add tasks with staggered animation
tasks.forEach((task, index) => {
const taskElement = document.createElement('div');
taskElement.className = 'task';
taskElement.textContent = `${index + 1}. ${task}`;
taskElement.style.animationDelay = `${0.1 * index}s`;
developmentGoals.appendChild(taskElement);
});
outputSection.classList.remove('hidden');
}
function saveToHistory(requirement, tasks) {
const historyItem = document.createElement('div');
historyItem.className = 'history-item';
historyItem.textContent = requirement.substring(0, 50) + '...';
historyItem.addEventListener('click', () => {
requirementInput.value = requirement;
currentTasks = tasks;
displayTasks(tasks);
});
historyList.insertBefore(historyItem, historyList.firstChild);
}
// Removed downloadFiles function as it's handled directly in the event listener now.
function displayAnalysis(data) {
developmentGoals.innerHTML = '';
// Display tech stack
if (data.techStack) {
const techStackSection = document.createElement('div');
techStackSection.className = 'analysis-section';
techStackSection.innerHTML = `
<h3>Tech Stack</h3>
<div class="tech-info">
<p><strong>Language:</strong> ${data.techStack.language}</p>
<p><strong>Frameworks:</strong> ${data.techStack.frameworks.join(', ') || 'None'}</p>
</div>
`;
developmentGoals.appendChild(techStackSection);
}
// Display project structure
if (data.projectStructure) {
const structureSection = document.createElement('div');
structureSection.className = 'analysis-section';
structureSection.innerHTML = `
<h3>Project Structure</h3>
<pre class="project-tree">${data.projectStructure}</pre>
`;
developmentGoals.appendChild(structureSection);
}
// Display tasks with staggered animation
const tasksSection = document.createElement('div');
tasksSection.className = 'analysis-section';
tasksSection.innerHTML = '<h3>Development Tasks</h3>';
data.tasks.forEach((task, index) => {
const taskElement = document.createElement('div');
taskElement.className = 'task';
taskElement.textContent = task;
taskElement.style.animationDelay = `${0.1 * index}s`;
tasksSection.appendChild(taskElement);
});
developmentGoals.appendChild(tasksSection);
outputSection.classList.remove('hidden');
}