Skip to content

Commit 1b7ca52

Browse files
feat: add looping functionality to slideshow for seamless experience (#1100)
Co-authored-by: Rahul Harpal <51887323+rahulharpal1603@users.noreply.github.com>
1 parent ee4a3c7 commit 1b7ca52

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

frontend/src/components/Media/MediaView.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,18 @@ export function MediaView({
8181
const location = useLocation();
8282
const { toggleFavourite } = useToggleFav();
8383

84+
// Loop to first image handler for slideshow
85+
const handleLoopToStart = useCallback(() => {
86+
dispatch(setCurrentViewIndex(0));
87+
handlers.resetZoom();
88+
}, [dispatch, handlers]);
89+
8490
// Slideshow functionality
8591
const { isSlideshowActive, toggleSlideshow } = useSlideshow(
8692
totalImages,
8793
handleNextImage,
94+
handleLoopToStart,
95+
currentViewIndex,
8896
);
8997

9098
/** Opens the system file explorer at the current image's location. */

frontend/src/hooks/useSlideshow.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
import { useState, useEffect, useCallback } from 'react';
22

3-
export const useSlideshow = (totalImages: number, onNextImage: () => void) => {
3+
export const useSlideshow = (
4+
totalImages: number,
5+
onNextImage: () => void,
6+
onLoopToStart?: () => void,
7+
currentIndex?: number,
8+
) => {
49
const [isSlideshowActive, setIsSlideshowActive] = useState(false);
510

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

914
if (isSlideshowActive && totalImages > 1) {
1015
slideshowInterval = setInterval(() => {
11-
onNextImage();
16+
// Loop back to first image when at the end
17+
if (
18+
currentIndex !== undefined &&
19+
onLoopToStart &&
20+
currentIndex >= totalImages - 1
21+
) {
22+
onLoopToStart();
23+
} else {
24+
onNextImage();
25+
}
1226
}, 3000);
1327
}
1428

@@ -17,7 +31,13 @@ export const useSlideshow = (totalImages: number, onNextImage: () => void) => {
1731
clearInterval(slideshowInterval);
1832
}
1933
};
20-
}, [isSlideshowActive, totalImages, onNextImage]);
34+
}, [
35+
isSlideshowActive,
36+
totalImages,
37+
onNextImage,
38+
onLoopToStart,
39+
currentIndex,
40+
]);
2141

2242
const toggleSlideshow = useCallback(() => {
2343
setIsSlideshowActive((prev) => !prev);

0 commit comments

Comments
 (0)