/** * Env Config View * Manage and edit .env file content with local SQLite version history & restore capabilities. */ const EnvConfigView = { currentContent: '', history: [], selectedPreview: null, async render() { const container = document.getElementById('view-container'); container.innerHTML = `

Caricamento configurazione .env...

`; try { const data = await App.api('/api/config/env'); this.currentContent = data.content || ''; this.history = data.history || []; this.renderContent(container); this.bindEvents(); } catch (err) { console.error('Error loading env config:', err); container.innerHTML = `
โš ๏ธ
Errore caricamento configurazione .env
${App.escapeHtml(err.message)}
`; } }, renderContent(container) { container.innerHTML = `

๐Ÿ“ Editor File .env

Modifica Diretta
Le modifiche avranno effetto sui moduli del server al riavvio.

๐Ÿ“œ Storico Modifiche

${this.history.length} Versioni
${this.renderHistoryList()}
`; }, renderHistoryList() { if (!this.history || this.history.length === 0) { return `
Nessuna versione registrata nello storico. Salva una modifica per iniziare l'archiviazione.
`; } return this.history.map((h, idx) => { const isCurrentVersion = idx === 0; const formattedDate = App.formatDateTime ? App.formatDateTime(h.created_at) : h.created_at; return `
#${h.id} ${isCurrentVersion ? `Attuale` : ''}
${formattedDate}
${App.escapeHtml(h.agent_name || 'Agente')}: ${App.escapeHtml(h.note || 'Modifica senza nota')}
${!isCurrentVersion ? ` ` : ''}
`; }).join(''); }, bindEvents() { const saveBtn = document.getElementById('env-save-btn'); const reloadBtn = document.getElementById('env-reload-btn'); const editorTextarea = document.getElementById('env-editor-content'); const noteInput = document.getElementById('env-note-input'); if (saveBtn) { saveBtn.addEventListener('click', async () => { const content = editorTextarea.value; const note = noteInput.value.trim(); try { saveBtn.disabled = true; saveBtn.textContent = 'Salvataggio...'; const res = await App.api('/api/config/env', { method: 'POST', body: JSON.stringify({ content, note }) }); Toast.success(res.message || 'File .env aggiornato con successo!'); if (noteInput) noteInput.value = ''; this.currentContent = content; this.history = res.history || []; try { await App.ensureLookups(true); App.updateDailyTimer(); } catch (_) {} // Re-render history panel const historyListDiv = document.getElementById('env-history-list'); if (historyListDiv) { historyListDiv.innerHTML = this.renderHistoryList(); this.bindHistoryEvents(); } } catch (err) { Toast.error('Errore salvataggio file .env: ' + err.message); } finally { saveBtn.disabled = false; saveBtn.innerHTML = '๐Ÿ’พ Salva Modifiche'; } }); } if (reloadBtn) { reloadBtn.addEventListener('click', () => { this.render(); }); } this.bindHistoryEvents(); }, bindHistoryEvents() { // Preview buttons document.querySelectorAll('.env-history-preview-btn').forEach(btn => { btn.addEventListener('click', () => { const id = parseInt(btn.dataset.id, 10); const record = this.history.find(h => h.id === id); if (record) { this.showPreviewModal(record); } }); }); // Restore buttons document.querySelectorAll('.env-history-restore-btn').forEach(btn => { btn.addEventListener('click', async () => { const id = parseInt(btn.dataset.id, 10); const record = this.history.find(h => h.id === id); if (!record) return; const confirmed = await App.confirm( `Ripristino File .env (Versione #${record.id})`, `Sei sicuro di voler ripristinare il file .env alla versione #${record.id} salvata il ${record.created_at}?\nL'operazione sovrascriverร  il file .env attuale e salverร  una nuova voce di ripristino nello storico.` ); if (!confirmed) return; try { btn.disabled = true; const res = await App.api(`/api/config/env/restore/${id}`, { method: 'POST' }); Toast.success(res.message || `Versione #${id} ripristinata con successo!`); this.currentContent = res.content || record.content; this.history = res.history || []; try { await App.ensureLookups(true); App.updateDailyTimer(); } catch (_) {} const editorTextarea = document.getElementById('env-editor-content'); if (editorTextarea) { editorTextarea.value = this.currentContent; } const historyListDiv = document.getElementById('env-history-list'); if (historyListDiv) { historyListDiv.innerHTML = this.renderHistoryList(); this.bindHistoryEvents(); } } catch (err) { Toast.error('Errore durante il ripristino: ' + err.message); } finally { btn.disabled = false; } }); }); // Delete buttons document.querySelectorAll('.env-history-delete-btn').forEach(btn => { btn.addEventListener('click', async () => { const id = parseInt(btn.dataset.id, 10); const record = this.history.find(h => h.id === id); if (!record) return; const confirmed = await App.confirm( `Eliminazione Versione #${record.id}`, `Sei sicuro di voler eliminare permanentemente la versione #${record.id} dallo storico?` ); if (!confirmed) return; try { btn.disabled = true; let res; try { res = await App.api(`/api/config/env/history/${id}`, { method: 'DELETE' }); } catch (delErr) { // Fallback to POST endpoint if DELETE route returned 404 res = await App.api(`/api/config/env/history/${id}/delete`, { method: 'POST' }); } Toast.success(res.message || `Versione #${id} eliminata dallo storico!`); this.history = res.history || []; const historyListDiv = document.getElementById('env-history-list'); if (historyListDiv) { historyListDiv.innerHTML = this.renderHistoryList(); this.bindHistoryEvents(); } } catch (err) { Toast.error('Errore durante l\'eliminazione dello storico: ' + err.message); btn.disabled = false; } }); }); }, showPreviewModal(record) { const modalContainer = document.getElementById('env-preview-modal-container'); if (!modalContainer) return; modalContainer.innerHTML = ` `; const closeModal = () => { modalContainer.innerHTML = ''; }; document.getElementById('preview-modal-close')?.addEventListener('click', closeModal); document.getElementById('preview-modal-cancel')?.addEventListener('click', closeModal); document.getElementById('preview-copy-btn')?.addEventListener('click', () => { navigator.clipboard.writeText(record.content).then(() => { Toast.success('Contenuto copiato negli appunti!'); }).catch(err => Toast.error('Errore durante la copia: ' + err.message)); }); document.getElementById('preview-modal-restore-btn')?.addEventListener('click', async () => { closeModal(); const restoreBtn = document.querySelector(`.env-history-restore-btn[data-id="${record.id}"]`); if (restoreBtn) { restoreBtn.click(); } }); } };