Update script.js

This commit is contained in:
ADARSH 2025-07-05 21:26:45 +05:30 committed by GitHub
parent 2b04d97cc7
commit 81b0b0bf90
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -142,13 +142,23 @@ class PromptArsenal {
this.showLoading(true); this.showLoading(true);
try { try {
// Get all files from the server console.log('Loading file structure from server...');
const response = await fetch('/api/files'); const response = await fetch('/api/files');
if (response.ok) { if (response.ok) {
this.allFiles = await response.json(); const data = await response.json();
this.allFiles = Array.isArray(data) ? data : [];
console.log(`Loaded ${this.allFiles.length} files from server`);
if (this.allFiles.length === 0) {
console.warn('No files returned from server');
this.showNotification('No markdown files found', 'warning');
}
} else { } else {
// Fallback to hardcoded structure if API fails const errorText = await response.text();
this.allFiles = this.buildHardcodedStructure(); console.error('Failed to load files from server:', response.status, errorText);
this.allFiles = [];
this.showNotification(`Server error: ${response.status}`, 'error');
} }
this.populateFolderFilter(); this.populateFolderFilter();
@ -156,9 +166,10 @@ class PromptArsenal {
this.filteredFiles = [...this.allFiles]; this.filteredFiles = [...this.allFiles];
} catch (error) { } catch (error) {
console.error('Failed to load file structure:', error); console.error('Network error loading file structure:', error);
this.allFiles = this.buildHardcodedStructure(); this.allFiles = [];
this.filteredFiles = [...this.allFiles]; this.filteredFiles = [];
this.showNotification('Network error - is the server running?', 'error');
} }
this.showLoading(false); this.showLoading(false);
@ -557,10 +568,9 @@ class PromptArsenal {
// Add click events to files // Add click events to files
treeStructure.querySelectorAll('.tree-file').forEach(fileEl => { treeStructure.querySelectorAll('.tree-file').forEach(fileEl => {
fileEl.addEventListener('click', (e) => { fileEl.addEventListener('click', async (e) => {
const filePath = e.currentTarget.dataset.file; const filePath = e.currentTarget.dataset.file;
this.openFile(filePath); await this.showFilePreview(filePath);
this.showFilePreview(filePath);
}); });
}); });
} }
@ -573,26 +583,32 @@ class PromptArsenal {
let content = this.fileContents.get(filePath); let content = this.fileContents.get(filePath);
if (!content) { if (!content) {
// Try to load from server first
try { try {
console.log('Loading file:', filePath); console.log('Loading file:', filePath);
const response = await fetch(`/api/file?path=${encodeURIComponent(filePath)}`); const response = await fetch(`/api/file?path=${encodeURIComponent(filePath)}`);
if (response.ok) { if (response.ok) {
content = await response.text(); content = await response.text();
console.log('File loaded successfully:', filePath); console.log('File loaded successfully:', filePath);
this.fileContents.set(filePath, content);
} else { } else {
const errorText = await response.text(); let errorText = 'Unknown error';
try {
const errorResponse = await response.json();
errorText = errorResponse.error || errorText;
} catch (e) {
errorText = await response.text();
}
console.error('Server error:', response.status, errorText); console.error('Server error:', response.status, errorText);
throw new Error(`Server error: ${response.status} - ${errorText}`); content = `Error loading file: ${response.status}\n${errorText}`;
this.showNotification(`Failed to load file: ${response.status}`, 'error');
} }
} catch (error) { } catch (error) {
console.error('Error fetching file:', error); console.error('Error fetching file:', error);
// Fallback to mock content content = `Network error loading file: ${error.message}`;
content = this.generateMockContent(filePath); this.showNotification('Network error loading file', 'error');
this.showNotification('Could not load file, showing sample content', 'warning');
} }
this.fileContents.set(filePath, content);
} }
this.showModal(filePath, content); this.showModal(filePath, content);
@ -737,7 +753,7 @@ Apply these techniques responsibly and ethically.`;
document.body.style.overflow = ''; document.body.style.overflow = '';
} }
showFilePreview(filePath) { async showFilePreview(filePath) {
const filePreview = document.getElementById('filePreview'); const filePreview = document.getElementById('filePreview');
if (!filePreview) return; if (!filePreview) return;
@ -745,8 +761,17 @@ Apply these techniques responsibly and ethically.`;
let content = this.fileContents.get(filePath); let content = this.fileContents.get(filePath);
if (!content) { if (!content) {
content = this.generateMockContent(filePath); try {
this.fileContents.set(filePath, content); const response = await fetch(`/api/file?path=${encodeURIComponent(filePath)}`);
if (response.ok) {
content = await response.text();
this.fileContents.set(filePath, content);
} else {
content = 'Error loading file preview';
}
} catch (error) {
content = 'Network error loading file preview';
}
} }
filePreview.innerHTML = ` filePreview.innerHTML = `
@ -838,8 +863,20 @@ Apply these techniques responsibly and ethically.`;
async refresh() { async refresh() {
this.showLoading(true); this.showLoading(true);
this.fileContents.clear(); this.fileContents.clear();
try {
// Call refresh endpoint first
const refreshResponse = await fetch('/api/refresh');
if (refreshResponse.ok) {
console.log('Server refreshed file index');
}
} catch (error) {
console.warn('Could not refresh server index:', error);
}
await this.loadFileStructure(); await this.loadFileStructure();
this.filterAndRenderFiles(); this.filterAndRenderFiles();
this.updateStats();
this.showNotification('Data refreshed!'); this.showNotification('Data refreshed!');
} }