Skip to content

Commit 3d897cf

Browse files
committed
feat: enhance update notification handling and improve logging for button actions
1 parent dd5745a commit 3d897cf

File tree

4 files changed

+143
-34
lines changed

4 files changed

+143
-34
lines changed

src/components/update-notification.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,23 @@ export class UpdateNotification {
357357
*/
358358
bindButtonEvents() {
359359
const buttons = this.container.querySelectorAll('[data-action]');
360+
console.log('🔔 [UpdateNotification] Trovati', buttons.length, 'pulsanti con data-action');
360361
buttons.forEach(button => {
362+
console.log('🔔 [UpdateNotification] Binding evento per pulsante:', button.dataset.action);
361363
button.addEventListener('click', (e) => {
362-
const action = e.target.dataset.action;
363-
this.handleAction(action);
364+
// Trova il pulsante con data-action, anche se si clicca su un elemento figlio (come l'icona SVG)
365+
let target = e.target;
366+
while (target && !target.dataset.action) {
367+
target = target.parentElement;
368+
}
369+
370+
const action = target ? target.dataset.action : null;
371+
console.log('🔔 [UpdateNotification] Target trovato:', target, 'Action:', action);
372+
if (action) {
373+
this.handleAction(action);
374+
} else {
375+
console.warn('🔔 [UpdateNotification] Nessuna azione trovata per questo click');
376+
}
364377
});
365378
});
366379
}
@@ -369,6 +382,7 @@ export class UpdateNotification {
369382
* Handles button actions
370383
*/
371384
handleAction(action) {
385+
console.log('🔔 [UpdateNotification] Azione pulsante:', action);
372386
switch (action) {
373387
case 'download':
374388
this.startDownload();
@@ -538,14 +552,15 @@ export class UpdateNotification {
538552
}
539553

540554
// Verifica se siamo in modalità sviluppo senza test mode
541-
const updateManager = getUpdateManager();
542-
if (updateManager && updateManager.isDevelopmentMode && updateManager.isDevelopmentMode()) {
543-
const hasTestMode = localStorage.getItem('presto_force_update_test') === 'true';
544-
if (!hasTestMode) {
545-
console.log('🔍 [UpdateNotification] Modalità sviluppo senza test mode - non mostro notifica');
546-
return;
547-
}
548-
}
555+
// RIMOSSO: Ora permettiamo la notifica anche in modalità sviluppo per GitHub releases
556+
// const updateManager = getUpdateManager();
557+
// if (updateManager && updateManager.isDevelopmentMode && updateManager.isDevelopmentMode()) {
558+
// const hasTestMode = localStorage.getItem('presto_force_update_test') === 'true';
559+
// if (!hasTestMode) {
560+
// console.log('🔍 [UpdateNotification] Modalità sviluppo senza test mode - non mostro notifica');
561+
// return;
562+
// }
563+
// }
549564

550565
// Don't show if this version has been skipped
551566
if (this.isVersionSkipped(updateInfo.version)) {
@@ -659,5 +674,5 @@ export class UpdateNotification {
659674
}
660675
}
661676

662-
// Export singleton instance
663-
export const updateNotification = new UpdateNotification();
677+
// Export the class, not an instance - let main.js handle initialization
678+
// export const updateNotification = new UpdateNotification();

src/main.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { TeamManager } from './managers/team-manager.js';
66
// Auth manager will be imported after Supabase is loaded
77
import { PomodoroTimer } from './core/pomodoro-timer.js';
88
import { NotificationUtils } from './utils/common-utils.js';
9-
// Removed unused import: updateNotification
9+
import { UpdateNotification } from './components/update-notification.js';
1010

1111
// Global application state
1212
let timer = null;
@@ -1556,6 +1556,11 @@ async function initializeApplication() {
15561556
window.updateManager.loadPreferences(); // Carica le preferenze salvate se supportato
15571557
}
15581558

1559+
// Initialize Update Notification component
1560+
console.log('🔔 Initializing Update Notification...');
1561+
const updateNotification = new UpdateNotification();
1562+
window.updateNotification = updateNotification; // Make it globally available
1563+
15591564
// Skip first run authentication - proceed directly with guest mode
15601565
if (authManager.isFirstRun()) {
15611566
console.log('👋 First run detected, proceeding as guest...');
@@ -1891,9 +1896,10 @@ function setupUpdateManagement() {
18911896
}
18921897
});
18931898

1894-
updateManager.on('checkError', () => {
1899+
updateManager.on('checkError', (event) => {
18951900
if (updateStatus) {
1896-
updateStatus.innerHTML = '<span class="status-text error">Check failed</span>';
1901+
const errorMessage = event?.detail?.message || 'Check failed';
1902+
updateStatus.innerHTML = `<span class="status-text error">${errorMessage}</span>`;
18971903
}
18981904
});
18991905

src/managers/update-manager-global.js

Lines changed: 106 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ window.UpdateManagerV2 = class UpdateManagerV2 {
1919
// Eventi personalizzati
2020
this.eventTarget = new EventTarget();
2121

22-
// Inizializza il controllo automatico solo se non siamo in dev mode
23-
if (!this.isDevelopmentMode()) {
24-
this.startAutoCheck();
25-
}
22+
// Inizializza il controllo automatico sempre (ora funziona anche in dev mode)
23+
this.startAutoCheck();
2624

2725
console.log('✅ UpdateManager v2 inizializzato (global)');
2826
}
@@ -106,8 +104,8 @@ window.UpdateManagerV2 = class UpdateManagerV2 {
106104

107105
throw new Error('API versione non disponibile');
108106
} catch (error) {
109-
console.warn('❌ Errore recupero versione:', error);
110-
return '0.2.2'; // fallback
107+
console.error('❌ Impossibile ottenere la versione dell\'app:', error);
108+
throw new Error('Impossibile determinare la versione corrente dell\'applicazione');
111109
}
112110
}
113111

@@ -245,16 +243,18 @@ window.UpdateManagerV2 = class UpdateManagerV2 {
245243
* Avvia il controllo automatico degli aggiornamenti
246244
*/
247245
startAutoCheck() {
248-
if (this.autoCheck && !this.checkInterval && !this.isDevelopmentMode()) {
246+
if (this.autoCheck && !this.checkInterval) {
249247
// Controlla ogni ora
250248
this.checkInterval = setInterval(() => {
249+
console.log('🔄 Controllo automatico periodico degli aggiornamenti...');
251250
this.checkForUpdates(false); // silent check
252251
}, 60 * 60 * 1000);
253252

254-
// Controllo iniziale dopo 30 secondi
253+
// Controllo iniziale dopo 5 secondi
255254
setTimeout(() => {
256-
this.checkForUpdates(false);
257-
}, 30000);
255+
console.log('🔄 Controllo automatico iniziale degli aggiornamenti...');
256+
this.checkForUpdates(false); // silent - mostra il banner se c'è un aggiornamento
257+
}, 5000);
258258

259259
console.log('🔄 Controllo automatico aggiornamenti avviato');
260260
}
@@ -292,6 +292,83 @@ window.UpdateManagerV2 = class UpdateManagerV2 {
292292
return 0;
293293
}
294294

295+
/**
296+
* Controlla solo la versione da GitHub senza tentare l'installazione (per modalità sviluppo)
297+
*/
298+
async checkVersionFromGitHub(showDialog = true) {
299+
try {
300+
// Ottieni versione corrente
301+
let currentVersion;
302+
try {
303+
currentVersion = await this.getAppVersion();
304+
console.log(`📋 Versione corrente: ${currentVersion}`);
305+
} catch (error) {
306+
console.error('❌ Errore nel recupero della versione corrente:', error);
307+
this.emit('checkError', { error: 'Impossibile determinare la versione corrente' });
308+
if (showDialog) {
309+
alert('Impossibile verificare gli aggiornamenti: versione corrente non determinabile');
310+
}
311+
return false;
312+
}
313+
314+
// Controlla ultima release su GitHub
315+
const response = await fetch('https://api.github.com/repos/murdercode/presto/releases/latest');
316+
if (!response.ok) {
317+
throw new Error(`HTTP ${response.status}`);
318+
}
319+
320+
const githubRelease = await response.json();
321+
const latestVersion = githubRelease.tag_name.replace(/^v/, '');
322+
323+
console.log(`📋 Ultima versione GitHub: ${latestVersion}`);
324+
325+
// Confronta versioni
326+
if (this.compareVersions(latestVersion, currentVersion) <= 0) {
327+
console.log('✅ Nessun aggiornamento disponibile');
328+
this.updateAvailable = false;
329+
this.currentUpdate = null;
330+
this.emit('updateNotAvailable');
331+
332+
if (showDialog) {
333+
alert(`Nessun aggiornamento disponibile.\n\nVersione corrente: ${currentVersion}\nUltima versione: ${latestVersion}`);
334+
}
335+
return false;
336+
}
337+
338+
// Aggiornamento disponibile
339+
console.log(`🎉 Aggiornamento disponibile: ${latestVersion}`);
340+
this.updateAvailable = true;
341+
this.currentUpdate = {
342+
version: latestVersion,
343+
body: githubRelease.body || '',
344+
date: githubRelease.published_at
345+
};
346+
347+
// console.log('📢 Emetto evento updateAvailable con:', this.currentUpdate); // Debug rimosso
348+
this.emit('updateAvailable', this.currentUpdate);
349+
350+
if (showDialog) {
351+
const message = `🎉 Aggiornamento disponibile!\n\n` +
352+
`Versione corrente: ${currentVersion}\n` +
353+
`Nuova versione: ${latestVersion}\n\n` +
354+
`Nota: In modalità sviluppo, scarica manualmente da GitHub.`;
355+
alert(message);
356+
}
357+
358+
return true;
359+
360+
} catch (error) {
361+
console.error('❌ Errore controllo versione GitHub:', error);
362+
this.emit('checkError', { error: `Errore di rete: ${error.message}` });
363+
if (showDialog) {
364+
alert(`Errore nel controllo degli aggiornamenti:\n${error.message}`);
365+
}
366+
return false;
367+
} finally {
368+
this.isChecking = false;
369+
}
370+
}
371+
295372
/**
296373
* Controlla se sono disponibili aggiornamenti usando approccio ibrido sicuro
297374
*/
@@ -312,12 +389,9 @@ window.UpdateManagerV2 = class UpdateManagerV2 {
312389
const hasTestMode = localStorage.getItem('presto_force_update_test') === 'true';
313390

314391
if (isDevMode && !hasTestMode) {
315-
console.warn('⚠️ Modalità sviluppo - controllo disabilitato');
316-
this.emit('updateNotAvailable');
317-
if (showDialog) {
318-
await this.showDevelopmentMessage();
319-
}
320-
return false;
392+
console.log('🔍 Modalità sviluppo - controllo tramite GitHub API senza installazione');
393+
// In modalità sviluppo facciamo solo il controllo della versione senza installazione
394+
return await this.checkVersionFromGitHub(showDialog);
321395
}
322396

323397
// Se è modalità test, simula un aggiornamento
@@ -329,8 +403,21 @@ window.UpdateManagerV2 = class UpdateManagerV2 {
329403

330404
// Controlla aggiornamenti reali
331405
// 1. Prima prova con l'API GitHub per avere info complete
332-
const currentVersion = await this.getAppVersion();
333-
console.log(`📋 Versione corrente: ${currentVersion}`);
406+
let currentVersion;
407+
try {
408+
currentVersion = await this.getAppVersion();
409+
console.log(`📋 Versione corrente: ${currentVersion}`);
410+
} catch (versionError) {
411+
console.error('❌ Impossibile ottenere la versione corrente:', versionError.message);
412+
this.updateAvailable = false;
413+
this.currentUpdate = null;
414+
if (!silent) {
415+
this.eventTarget.dispatchEvent(new CustomEvent('checkError', {
416+
detail: { message: 'Impossibile verificare la versione corrente dell\'applicazione' }
417+
}));
418+
}
419+
return false;
420+
}
334421

335422
// Controlla GitHub API
336423
const response = await fetch('https://api.github.com/repos/StefanoNovelli/presto/releases/latest');
@@ -623,6 +710,7 @@ window.UpdateManagerV2 = class UpdateManagerV2 {
623710
}
624711

625712
emit(event, data = null) {
713+
// console.log(`📢 [UpdateManager] Emetto evento: ${event}`, data); // Debug rimosso
626714
this.eventTarget.dispatchEvent(new CustomEvent(event, { detail: data }));
627715
}
628716

src/utils/theme-loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ThemeLoader {
3939
// that gets updated by the build process or manually maintained
4040

4141
// This could be enhanced to use a build-time script that generates this list
42-
const knownThemes = [
42+
const knownThemes = [
4343
'espresso.css',
4444
'pipboy.css',
4545
'pommodore64.css'

0 commit comments

Comments
 (0)