diff --git a/src/features/background/components/BackgroundImage.jsx b/src/features/background/components/BackgroundImage.jsx index 3ee5f8a3..7dc18286 100644 --- a/src/features/background/components/BackgroundImage.jsx +++ b/src/features/background/components/BackgroundImage.jsx @@ -1,23 +1,37 @@ -import { memo } from 'react'; +import { memo, useState, useEffect } from 'react'; import PhotoInformation from './PhotoInformation'; import variables from 'config/variables'; import { updateHash } from 'utils/deepLinking'; import EventBus from 'utils/eventbus'; +import { getAllBackgrounds } from 'utils/customBackgroundDB'; /** * BackgroundImage component for rendering image backgrounds */ function BackgroundImage({ photoInfo, currentAPI, url }) { const isCustomType = localStorage.getItem('backgroundType') === 'custom'; - const customBackgrounds = (() => { - try { - const stored = localStorage.getItem('customBackground'); - return stored && stored !== 'null' ? JSON.parse(stored) : []; - } catch { - return []; - } - })(); - const hasNoCustomImages = isCustomType && (!customBackgrounds || customBackgrounds.length === 0); + const [customBackgrounds, setCustomBackgrounds] = useState([]); + const [isLoading, setIsLoading] = useState(true); + + useEffect(() => { + const loadCustomBackgrounds = async () => { + if (isCustomType) { + try { + const backgrounds = await getAllBackgrounds(); + setCustomBackgrounds(backgrounds || []); + } catch (error) { + console.error('Failed to load custom backgrounds:', error); + setCustomBackgrounds([]); + } + } + setIsLoading(false); + }; + + loadCustomBackgrounds(); + }, [isCustomType]); + + const hasNoCustomImages = + isCustomType && !isLoading && (!customBackgrounds || customBackgrounds.length === 0); const handleOpenSettings = () => { updateHash('#settings/background/source'); @@ -28,21 +42,24 @@ function BackgroundImage({ photoInfo, currentAPI, url }) { <>
{hasNoCustomImages && ( -- {variables.getMessage('widgets.background.no_images_description') || 'Please add custom images in the Background settings'} + {variables.getMessage('widgets.background.no_images_description') || + 'Please add custom images in the Background settings'}