|
| 1 | +import { backend, IS_DESKTOP } from './environment.js'; |
| 2 | + |
| 3 | +const ids = { |
| 4 | + heroTitle: document.getElementById('heroTitle'), |
| 5 | + currentVersion: document.getElementById('currentVersion'), |
| 6 | + newVersion: document.getElementById('newVersion'), |
| 7 | + notesContent: document.getElementById('notesContent'), |
| 8 | + progressWrap: document.getElementById('progressWrap'), |
| 9 | + progressLabel: document.getElementById('progressLabel'), |
| 10 | + progressFill: document.getElementById('progressFill'), |
| 11 | + statusMsg: document.getElementById('statusMsg'), |
| 12 | + updateBtn: document.getElementById('updateBtn'), |
| 13 | + laterBtn: document.getElementById('laterBtn'), |
| 14 | +}; |
| 15 | + |
| 16 | +let isInstalling = false; |
| 17 | + |
| 18 | +function setStatus(msg) { |
| 19 | + ids.statusMsg.textContent = msg; |
| 20 | +} |
| 21 | + |
| 22 | +function showProgress(pct, label) { |
| 23 | + ids.progressWrap.classList.add('visible'); |
| 24 | + ids.progressFill.style.width = `${pct}%`; |
| 25 | + if (label) ids.progressLabel.textContent = label; |
| 26 | +} |
| 27 | + |
| 28 | +async function loadUpdateInfo() { |
| 29 | + if (!IS_DESKTOP) { |
| 30 | + ids.currentVersion.textContent = '1.5.1'; |
| 31 | + ids.newVersion.textContent = '2.0.0'; |
| 32 | + ids.notesContent.textContent = 'Auto-update support.\nDashboard improvements.'; |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + const info = await backend.invoke('check-for-update'); |
| 38 | + if (!info) { |
| 39 | + ids.heroTitle.textContent = 'Up to Date'; |
| 40 | + ids.notesContent.textContent = 'No update available.'; |
| 41 | + ids.updateBtn.disabled = true; |
| 42 | + setStatus('You are running the latest version.'); |
| 43 | + return; |
| 44 | + } |
| 45 | + ids.currentVersion.textContent = `v${info.currentVersion}`; |
| 46 | + ids.newVersion.textContent = `v${info.version}`; |
| 47 | + ids.notesContent.textContent = info.body || 'See GitHub releases for details.'; |
| 48 | + } catch (err) { |
| 49 | + ids.notesContent.textContent = 'Could not check for updates.'; |
| 50 | + ids.updateBtn.disabled = true; |
| 51 | + setStatus(`Error: ${err}`); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +async function startUpdate() { |
| 56 | + if (isInstalling) return; |
| 57 | + isInstalling = true; |
| 58 | + ids.updateBtn.disabled = true; |
| 59 | + ids.laterBtn.disabled = true; |
| 60 | + ids.heroTitle.textContent = 'Downloading Update…'; |
| 61 | + showProgress(0, 'DOWNLOADING...'); |
| 62 | + setStatus('Downloading update — do not close this window.'); |
| 63 | + |
| 64 | + if (!IS_DESKTOP) return; |
| 65 | + |
| 66 | + const unlistenProgress = await backend.on('update-progress', (_event, data) => { |
| 67 | + if (data.total) { |
| 68 | + const pct = Math.round((data.downloaded / data.total) * 100); |
| 69 | + showProgress(pct, `DOWNLOADING ${pct}%`); |
| 70 | + } else { |
| 71 | + showProgress(100, 'DOWNLOADING...'); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + try { |
| 76 | + await backend.invoke('install-update'); |
| 77 | + // If we reach here the installer is running — app may close at any moment. |
| 78 | + showProgress(100, 'INSTALLING...'); |
| 79 | + ids.heroTitle.textContent = 'Installing…'; |
| 80 | + setStatus('Installing update. The app will restart automatically.'); |
| 81 | + } catch (err) { |
| 82 | + isInstalling = false; |
| 83 | + ids.updateBtn.disabled = false; |
| 84 | + ids.laterBtn.disabled = false; |
| 85 | + ids.heroTitle.textContent = 'Update Failed'; |
| 86 | + showProgress(0); |
| 87 | + setStatus(`Install failed: ${err}`); |
| 88 | + } finally { |
| 89 | + unlistenProgress(); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +ids.updateBtn.addEventListener('click', startUpdate); |
| 94 | +ids.laterBtn.addEventListener('click', async () => { |
| 95 | + if (!IS_DESKTOP) return; |
| 96 | + await backend.invoke('close-window'); |
| 97 | +}); |
| 98 | + |
| 99 | +document.addEventListener('keydown', async (event) => { |
| 100 | + if (event.key === 'Escape' && IS_DESKTOP && !isInstalling) { |
| 101 | + await backend.invoke('close-window'); |
| 102 | + } |
| 103 | +}); |
| 104 | + |
| 105 | +loadUpdateInfo(); |
0 commit comments