Skip to content

armgit / ente

Public Imported from GitHub
0

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

feat(EditAlbum):add-edit-album-and-unify-dialog

Aswin Asok694275926152
diff --git a/web/apps/photos/src/components/Collections/CollectionHeader.tsx b/web/apps/photos/src/components/Collections/CollectionHeader.tsx
index 52a36ca8cb..fbfaedc961 100644
--- a/web/apps/photos/src/components/Collections/CollectionHeader.tsx
+++ b/web/apps/photos/src/components/Collections/CollectionHeader.tsx
@@ -32,7 +32,6 @@ import {
OverflowMenu,
OverflowMenuOption,
} from "ente-base/components/OverflowMenu";
-import { SingleInputDialog } from "ente-base/components/SingleInputDialog";
import { useModalVisibility } from "ente-base/components/utils/modal";
import { useBaseContext } from "ente-base/context";
import type { AddSaveGroup } from "ente-gallery/components/utils/save-groups";
@@ -56,6 +55,7 @@ import {
isHiddenCollection,
leaveSharedCollection,
renameCollection,
+ updateCollectionDescription,
updateCollectionOrder,
updateCollectionSortOrder,
updateCollectionVisibility,
@@ -75,6 +75,10 @@ import { usePhotosAppContext } from "ente-new/photos/types/context";
import { t } from "i18next";
import React, { useCallback, useRef } from "react";
import { Trans } from "react-i18next";
+import {
+ EditAlbumDetailsDialog,
+ type AlbumDetails,
+} from "./EditAlbumDetailsDialog";
export interface CollectionHeaderProps {
collectionSummary: CollectionSummary;
@@ -93,7 +97,7 @@ export interface CollectionHeaderProps {
}
export const CollectionHeader: React.FC<CollectionHeaderProps> = (props) => {
- const { collectionSummary } = props;
+ const { activeCollection, collectionSummary } = props;
const { name, type, attributes, fileCount } = collectionSummary;
@@ -117,6 +121,9 @@ export const CollectionHeader: React.FC<CollectionHeaderProps> = (props) => {
<GalleryItemsSummary
name={name}
fileCount={fileCount}
+ description={
+ activeCollection?.pubMagicMetadata?.data.caption
+ }
endIcon={<EndIcon />}
/>
{shouldShowOptions(type) && (
@@ -152,8 +159,10 @@ const CollectionHeaderOptions: React.FC<CollectionHeaderProps> = ({
const { show: showSortOrderMenu, props: sortOrderMenuVisibilityProps } =
useModalVisibility();
- const { show: showAlbumNameInput, props: albumNameInputVisibilityProps } =
- useModalVisibility();
+ const {
+ show: showEditAlbumDetails,
+ props: editAlbumDetailsVisibilityProps,
+ } = useModalVisibility();
const { type: collectionSummaryType, fileCount } = collectionSummary;
const isQuickLinkAlbum =
@@ -178,13 +187,41 @@ const CollectionHeaderOptions: React.FC<CollectionHeaderProps> = ({
[showLoadingBar, hideLoadingBar, onGenericError, onRemotePull],
);
- const handleRenameCollection = useCallback(
- async (newName: string) => {
+ const handleEditAlbumDetails = useCallback(
+ async ({ name, description }: AlbumDetails) => {
if (!activeCollection) return;
- if (activeCollection.name !== newName) {
- await renameCollection(activeCollection, newName);
- void onRemotePull({ silent: true });
+
+ let didMutate = false;
+ let failure: { error: unknown } | undefined;
+ try {
+ if (activeCollection.name.trim() != name) {
+ didMutate = true;
+ await renameCollection(activeCollection, name);
+ }
+ if (
+ (
+ activeCollection.pubMagicMetadata?.data.caption ?? ""
+ ).trim() != description
+ ) {
+ didMutate = true;
+ await updateCollectionDescription(
+ activeCollection,
+ description,
+ );
+ }
+ } catch (error) {
+ failure = { error };
}
+
+ if (didMutate) {
+ try {
+ await onRemotePull({ silent: true, strict: true });
+ } catch (error) {
+ failure ??= { error };
+ }
+ }
+
+ if (failure) throw failure.error;
},
[activeCollection, onRemotePull],
);
@@ -582,13 +619,15 @@ const CollectionHeaderOptions: React.FC<CollectionHeaderProps> = ({
break;
}
menuOptions = [
- <OverflowMenuOption
- key="rename"
- onClick={showAlbumNameInput}
- startIcon={<EditIcon />}
- >
- {t("rename_album")}
- </OverflowMenuOption>,
+ activeCollection && (
+ <OverflowMenuOption
+ key="edit-details"
+ onClick={showEditAlbumDetails}
+ startIcon={<EditIcon />}
+ >
+ {t("edit_details")}
+ </OverflowMenuOption>
+ ),
canSetAlbumCover && (
<OverflowMenuOption
key="set-cover"
@@ -731,15 +770,17 @@ const CollectionHeaderOptions: React.FC<CollectionHeaderProps> = ({
onAscClick={changeSortOrderAsc}
onDescClick={changeSortOrderDesc}
/>
- <SingleInputDialog
- {...albumNameInputVisibilityProps}
- title={t("rename_album")}
- label={t("album_name")}
- initialValue={activeCollection?.name}
- submitButtonColor="primary"
- submitButtonTitle={t("rename")}
- onSubmit={handleRenameCollection}
- />
+ {activeCollection && editAlbumDetailsVisibilityProps.open && (
+ <EditAlbumDetailsDialog
+ key={activeCollection.id}
+ {...editAlbumDetailsVisibilityProps}
+ initialName={activeCollection.name}
+ initialDescription={
+ activeCollection.pubMagicMetadata?.data.caption ?? ""
+ }
+ onSubmit={handleEditAlbumDetails}
+ />
+ )}
</Box>
);
};
diff --git a/web/apps/photos/src/components/Collections/EditAlbumDetailsDialog.tsx b/web/apps/photos/src/components/Collections/EditAlbumDetailsDialog.tsx
new file mode 100644
index 0000000000..3cd7aca9a6
--- /dev/null
+++ b/web/apps/photos/src/components/Collections/EditAlbumDetailsDialog.tsx
@@ -0,0 +1,149 @@
+import {
+ Dialog,
+ DialogContent,
+ DialogTitle,
+ Stack,
+ TextField,
+} from "@mui/material";
+import { FocusVisibleButton } from "ente-base/components/mui/FocusVisibleButton";
+import { LoadingButton } from "ente-base/components/mui/LoadingButton";
+import type { ModalVisibilityProps } from "ente-base/components/utils/modal";
+import { useBaseContext } from "ente-base/context";
+import { maxAlbumDescriptionLength } from "ente-media/collection";
+import { albumDescriptionGraphemeCount } from "ente-new/photos/services/collection";
+import { useFormik } from "formik";
+import { t } from "i18next";
+import React, { useId } from "react";
+
+export interface AlbumDetails {
+ name: string;
+ description: string;
+}
+
+type EditAlbumDetailsDialogProps = ModalVisibilityProps & {
+ initialName: string;
+ initialDescription: string;
+ onSubmit: (details: AlbumDetails) => Promise<void>;
+};
+
+export const EditAlbumDetailsDialog: React.FC<EditAlbumDetailsDialogProps> = ({
+ open,
+ onClose,
+ initialName,
+ initialDescription,
+ onSubmit,
+}) => {
+ const { showMiniDialog, onGenericError } = useBaseContext();
+ const titleID = useId();
+ const formik = useFormik<AlbumDetails>({
+ initialValues: { name: initialName, description: initialDescription },
+ onSubmit: async ({
+ name: untrimmedName,
+ description: untrimmedDescription,
+ }) => {
+ const name = untrimmedName.trim();
+ const description = untrimmedDescription.trim();
+ const descriptionLength =
+ albumDescriptionGraphemeCount(description);
+ if (!name || descriptionLength > maxAlbumDescriptionLength) return;
+ try {
+ await onSubmit({ name, description });
+ onClose();
+ } catch (e) {
+ onGenericError(e);
+ }
+ },
+ });
+
+ const trimmedName = formik.values.name.trim();
+ const trimmedDescription = formik.values.description.trim();
+ const descriptionLength = albumDescriptionGraphemeCount(trimmedDescription);
+ const isDescriptionTooLong = descriptionLength > maxAlbumDescriptionLength;
+ const hasChanges =
+ trimmedName != initialName.trim() ||
+ trimmedDescription != initialDescription.trim();
+ const isSaveDisabled =
+ !trimmedName ||
+ isDescriptionTooLong ||
+ !hasChanges ||
+ formik.isSubmitting;
+
+ const handleClose = () => {
+ if (formik.isSubmitting) return;
+ if (hasChanges) {
+ showMiniDialog({
+ title: t("discard_changes"),
+ message: t("discard_changes_confirm_message"),
+ continue: {
+ text: t("discard"),
+ color: "critical",
+ action: onClose,
+ },
+ });
+ return;
+ }
+ onClose();
+ };
+
+ return (
+ <Dialog
+ open={open}
+ onClose={handleClose}
+ aria-labelledby={titleID}
+ maxWidth="xs"
+ fullWidth
+ slotProps={{ paper: { sx: { p: "8px 4px 4px 4px" } } }}
+ >
+ <DialogTitle id={titleID}>{t("edit_details")}</DialogTitle>
+ <DialogContent sx={{ "&&&": { pt: 0 } }}>
+ <Stack component="form" onSubmit={formik.handleSubmit}>
+ <TextField
+ name="name"
+ value={formik.values.name}
+ onChange={formik.handleChange}
+ label={t("album_name")}
+ autoFocus
+ fullWidth
+ margin="normal"
+ disabled={formik.isSubmitting}
+ error={!trimmedName}
+ helperText={!trimmedName ? t("required") : " "}
+ />
+ <TextField
+ name="description"
+ value={formik.values.description}
+ onChange={formik.handleChange}
+ label={t("description")}
+ placeholder={t("caption_placeholder")}
+ multiline
+ minRows={5}
+ fullWidth
+ disabled={formik.isSubmitting}
+ error={isDescriptionTooLong}
+ helperText={`${descriptionLength}/${maxAlbumDescriptionLength}`}
+ />
+ <Stack direction="row" sx={{ gap: "12px", mt: 3 }}>
+ <FocusVisibleButton
+ type="button"
+ fullWidth
+ color="secondary"
+ disabled={formik.isSubmitting}
+ onClick={handleClose}
+ >
+ {t("cancel")}
+ </FocusVisibleButton>
+ <LoadingButton
+ type="submit"
+ fullWidth
+ color="primary"
+ loading={formik.isSubmitting}
+ disabled={isSaveDisabled}
+ >
+ {t("save_changes")}
+ </LoadingButton>
+ </Stack>
+ </Stack>
+ </DialogContent>
+ </Dialog>
+ );
+};
diff --git a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
index 5b60d83103..ac2b725151 100644
--- a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
+++ b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
@@ -23,6 +23,7 @@ import type { Collection } from "ente-media/collection";
import {
GalleryItemsHeaderAdapter,
GalleryItemsSummary,
+ galleryItemsSummaryDescriptionHeight,
} from "ente-new/photos/components/gallery/ListHeader";
import {
collectionsSortBy,
@@ -209,7 +210,12 @@ export const GalleryBarAndListHeader: React.FC<
) : (
<></>
),
- height: 68,
+ height:
+ 68 +
+ (shouldRenderCollectionHeader &&
+ activeCollection?.pubMagicMetadata?.data.caption?.trim()
+ ? galleryItemsSummaryDescriptionHeight
+ : 0),
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
diff --git a/web/packages/base/locales/en-US/translation.json b/web/packages/base/locales/en-US/translation.json
index c8e085b75a..726c67a774 100644
--- a/web/packages/base/locales/en-US/translation.json
+++ b/web/packages/base/locales/en-US/translation.json
@@ -438,7 +438,6 @@
"mail_to_manage_subscription": "Please contact us at <a>{{emailID}}</a> to manage your subscription",
"rename": "Rename",
"rename_file": "Rename file",
- "rename_album": "Rename album",
"set_cover": "Set cover",
"select_cover_photo": "Select cover photo",
"reset_to_default": "Reset to default",
@@ -520,6 +519,7 @@
"enable_map": "Enable map",
"disable_map": "Disable map",
"details": "Details",
+ "edit_details": "Edit details",
"view_exif": "View all Exif data",
"no_exif": "No Exif data",
"use_shortcut_to_navigate": "Use <shortcut/> to navigate",
@@ -966,6 +966,7 @@
"server_endpoint": "Server endpoint",
"more_information": "More information",
"save": "Save",
+ "save_changes": "Save changes",
"theme": "Theme",
"system": "System",
"light": "Light",
diff --git a/web/packages/media/collection.ts b/web/packages/media/collection.ts
index 9b2d7f36f9..cb4569e722 100644
--- a/web/packages/media/collection.ts
+++ b/web/packages/media/collection.ts
@@ -250,12 +250,16 @@ export interface CollectionPublicMagicMetadataData {
asc?: boolean;
coverID?: number;
layout?: string;
+ caption?: string;
}
+export const maxAlbumDescriptionLength = 200;
+
export const CollectionPublicMagicMetadataData = z.looseObject({
asc: z.boolean().nullish().transform(nullToUndefined),
coverID: z.number().nullish().transform(nullToUndefined),
layout: z.string().nullish().transform(nullToUndefined),
+ caption: z.string().nullish().transform(nullToUndefined),
});
export interface CollectionShareeMagicMetadataData {
diff --git a/web/packages/new/photos/components/gallery/ListHeader.tsx b/web/packages/new/photos/components/gallery/ListHeader.tsx
index bce71a4456..1dec44a081 100644
--- a/web/packages/new/photos/components/gallery/ListHeader.tsx
+++ b/web/packages/new/photos/components/gallery/ListHeader.tsx
@@ -12,15 +12,25 @@ interface GalleryItemsSummaryProps {
name: string;
nameProps?: TypographyProps;
fileCount: number;
+ description?: string;
endIcon?: React.ReactNode;
// This cannot be the only way to invoke the action; it is mouse-only.
onNameClick?: () => void;
}
+const descriptionMaxLines = 3;
+const descriptionLineHeight = 17;
+const descriptionTopSpacing = 8;
+
+// ponytail: fixed 3-line reserve, measure if short descriptions leave visible slack.
+export const galleryItemsSummaryDescriptionHeight =
+ descriptionTopSpacing + descriptionMaxLines * descriptionLineHeight;
+
export const GalleryItemsSummary: React.FC<GalleryItemsSummaryProps> = ({
name,
nameProps,
fileCount,
+ description,
endIcon,
onNameClick,
}) => (
@@ -46,6 +56,24 @@ export const GalleryItemsSummary: React.FC<GalleryItemsSummaryProps> = ({
</Box>
)}
</Stack>
+ {description?.trim() && (
+ <Typography
+ variant="small"
+ sx={{
+ color: "text.muted",
+ mt: `${descriptionTopSpacing}px`,
+ height: `${descriptionMaxLines * descriptionLineHeight}px`,
+ lineHeight: `${descriptionLineHeight}px`,
+ overflow: "hidden",
+ textOverflow: "ellipsis",
+ display: "-webkit-box",
+ WebkitLineClamp: descriptionMaxLines,
+ WebkitBoxOrient: "vertical",
+ }}
+ >
+ {description.trim()}
+ </Typography>
+ )}
</div>
);
diff --git a/web/packages/new/photos/services/collection.ts b/web/packages/new/photos/services/collection.ts
index 9cd44a8ac6..b75f151397 100644
--- a/web/packages/new/photos/services/collection.ts
+++ b/web/packages/new/photos/services/collection.ts
@@ -16,6 +16,7 @@ import {
CollectionSubType,
decryptRemoteCollection,
findUserUncategorizedCollection,
+ maxAlbumDescriptionLength,
RemoteCollection,
RemotePublicURL,
type Collection,
@@ -972,6 +973,34 @@ export const updateCollectionCover = async (
coverID: number,
) => updateCollectionPublicMagicMetadata(collection, { coverID });
+const albumDescriptionSegmenter =
+ typeof Intl !== "undefined" && "Segmenter" in Intl
+ ? new Intl.Segmenter(undefined, { granularity: "grapheme" })
+ : null;
+
+export const albumDescriptionGraphemeCount = (description: string) =>
+ albumDescriptionSegmenter
+ ? Array.from(albumDescriptionSegmenter.segment(description)).length
+ : Array.from(description).length;
+
+export const updateCollectionDescription = async (
+ collection: Collection,
+ description: string,
+) => {
+ const normalizedDescription = description.trim();
+ if (
+ albumDescriptionGraphemeCount(normalizedDescription) >
+ maxAlbumDescriptionLength
+ ) {
+ throw new Error(
+ `Album descriptions cannot exceed ${maxAlbumDescriptionLength} characters`,
+ );
+ }
+ return updateCollectionPublicMagicMetadata(collection, {
+ caption: normalizedDescription,
+ });
+};
+
export const updateCollectionLayout = async (
collection: Collection,
layout: string,