Skip to content

armgit / ente

Public Imported from GitHub
0

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

feat(coverImage):update-cover-image-for-albums

Aswin Asoke3128a4ab554
diff --git a/web/apps/photos/src/components/Collections/CollectionHeader.tsx b/web/apps/photos/src/components/Collections/CollectionHeader.tsx
index 2769b2704d..8440bf61c8 100644
--- a/web/apps/photos/src/components/Collections/CollectionHeader.tsx
+++ b/web/apps/photos/src/components/Collections/CollectionHeader.tsx
@@ -9,6 +9,7 @@ import ArchiveOutlinedIcon from "@mui/icons-material/ArchiveOutlined";
import CheckIcon from "@mui/icons-material/Check";
import DeleteOutlinedIcon from "@mui/icons-material/DeleteOutlined";
import EditIcon from "@mui/icons-material/Edit";
+import ImageOutlinedIcon from "@mui/icons-material/ImageOutlined";
import LinkIcon from "@mui/icons-material/Link";
import LogoutIcon from "@mui/icons-material/Logout";
import MapOutlinedIcon from "@mui/icons-material/MapOutlined";
@@ -102,6 +103,8 @@ export interface CollectionHeaderProps
onCollectionShare: () => void;
onCollectionManageLink: () => void;
onCollectionCast: () => void;
+ canSetAlbumCover: boolean;
+ onSetAlbumCover: () => void;
/**
* A function that can be used to create a UI notification to track the
* progress of user-initiated download, and to cancel it if needed.
@@ -159,6 +162,8 @@ const CollectionHeaderOptions: React.FC<CollectionHeaderProps> = ({
onCollectionShare,
onCollectionManageLink,
onCollectionCast,
+ canSetAlbumCover,
+ onSetAlbumCover,
onAddSaveGroup,
isActiveCollectionDownloadInProgress,
onMarkTempDeleted,
@@ -629,6 +634,15 @@ const CollectionHeaderOptions: React.FC<CollectionHeaderProps> = ({
>
{t("rename_album")}
</OverflowMenuOption>,
+ canSetAlbumCover && (
+ <OverflowMenuOption
+ key="set-cover"
+ onClick={onSetAlbumCover}
+ startIcon={<ImageOutlinedIcon />}
+ >
+ {t("set_cover")}
+ </OverflowMenuOption>
+ ),
<OverflowMenuOption
key="sort"
onClick={showSortOrderMenu}
diff --git a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
index ba0c9902ac..5333554d31 100644
--- a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
+++ b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
@@ -67,6 +67,8 @@ type GalleryBarAndListHeaderProps = Omit<
| "collectionNameByID"
| "onSelectCollection"
| "onSelectPerson"
+ | "canSetAlbumCover"
+ | "onSetAlbumCover"
> &
Pick<
CollectionShareProps,
@@ -109,6 +111,8 @@ export const GalleryBarAndListHeader: React.FC<
emailByUserID,
shareSuggestionEmails,
onRemotePull,
+ canSetAlbumCover,
+ onSetAlbumCover,
onAddSaveGroup,
onMarkTempDeleted,
onAddFileToCollection,
@@ -207,6 +211,8 @@ export const GalleryBarAndListHeader: React.FC<
onCollectionShare={openCollectionShare}
onCollectionManageLink={openCollectionManageLink}
onCollectionCast={showCollectionCast}
+ canSetAlbumCover={canSetAlbumCover}
+ onSetAlbumCover={onSetAlbumCover}
/>
) : mode != "people" && collectionSummary ? (
<GalleryItemsHeaderAdapter>
diff --git a/web/apps/photos/src/components/Collections/PickCoverPhotoDialog.tsx b/web/apps/photos/src/components/Collections/PickCoverPhotoDialog.tsx
new file mode 100644
index 0000000000..acfbcbc3bc
--- /dev/null
+++ b/web/apps/photos/src/components/Collections/PickCoverPhotoDialog.tsx
@@ -0,0 +1,249 @@
+import {
+ Box,
+ Button,
+ Dialog,
+ DialogActions,
+ DialogContent,
+ Stack,
+ Typography,
+} from "@mui/material";
+import type { LocalUser } from "ente-accounts/services/user";
+import { DialogCloseIconButton } from "ente-base/components/mui/DialogCloseIconButton";
+import { useIsSmallWidth } from "ente-base/components/utils/hooks";
+import { isSameDay } from "ente-base/date";
+import { formattedDate } from "ente-base/i18n-date";
+import type { Collection } from "ente-media/collection";
+import type { EnteFile } from "ente-media/file";
+import { fileCreationTime } from "ente-media/file-metadata";
+import { FileType } from "ente-media/file-type";
+import { t } from "i18next";
+import React, { useCallback, useEffect, useMemo, useState } from "react";
+import AutoSizer from "react-virtualized-auto-sizer";
+import { type SelectedState } from "utils/file";
+import { FileList, type FileListAnnotatedFile } from "../FileList";
+
+interface PickCoverPhotoDialogProps {
+ open: boolean;
+ onClose: () => void;
+ collection: Collection;
+ files: EnteFile[];
+ user: LocalUser;
+ canResetToDefault: boolean;
+ onUseSelectedPhoto: (file: EnteFile) => Promise<boolean>;
+ onResetToDefault: () => Promise<boolean>;
+}
+
+/**
+ * Picker dialog for selecting a single file to use as an album cover.
+ */
+export const PickCoverPhotoDialog: React.FC<PickCoverPhotoDialogProps> = ({
+ open,
+ onClose,
+ collection,
+ files,
+ user,
+ canResetToDefault,
+ onUseSelectedPhoto,
+ onResetToDefault,
+}) => {
+ const isFullScreen = useIsSmallWidth();
+ const [isSubmitting, setIsSubmitting] = useState(false);
+ const [selected, setSelected] = useState<SelectedState>(
+ createEmptySelection(collection.id),
+ );
+
+ useEffect(() => {
+ if (open) {
+ setSelected(createEmptySelection(collection.id));
+ }
+ }, [open, collection.id]);
+
+ const annotatedFiles = useMemo(
+ (): FileListAnnotatedFile[] =>
+ files
+ .filter((file) => file.metadata.fileType !== FileType.video)
+ .map((file) => ({
+ file,
+ timelineDateString: fileTimelineDateString(file),
+ })),
+ [files],
+ );
+
+ const selectedFile = useMemo(() => {
+ for (const [key, value] of Object.entries(selected)) {
+ if (typeof value !== "boolean" || !value) continue;
+ const selectedFileID = Number(key);
+ return files.find(({ id }) => id === selectedFileID);
+ }
+ return undefined;
+ }, [selected, files]);
+
+ const handleItemClick = useCallback(
+ (index: number) => {
+ const file = annotatedFiles[index]?.file;
+ if (!file) return;
+ setSelected(createSingleSelection(file, collection.id));
+ },
+ [annotatedFiles, collection.id],
+ );
+
+ const handleUseSelectedPhoto = useCallback(async () => {
+ if (!selectedFile) return;
+
+ setIsSubmitting(true);
+ try {
+ const didUpdate = await onUseSelectedPhoto(selectedFile);
+ if (didUpdate) onClose();
+ } finally {
+ setIsSubmitting(false);
+ }
+ }, [onUseSelectedPhoto, onClose, selectedFile]);
+
+ const handleResetToDefault = useCallback(async () => {
+ setIsSubmitting(true);
+ try {
+ const didReset = await onResetToDefault();
+ if (didReset) onClose();
+ } finally {
+ setIsSubmitting(false);
+ }
+ }, [onResetToDefault, onClose]);
+
+ return (
+ <Dialog
+ open={open}
+ onClose={isSubmitting ? undefined : onClose}
+ fullWidth
+ fullScreen={isFullScreen}
+ slotProps={{
+ paper: {
+ sx: !isFullScreen
+ ? {
+ width: "min(980px, calc(100vw - 64px))",
+ height: "min(760px, calc(100vh - 64px))",
+ }
+ : undefined,
+ },
+ }}
+ >
+ <Stack sx={{ width: "100%", height: "100%" }}>
+ <Stack
+ direction="row"
+ sx={{
+ alignItems: "center",
+ gap: 1,
+ px: 2,
+ pt: 2,
+ pb: 1,
+ }}
+ >
+ <Box sx={{ flex: 1, minWidth: 0 }}>
+ <Typography variant="h5">
+ {t("select_cover_photo")}
+ </Typography>
+ <Typography
+ variant="small"
+ sx={{
+ color: "text.muted",
+ fontWeight: "regular",
+ }}
+ noWrap
+ >
+ {collection.name}
+ </Typography>
+ </Box>
+ {!isSubmitting && (
+ <DialogCloseIconButton onClose={onClose} />
+ )}
+ </Stack>
+
+ <DialogContent
+ sx={{ px: 2, py: 0, flex: 1, minHeight: 0, display: "flex" }}
+ >
+ <AutoSizer>
+ {({ width, height }) =>
+ width > 0 && height > 0 ? (
+ <FileList
+ {...{
+ width,
+ height,
+ user,
+ annotatedFiles,
+ selected,
+ setSelected,
+ }}
+ mode="albums"
+ enableSelect={false}
+ activeCollectionID={collection.id}
+ onItemClick={handleItemClick}
+ />
+ ) : (
+ <></>
+ )
+ }
+ </AutoSizer>
+ </DialogContent>
+
+ <DialogActions
+ sx={{
+ px: 2,
+ pt: 1,
+ pb: 2,
+ gap: 1,
+ justifyContent: "flex-end",
+ }}
+ >
+ <Button
+ color={canResetToDefault ? "secondary" : "inherit"}
+ onClick={
+ canResetToDefault ? handleResetToDefault : onClose
+ }
+ disabled={isSubmitting}
+ sx={isFullScreen ? { flex: 1 } : undefined}
+ >
+ {canResetToDefault
+ ? t("reset_to_default")
+ : t("cancel")}
+ </Button>
+ <Button
+ variant="contained"
+ color="primary"
+ onClick={handleUseSelectedPhoto}
+ disabled={isSubmitting || !selectedFile}
+ sx={isFullScreen ? { flex: 1 } : undefined}
+ >
+ {t("use_selected_photo")}
+ </Button>
+ </DialogActions>
+ </Stack>
+ </Dialog>
+ );
+};
+
+const createEmptySelection = (collectionID: number): SelectedState => ({
+ ownCount: 0,
+ count: 0,
+ collectionID,
+ context: { mode: "albums", collectionID },
+});
+
+const createSingleSelection = (
+ file: EnteFile,
+ collectionID: number,
+): SelectedState =>
+ ({
+ [file.id]: true,
+ ownCount: 0,
+ count: 0,
+ collectionID,
+ context: { mode: "albums", collectionID },
+ }) as SelectedState;
+
+const fileTimelineDateString = (file: EnteFile) => {
+ const date = new Date(fileCreationTime(file) / 1000);
+ return isSameDay(date, new Date())
+ ? t("today")
+ : isSameDay(date, new Date(Date.now() - 24 * 60 * 60 * 1000))
+ ? t("yesterday")
+ : formattedDate(date);
+};
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 494c781b70..a4c13937fa 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -12,6 +12,7 @@ import type { AddToAlbumPhase } from "components/AlbumAddedNotification";
import { AlbumAddedNotification } from "components/AlbumAddedNotification";
import { AuthenticateUser } from "components/AuthenticateUser";
import { GalleryBarAndListHeader } from "components/Collections/GalleryBarAndListHeader";
+import { PickCoverPhotoDialog } from "components/Collections/PickCoverPhotoDialog";
import { DownloadStatusNotifications } from "components/DownloadStatusNotifications";
import type { FileListHeaderOrFooter } from "components/FileList";
import { FileListWithViewer } from "components/FileListWithViewer";
@@ -50,7 +51,7 @@ import { FullScreenDropZone } from "ente-gallery/components/FullScreenDropZone";
import { type UploadTypeSelectorIntent } from "ente-gallery/components/Upload";
import { useSaveGroups } from "ente-gallery/components/utils/save-groups";
import { type FileViewerInitialSidebar } from "ente-gallery/components/viewer/FileViewer";
-import { type Collection } from "ente-media/collection";
+import { CollectionSubType, type Collection } from "ente-media/collection";
import { type EnteFile } from "ente-media/file";
import { type ItemVisibility } from "ente-media/file-metadata";
import {
@@ -106,6 +107,7 @@ import {
createQuickLinkCollection,
removeFromCollection,
removeFromFavoritesCollection,
+ updateCollectionCover,
} from "ente-new/photos/services/collection";
import {
haveOnlySystemCollections,
@@ -304,6 +306,10 @@ const Page: React.FC = () => {
useModalVisibility();
const { show: showEditLocation, props: editLocationVisibilityProps } =
useModalVisibility();
+ const {
+ show: showPickCoverPhotoDialog,
+ props: pickCoverPhotoDialogVisibilityProps,
+ } = useModalVisibility();
// Progress UI state for single-file add-to-album from the FileViewer
const [addToAlbumProgress, setAddToAlbumProgress] = useState<{
@@ -389,6 +395,33 @@ const Page: React.FC = () => {
const activePerson =
state.view?.type == "people" ? state.view.activePerson : undefined;
const activePersonID = activePerson?.id;
+ const isOwnedAlbumEligibleForCover = useMemo(() => {
+ if (!activeCollection || !activeCollectionSummary || !user)
+ return false;
+
+ if (activeCollection.owner.id != user.id) return false;
+ if (
+ activeCollection.magicMetadata?.data.subType ==
+ CollectionSubType.quicklink
+ ) {
+ return false;
+ }
+
+ return (
+ activeCollectionSummary.attributes.has("album") ||
+ activeCollectionSummary.attributes.has("folder")
+ );
+ }, [activeCollection, activeCollectionSummary, user]);
+ const activeCollectionFiles = useMemo(
+ () =>
+ !activeCollection
+ ? []
+ : state.collectionFiles.filter(
+ ({ collectionID }) =>
+ collectionID === activeCollection.id,
+ ),
+ [activeCollection, state.collectionFiles],
+ );
const selectedFilesInView = useMemo(
() => getSelectedFiles(selected, filteredFiles),
[selected, filteredFiles],
@@ -1368,6 +1401,33 @@ const Page: React.FC = () => {
return count;
}, [favoriteFileIDs, selected]);
+ const handleUpdateCollectionCover = useCallback(
+ async (coverID: number) => {
+ if (!activeCollection || !isOwnedAlbumEligibleForCover)
+ return false;
+
+ showLoadingBar();
+ try {
+ await updateCollectionCover(activeCollection, coverID);
+ await remotePull({ silent: true });
+ return true;
+ } catch (e) {
+ onGenericError(e);
+ return false;
+ } finally {
+ hideLoadingBar();
+ }
+ },
+ [
+ activeCollection,
+ isOwnedAlbumEligibleForCover,
+ showLoadingBar,
+ remotePull,
+ onGenericError,
+ hideLoadingBar,
+ ],
+ );
+
/**
* Handle a context menu action on a file.
*
@@ -1546,6 +1606,21 @@ const Page: React.FC = () => {
],
);
+ const handleOpenPickCoverPhotoDialog = useCallback(() => {
+ if (!isOwnedAlbumEligibleForCover) return;
+ showPickCoverPhotoDialog();
+ }, [isOwnedAlbumEligibleForCover, showPickCoverPhotoDialog]);
+
+ const handleUseSelectedCoverPhoto = useCallback(
+ async (file: EnteFile) => handleUpdateCollectionCover(file.id),
+ [handleUpdateCollectionCover],
+ );
+
+ const handleResetCollectionCover = useCallback(
+ async () => handleUpdateCollectionCover(0),
+ [handleUpdateCollectionCover],
+ );
+
const handleCloseCollectionSelector = useCallback(
() => setOpenCollectionSelector(false),
[],
@@ -1757,6 +1832,8 @@ const Page: React.FC = () => {
fileNormalCollectionIDs,
collectionNameByID,
onSelectCollection: handleSelectCollection,
+ canSetAlbumCover: isOwnedAlbumEligibleForCover,
+ onSetAlbumCover: handleOpenPickCoverPhotoDialog,
}}
mode={barMode}
shouldHide={isInSearchMode}
@@ -1892,6 +1969,20 @@ const Page: React.FC = () => {
}
/>
)}
+ {activeCollection && (
+ <PickCoverPhotoDialog
+ {...pickCoverPhotoDialogVisibilityProps}
+ collection={activeCollection}
+ files={activeCollectionFiles}
+ user={user}
+ canResetToDefault={
+ (activeCollection.pubMagicMetadata?.data.coverID ?? 0) >
+ 0
+ }
+ onUseSelectedPhoto={handleUseSelectedCoverPhoto}
+ onResetToDefault={handleResetCollectionCover}
+ />
+ )}
<Export {...exportVisibilityProps} {...{ collectionNameByID }} />
<AuthenticateUser
open={authenticateUserVisibilityProps.open}
diff --git a/web/packages/base/locales/en-US/translation.json b/web/packages/base/locales/en-US/translation.json
index f9995a12fb..2e01430f60 100644
--- a/web/packages/base/locales/en-US/translation.json
+++ b/web/packages/base/locales/en-US/translation.json
@@ -284,6 +284,10 @@
"rename": "Rename",
"rename_file": "Rename file",
"rename_album": "Rename album",
+ "set_cover": "Set cover",
+ "select_cover_photo": "Select cover photo",
+ "use_selected_photo": "Use selected photo",
+ "reset_to_default": "Reset to default",
"delete_album": "Delete album",
"delete_album_title": "Delete album?",
"delete_album_message": "Also delete the photos (and videos) present in this album from <a>all</a> other albums they are part of?",
diff --git a/web/packages/new/photos/services/collection.ts b/web/packages/new/photos/services/collection.ts
index ed15cdf6f7..6a0d4f56bd 100644
--- a/web/packages/new/photos/services/collection.ts
+++ b/web/packages/new/photos/services/collection.ts
@@ -1106,6 +1106,24 @@ export const updateCollectionSortOrder = async (
asc: boolean,
) => updateCollectionPublicMagicMetadata(collection, { asc });
+/**
+ * Change the cover photo of a collection on remote.
+ *
+ * Remote only, does not modify local state.
+ *
+ * This function works only for collections owned by the user.
+ *
+ * @param collection The collection whose cover we want to change.
+ *
+ * @param coverID The file ID to set as the cover.
+ *
+ * Pass `0` to reset to the default cover.
+ */
+export const updateCollectionCover = async (
+ collection: Collection,
+ coverID: number,
+) => updateCollectionPublicMagicMetadata(collection, { coverID });
+
/**
* Change the layout type of a collection on remote.
*