diff --git a/src/svelte-interface/components/themes/BackgroundSelector.svelte b/src/svelte-interface/components/themes/BackgroundSelector.svelte index 7ba11a07..f87b7aac 100644 --- a/src/svelte-interface/components/themes/BackgroundSelector.svelte +++ b/src/svelte-interface/components/themes/BackgroundSelector.svelte @@ -54,7 +54,7 @@ } } - async function loadBackgrounds(): Promise { + async function loadBackgroundMetadata(): Promise { try { isLoading = true; error = null; @@ -67,10 +67,29 @@ const data = await readAllData(); selectedBackground = await getTheme(); - if (!isVisible) { - backgrounds = data; - return; - }; + // Only load metadata (id and type) for placeholders + backgrounds = data.map(({ id, type }) => ({ id, type, blob: null })); + } catch (e) { + if (e instanceof Error) { + error = e.message; + } else { + error = 'An unknown error occurred'; + } + } finally { + isLoading = false; + } + } + + async function loadFullBackgrounds(): Promise { + try { + isLoading = true; + error = null; + + if (!isIndexedDBSupported()) { + throw new Error("Your browser doesn't support IndexedDB. Unable to load backgrounds."); + } + + const data = await readAllData(); backgrounds = await preloadBackgrounds(data); } catch (e) { if (e instanceof Error) { @@ -134,14 +153,14 @@ }); onMount(() => { - loadBackgrounds(); + loadBackgroundMetadata(); observer = new IntersectionObserver((entries) => { if (entries[0].isIntersecting) { delay(() => { isVisible = true; - loadBackgrounds(); - }, 100); + loadFullBackgrounds(); + }, 450); observer.disconnect(); } }); @@ -160,20 +179,18 @@ {#if !isEditMode} handleFileChange(e.detail)} /> {/if} - {#if isVisible} - {#each imageBackgrounds as bg (bg.id)} + {#each imageBackgrounds as bg (bg.id)} + {#if isVisible && bg.blob} selectBackground(bg.id)} onDelete={() => deleteBackground(bg.id)}/> - {/each} - {:else} - {#each imageBackgrounds as bg (bg.id)} + {:else}
- {/each} - {/if} + {/if} + {/each}

Background Videos

@@ -181,8 +198,8 @@ {#if !isEditMode} handleFileChange(e.detail)} /> {/if} - {#if isVisible} - {#each videoBackgrounds as bg (bg.id)} + {#each videoBackgrounds as bg (bg.id)} + {#if isVisible && bg.blob} selectBackground(bg.id)} onDelete={() => deleteBackground(bg.id)} /> - {/each} - {:else} - {#each videoBackgrounds as bg (bg.id)} + {:else}
- {/each} - {/if} + {/if} + {/each} \ No newline at end of file