Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

refactor(photos):extract-gallery-slideshow-hook

Aswin Asok77f52402b62c
diff --git a/web/apps/photos/src/components/gallery/hooks/use-gallery-slideshow.ts b/web/apps/photos/src/components/gallery/hooks/use-gallery-slideshow.ts
new file mode 100644
index 0000000000..92845d1dfb
--- /dev/null
+++ b/web/apps/photos/src/components/gallery/hooks/use-gallery-slideshow.ts
@@ -0,0 +1,65 @@
+import { slideshowFiles } from "@/components/collections/album-slideshow";
+import type { LocalUser } from "ente-accounts/services/user";
+import type { Collection } from "ente-media/collection";
+import type { EnteFile } from "ente-media/file";
+import type { CollectionSummary } from "ente-new/photos/services/collection-summary";
+import { usePhotosAppContext } from "ente-new/photos/types/context";
+import { t } from "i18next";
+import { useCallback, useEffect, useState } from "react";
+
+export const useGallerySlideshow = ({
+ activeCollection,
+ activeCollectionFiles,
+ activeCollectionSummary,
+ isInSearchMode,
+ user,
+}: {
+ activeCollection: Collection | undefined;
+ activeCollectionFiles: EnteFile[];
+ activeCollectionSummary: CollectionSummary | undefined;
+ isInSearchMode: boolean;
+ user: LocalUser | undefined;
+}) => {
+ const { showNotification } = usePhotosAppContext();
+
+ const [slideshow, setSlideshow] = useState<{
+ files: EnteFile[];
+ title: string;
+ collectionID: number;
+ }>();
+ const closeSlideshow = useCallback(() => setSlideshow(undefined), []);
+
+ const startSlideshow = useCallback(() => {
+ if (!activeCollection) return;
+ const files = slideshowFiles(activeCollectionFiles);
+ if (!files.length) {
+ showNotification({
+ color: "primary",
+ title: t("no_photos_found_here"),
+ });
+ return;
+ }
+ setSlideshow({
+ files,
+ title: activeCollectionSummary?.name ?? activeCollection.name,
+ collectionID: activeCollection.id,
+ });
+ }, [
+ activeCollection,
+ activeCollectionFiles,
+ activeCollectionSummary?.name,
+ showNotification,
+ ]);
+
+ useEffect(() => {
+ if (
+ slideshow &&
+ (slideshow.collectionID !== activeCollection?.id ||
+ isInSearchMode ||
+ !user)
+ )
+ closeSlideshow();
+ }, [slideshow, activeCollection?.id, isInSearchMode, user, closeSlideshow]);
+
+ return { slideshow, startSlideshow, closeSlideshow };
+};
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 791eb25e9b..3c320871e2 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -24,7 +24,6 @@ import {
type AlbumDetails,
} from "@/components/collections/EditAlbumDetailsDialog";
import { GalleryBarAndListHeader } from "@/components/collections/GalleryBarAndListHeader";
-import { slideshowFiles } from "@/components/collections/album-slideshow";
import {
GalleryEmptyState,
PeopleConsentDialog,
@@ -48,6 +47,7 @@ import {
showCollectionSummaryWithAuthentication,
} from "@/components/gallery/helpers";
import { useGalleryAuthentication } from "@/components/gallery/hooks/use-gallery-authentication";
+import { useGallerySlideshow } from "@/components/gallery/hooks/use-gallery-slideshow";
import { useSubscriptionCompletionRedirect } from "@/components/gallery/hooks/use-subscription-completion-redirect";
import {
useGalleryReducer,
@@ -223,13 +223,6 @@ const Page: React.FC = () => {
[],
);
const [isFileViewerOpen, setIsFileViewerOpen] = useState(false);
- const [slideshow, setSlideshow] = useState<{
- files: EnteFile[];
- title: string;
- collectionID: number;
- }>();
- const closeSlideshow = useCallback(() => setSlideshow(undefined), []);
-
const [pendingFileNavigation, setPendingFileNavigation] = useState<{
fileIndex: number;
sidebar?: FileViewerInitialSidebar;
@@ -487,37 +480,13 @@ const Page: React.FC = () => {
tempDeletedFileIDs,
tempHiddenFileIDs,
]);
- const startSlideshow = useCallback(() => {
- if (!activeCollection) return;
- const files = slideshowFiles(activeCollectionFiles);
- if (!files.length) {
- showNotification({
- color: "primary",
- title: t("no_photos_found_here"),
- });
- return;
- }
- setSlideshow({
- files,
- title: activeCollectionSummary?.name ?? activeCollection.name,
- collectionID: activeCollection.id,
- });
- }, [
+ const { slideshow, startSlideshow, closeSlideshow } = useGallerySlideshow({
activeCollection,
activeCollectionFiles,
- activeCollectionSummary?.name,
- showNotification,
- ]);
-
- useEffect(() => {
- if (
- slideshow &&
- (slideshow.collectionID !== activeCollection?.id ||
- isInSearchMode ||
- !user)
- )
- closeSlideshow();
- }, [slideshow, activeCollection?.id, isInSearchMode, user, closeSlideshow]);
+ activeCollectionSummary,
+ isInSearchMode,
+ user,
+ });
const mapFileSource = useMemo(
() => ({