diff --git a/src/features/images/popupImageManager.js b/src/features/images/popupImageManager.js index 45d64db..980e52d 100644 --- a/src/features/images/popupImageManager.js +++ b/src/features/images/popupImageManager.js @@ -71,8 +71,12 @@ class PopupImageManager { const { minInterval, maxInterval } = this.periodicSystem; const interval = Math.random() * (maxInterval - minInterval) + minInterval; - this.periodicSystem.interval = setTimeout(() => { - this.showPeriodicPopup(); + this.periodicSystem.interval = setTimeout(async () => { + try { + await this.showPeriodicPopup(); + } catch (error) { + console.error('Error showing periodic popup:', error); + } this.scheduleNextPeriodicPopup(); }, interval); @@ -82,12 +86,12 @@ class PopupImageManager { /** * Show a periodic random popup */ - showPeriodicPopup() { + async showPeriodicPopup() { if (!this.periodicSystem.isActive) { return; } - const image = this.getRandomPeriodicImage(); + const image = await this.getRandomPeriodicImage(); if (!image) { console.log('⚠️ No images available for periodic popup'); return; @@ -99,9 +103,9 @@ class PopupImageManager { /** * Get random image for periodic popups (from linked directories and individual files) */ - getRandomPeriodicImage() { + async getRandomPeriodicImage() { // Get images from the same sources as the main library - const allImages = this.getLinkedImages(); + const allImages = await this.getLinkedImages(); if (allImages.length === 0) { console.log('⚠️ No linked images available for popups'); @@ -150,7 +154,7 @@ class PopupImageManager { /** * Get all images from linked directories and individual files (same as main library) */ - getLinkedImages() { + async getLinkedImages() { const allImages = []; try { @@ -185,7 +189,24 @@ class PopupImageManager { for (const dir of linkedDirs) { try { if (window.electronAPI.readDirectory) { - const files = window.electronAPI.readDirectory(dir.path); + const result = window.electronAPI.readDirectory(dir.path); + + let files = []; + // Handle both sync and async results + if (result && typeof result.then === 'function') { + try { + files = await result; + } catch (asyncError) { + console.error(`Error loading images from directory ${dir.path}:`, asyncError); + continue; + } + } else if (Array.isArray(result)) { + files = result; + } else { + console.warn(`Unexpected result type from readDirectory for ${dir.path}:`, typeof result); + continue; + } + const imageFiles = files.filter(file => imageExtensions.test(file.name)); imageFiles.forEach(file => {