Fetch photo list fresh on each slideshow start

Previously fetched once at page load — if photos were added after the
service started they wouldn't appear until a restart. Now fetches on
every slideshow activation so new photos show up immediately.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-07-28 21:27:40 +12:00
parent 21073f7469
commit afddfd5bb1

View File

@@ -234,19 +234,10 @@ function showAddToast() {
// ---------- Slideshow screensaver ---------- // ---------- Slideshow screensaver ----------
async function initSlideshow() { function initSlideshow() {
try {
const res = await fetch('/api/photos');
photos = await res.json();
} catch (e) {
console.error('Could not load photos:', e);
}
// Any interaction resets the inactivity timer (or dismisses the slideshow)
['touchstart', 'click', 'mousemove', 'keydown'].forEach(evt => { ['touchstart', 'click', 'mousemove', 'keydown'].forEach(evt => {
document.addEventListener(evt, handleUserActivity, { passive: true }); document.addEventListener(evt, handleUserActivity, { passive: true });
}); });
resetInactivityTimer(); resetInactivityTimer();
} }
@@ -260,21 +251,30 @@ function handleUserActivity() {
function resetInactivityTimer() { function resetInactivityTimer() {
clearTimeout(inactivityTimer); clearTimeout(inactivityTimer);
if (photos.length > 0) { inactivityTimer = setTimeout(showSlideshow, INACTIVITY_TIMEOUT);
inactivityTimer = setTimeout(showSlideshow, INACTIVITY_TIMEOUT);
}
} }
function showSlideshow() { async function showSlideshow() {
// Fetch fresh each time so newly added photos appear without a restart
try {
const res = await fetch('/api/photos');
photos = await res.json();
} catch (e) {
console.error('Could not load photos:', e);
}
if (!photos.length) {
resetInactivityTimer();
return;
}
slideshowActive = true; slideshowActive = true;
const overlay = document.getElementById('slideshow-overlay'); const overlay = document.getElementById('slideshow-overlay');
overlay.classList.remove('hidden'); overlay.classList.remove('hidden');
// Start from a random position in the shuffled list
slideIndex = Math.floor(Math.random() * photos.length); slideIndex = Math.floor(Math.random() * photos.length);
activeSlot = 'a'; activeSlot = 'a';
// Show first photo immediately; preload second into the inactive slot
loadSlide('a', photos[slideIndex]); loadSlide('a', photos[slideIndex]);
if (photos.length > 1) { if (photos.length > 1) {
loadSlide('b', photos[(slideIndex + 1) % photos.length]); loadSlide('b', photos[(slideIndex + 1) % photos.length]);