// Launcher Update Manager UI let updateModal = null; let downloadProgressBar = null; function initUpdater() { // Listen for update events from main process if (window.electronAPI && window.electronAPI.onUpdateAvailable) { window.electronAPI.onUpdateAvailable((updateInfo) => { showUpdateModal(updateInfo); }); } if (window.electronAPI && window.electronAPI.onUpdateDownloadProgress) { window.electronAPI.onUpdateDownloadProgress((progress) => { updateDownloadProgress(progress); }); } if (window.electronAPI && window.electronAPI.onUpdateDownloaded) { window.electronAPI.onUpdateDownloaded((info) => { showInstallUpdatePrompt(info); }); } } function showUpdateModal(updateInfo) { if (updateModal) { updateModal.remove(); } updateModal = document.createElement('div'); updateModal.className = 'update-modal-overlay'; updateModal.innerHTML = `

Launcher Update Available

Version ${updateInfo.newVersion} is available!

Current version: ${updateInfo.currentVersion}

${updateInfo.releaseNotes ? `
${updateInfo.releaseNotes}
` : ''}
`; document.body.appendChild(updateModal); } async function downloadUpdate() { const downloadBtn = updateModal.querySelector('.btn-primary'); const laterBtn = updateModal.querySelector('.btn-secondary'); const progressDiv = updateModal.querySelector('.update-progress'); // Disable buttons and show progress downloadBtn.disabled = true; laterBtn.disabled = true; progressDiv.style.display = 'block'; try { await window.electronAPI.downloadUpdate(); } catch (error) { console.error('Failed to download update:', error); alert('Failed to download update. Please try again later.'); dismissUpdateModal(); } } function updateDownloadProgress(progress) { if (!updateModal) return; const progressBar = document.getElementById('updateProgressBar'); const progressText = document.getElementById('updateProgressText'); if (progressBar) { progressBar.style.width = `${progress.percent}%`; } if (progressText) { const mbTransferred = (progress.transferred / 1024 / 1024).toFixed(2); const mbTotal = (progress.total / 1024 / 1024).toFixed(2); const speed = (progress.bytesPerSecond / 1024 / 1024).toFixed(2); progressText.textContent = `Downloading... ${mbTransferred}MB / ${mbTotal}MB (${speed} MB/s)`; } } function showInstallUpdatePrompt(info) { if (updateModal) { updateModal.remove(); } updateModal = document.createElement('div'); updateModal.className = 'update-modal-overlay'; updateModal.innerHTML = `

Update Downloaded

Version ${info.version} has been downloaded and is ready to install.

The launcher will restart to complete the installation.

`; document.body.appendChild(updateModal); } async function installUpdate() { try { await window.electronAPI.installUpdate(); } catch (error) { console.error('Failed to install update:', error); } } function dismissUpdateModal() { if (updateModal) { updateModal.remove(); updateModal = null; } } // Initialize when DOM is ready document.addEventListener('DOMContentLoaded', initUpdater); // Export functions window.UpdaterUI = { showUpdateModal, dismissUpdateModal, downloadUpdate, installUpdate };