Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions frontend/src/components/Media/MediaView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,18 @@ export function MediaView({
const location = useLocation();
const { toggleFavourite } = useToggleFav();

// Loop to first image handler for slideshow
const handleLoopToStart = useCallback(() => {
dispatch(setCurrentViewIndex(0));
handlers.resetZoom();
}, [dispatch, handlers]);

// Slideshow functionality
const { isSlideshowActive, toggleSlideshow } = useSlideshow(
totalImages,
handleNextImage,
handleLoopToStart,
currentViewIndex,
);

// Folder Open functionality
Expand Down
26 changes: 23 additions & 3 deletions frontend/src/hooks/useSlideshow.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { useState, useEffect, useCallback } from 'react';

export const useSlideshow = (totalImages: number, onNextImage: () => void) => {
export const useSlideshow = (
totalImages: number,
onNextImage: () => void,
onLoopToStart?: () => void,
currentIndex?: number,
) => {
const [isSlideshowActive, setIsSlideshowActive] = useState(false);

useEffect(() => {
let slideshowInterval: NodeJS.Timeout | null = null;

if (isSlideshowActive && totalImages > 1) {
slideshowInterval = setInterval(() => {
onNextImage();
// Loop back to first image when at the end
if (
currentIndex !== undefined &&
onLoopToStart &&
currentIndex >= totalImages - 1
) {
onLoopToStart();
} else {
onNextImage();
}
}, 3000);
}

Expand All @@ -17,7 +31,13 @@ export const useSlideshow = (totalImages: number, onNextImage: () => void) => {
clearInterval(slideshowInterval);
}
};
}, [isSlideshowActive, totalImages, onNextImage]);
}, [
isSlideshowActive,
totalImages,
onNextImage,
onLoopToStart,
currentIndex,
]);

const toggleSlideshow = useCallback(() => {
setIsSlideshowActive((prev) => !prev);
Expand Down
Loading