feat: auto-load images when user stops typing link (500ms debounce)

This commit is contained in:
Rasmus Q
2026-03-16 13:40:48 +00:00
parent 1808f6d0ac
commit 7084f703dc

View File

@@ -49,37 +49,50 @@
let color = $state<string | null>(item?.color || null); let color = $state<string | null>(item?.color || null);
let scrapedImages = $state<string[]>([]); let scrapedImages = $state<string[]>([]);
let isLoadingImages = $state(false); let isLoadingImages = $state(false);
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
// Form action based on mode // Form action based on mode
const formAction = isEdit ? '?/updateItem' : '?/addItem'; const formAction = isEdit ? '?/updateItem' : '?/addItem';
const submitLabel = isEdit ? t.form.saveChanges : t.wishlist.addWish; const submitLabel = isEdit ? t.form.saveChanges : t.wishlist.addWish;
const titleLabel = isEdit ? t.wishlist.editWish : t.form.addNewWish; const titleLabel = isEdit ? t.wishlist.editWish : t.form.addNewWish;
async function handleLinkChange(event: Event) { async function scrapeImages(url: string) {
if (!url || !url.startsWith('http')) return;
isLoadingImages = true;
scrapedImages = [];
try {
const response = await fetch('/api/scrape-images', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url })
});
if (response.ok) {
const data = await response.json();
scrapedImages = data.images || [];
}
} catch (error) {
console.error('Failed to scrape images:', error);
} finally {
isLoadingImages = false;
}
}
function handleLinkInput(event: Event) {
const input = event.target as HTMLInputElement; const input = event.target as HTMLInputElement;
linkUrl = input.value; linkUrl = input.value;
if (linkUrl && linkUrl.startsWith('http')) { // Clear existing timer
isLoadingImages = true; if (debounceTimer) {
scrapedImages = []; clearTimeout(debounceTimer);
try {
const response = await fetch('/api/scrape-images', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url: linkUrl })
});
if (response.ok) {
const data = await response.json();
scrapedImages = data.images || [];
}
} catch (error) {
console.error('Failed to scrape images:', error);
} finally {
isLoadingImages = false;
}
} }
// Set new timer to scrape after user stops typing for 500ms
debounceTimer = setTimeout(() => {
scrapeImages(linkUrl);
}, 500);
} }
function handleColorChange() { function handleColorChange() {
@@ -141,7 +154,7 @@
type="url" type="url"
placeholder="https://..." placeholder="https://..."
bind:value={linkUrl} bind:value={linkUrl}
oninput={handleLinkChange} oninput={handleLinkInput}
/> />
</div> </div>