Skip to content

armgit / ente

Public Imported from GitHub
0

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

5.7 KB
Raw Blame
import type { useGalleryLibrary } from "@/components/gallery/hooks/use-gallery-library";
import { useGalleryUploads } from "@/components/gallery/hooks/use-gallery-uploads";
import { useFileMutations } from "@/components/gallery/media/use-file-mutations";
import type { GalleryBarMode } from "@/components/gallery/reducer";
import type { GallerySectionOperations } from "@/components/gallery/sections/types";
import { useSaveGroups } from "ente-gallery/components/utils/save-groups";
import { ItemVisibility } from "ente-media/file-metadata";
import { haveOnlySystemCollections } from "ente-new/photos/services/collection-summary";
import type { SidebarActionID } from "ente-new/photos/services/search/types";
import { usePhotosAppContext } from "ente-new/photos/types/context";
import { useCallback, useState } from "react";
import { useCollectionDialogs } from "../media/use-collection-dialogs";
import type { GalleryNavigationState } from "../navigation/navigation-state";
export const useGalleryOperations = (
    libraryOwner: ReturnType<typeof useGalleryLibrary>,
    navigation: GalleryNavigationState,
    authenticateOpen: boolean,
    showSettings: (action?: SidebarActionID) => void,
) => {
    const {
        state,
        dispatch,
        peopleState,
        remotePull,
        remoteFilesPull,
        showSessionExpiredDialog,
    } = libraryOwner;
    const { user, normalCollectionSummaries } = state;
    const barMode: GalleryBarMode =
        navigation.destination.albumMode ?? "albums";
    const activeCollection =
        navigation.destination.section === "albums" ||
        navigation.destination.section === "places"
            ? state.collections.find(
                  (collection) =>
                      collection.id === navigation.destination.collectionID,
              )
            : undefined;
    const isCleanupViewOpen =
        navigation.overlay?.section === "duplicates" ||
        navigation.overlay?.section === "large-files";
    const { showLoadingBar, hideLoadingBar } = usePhotosAppContext();
    const [sectionActivity, setSectionActivity] = useState({
        selection: false,
        drop: false,
    });
    const [blockingLoad, setBlockingLoad] = useState(false);
    const { saveGroups, onAddSaveGroup, onRemoveSaveGroup } = useSaveGroups();
    const quickLinkVisibility =
        barMode == "hidden-albums"
            ? ItemVisibility.hidden
            : ItemVisibility.visible;
    const {
        publicLinkToast,
        setPublicLinkToast,
        handleFavoriteFileOp,
        onSendLinkError,
        handleFileViewerToggleFavorite,
        handleFileViewerFileVisibilityUpdate,
        handleFileViewerSendLink,
        handleMarkTempDeleted,
    } = useFileMutations({ state, dispatch, remotePull, quickLinkVisibility });
    const handleVisualFeedback = useCallback(() => {
        showLoadingBar();
        setTimeout(hideLoadingBar, 0);
    }, [showLoadingBar, hideLoadingBar]);
    const {
        openCollectionSelector,
        albumNameInputVisibilityProps,
        handleOpenCollectionSelector,
        handleCloseCollectionSelector,
        handleAddSingleFileToCollection,
        collectionSelectorProps,
        albumNameInputProps,
    } = useCollectionDialogs({ state, remotePull });
    const { openUploader, uploadTypeSelectorView, uploadProps, dropzoneProps } =
        useGalleryUploads({
            user,
            activeCollection,
            isFirstUpload: haveOnlySystemCollections(normalCollectionSummaries),
            isInHiddenSection: barMode == "hidden-albums",
            onUploadFile: (file) => dispatch({ type: "uploadFile", file }),
            onShowSubscription: () => showSettings("subscription"),
            onRemotePull: remotePull,
            onRemoteFilesPull: remoteFilesPull,
            onOpenCollectionSelector: handleOpenCollectionSelector,
            onCloseCollectionSelector: handleCloseCollectionSelector,
            onShowSessionExpiredDialog: showSessionExpiredDialog,
            setLoading: setBlockingLoad,
        });
    const sectionOperations: GallerySectionOperations = {
        dispatch,
        peopleState,
        mutations: {
            setPublicLinkToast,
            handleFavoriteFileOp,
            onSendLinkError,
            handleMarkTempDeleted,
        },
        selectionDisabled:
            uploadTypeSelectorView ||
            openCollectionSelector ||
            albumNameInputVisibilityProps.open ||
            authenticateOpen ||
            sectionActivity.selection,
        onAddSaveGroup,
        onToggleFavorite: handleFileViewerToggleFavorite,
        onFileVisibilityUpdate: handleFileViewerFileVisibilityUpdate,
        onSendLink: handleFileViewerSendLink,
        onMarkTempDeleted: handleMarkTempDeleted,
        onRemotePull: remotePull,
        onRemoteFilesPull: remoteFilesPull,
        onVisualFeedback: handleVisualFeedback,
        onAddFileToCollection: handleAddSingleFileToCollection,
        setBlockingLoad,
        onUpload: openUploader,
        saveGroups,
        setSectionActivity,
    };

    return {
        operations: sectionOperations,
        dropzoneProps: {
            ...dropzoneProps,
            disabled:
                dropzoneProps.disabled ||
                sectionActivity.drop ||
                isCleanupViewOpen,
        },
        blockingLoad,
        collectionSelectorProps,
        albumNameInputProps,
        downloadNotificationsProps: { saveGroups, onRemoveSaveGroup },
        uploadProps,
        quickLinkNotificationProps: {
            open: publicLinkToast.open,
            onCopy: () => {
                if (publicLinkToast.url)
                    void navigator.clipboard.writeText(publicLinkToast.url);
            },
            onClose: () =>
                setPublicLinkToast((prev) => ({ ...prev, open: false })),
        },
    };
};