Skip to content

armgit / ente

Public Imported from GitHub
0

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

[web][photos] Add clean uncategorized option to remove duplicates

Anand Baburajan830e796546f4
diff --git a/web/apps/photos/src/components/Collections/CollectionHeader.tsx b/web/apps/photos/src/components/Collections/CollectionHeader.tsx
index 8c587e6343..a48f4873c7 100644
--- a/web/apps/photos/src/components/Collections/CollectionHeader.tsx
+++ b/web/apps/photos/src/components/Collections/CollectionHeader.tsx
@@ -1,3 +1,5 @@
+import { CleanIcon } from "@hugeicons/core-free-icons";
+import { HugeiconsIcon } from "@hugeicons/react";
import ArchiveOutlinedIcon from "@mui/icons-material/ArchiveOutlined";
import CheckIcon from "@mui/icons-material/Check";
import DeleteOutlinedIcon from "@mui/icons-material/DeleteOutlined";
@@ -36,6 +38,7 @@ import {
import { StarIcon } from "ente-new/photos/components/icons/StarIcon";
import { useSettingsSnapshot } from "ente-new/photos/components/utils/use-snapshot";
import {
+ cleanUncategorized,
defaultHiddenCollectionUserFacingName,
deleteCollection,
findDefaultHiddenCollectionIDs,
@@ -156,7 +159,8 @@ const CollectionHeaderOptions: React.FC<CollectionHeaderProps> = ({
onSelectPerson,
}) => {
const { showMiniDialog, onGenericError } = useBaseContext();
- const { showLoadingBar, hideLoadingBar } = usePhotosAppContext();
+ const { showLoadingBar, hideLoadingBar, showNotification } =
+ usePhotosAppContext();
const { mapEnabled } = useSettingsSnapshot();
const overflowMenuIconRef = useRef<SVGSVGElement | null>(null);
@@ -287,6 +291,26 @@ const CollectionHeaderOptions: React.FC<CollectionHeaderProps> = ({
setActiveCollectionID(PseudoCollectionID.all);
});
+ const confirmCleanUncategorized = () =>
+ showMiniDialog({
+ title: t("clean_uncategorized"),
+ message: t("clean_uncategorized_message"),
+ continue: {
+ text: t("clean_uncategorized"),
+ color: "primary",
+ action: doCleanUncategorized,
+ },
+ });
+
+ const doCleanUncategorized = wrap(async () => {
+ if (!activeCollection) return;
+ const count = await cleanUncategorized(activeCollection);
+ showNotification({
+ color: "secondary",
+ title: t("clean_uncategorized_success", { count }),
+ });
+ });
+
const _downloadCollection = async () => {
if (isActiveCollectionDownloadInProgress()) return;
@@ -464,13 +488,7 @@ const CollectionHeaderOptions: React.FC<CollectionHeaderProps> = ({
break;
case "uncategorized":
- menuOptions = [
- fileCount && (
- <DownloadOption key="download" onClick={downloadCollection}>
- {t("download_uncategorized")}
- </DownloadOption>
- ),
- ];
+ // Quick options (download + clean) are shown instead of a menu
break;
case "hiddenItems":
@@ -655,6 +673,7 @@ const CollectionHeaderOptions: React.FC<CollectionHeaderProps> = ({
onEmptyTrashClick={confirmEmptyTrash}
onDownloadClick={downloadCollection}
onShareClick={onCollectionShare}
+ onCleanUncategorizedClick={confirmCleanUncategorized}
/>
{validMenuOptions.length > 0 && (
<OverflowMenu
@@ -712,12 +731,14 @@ interface QuickOptionsProps {
onEmptyTrashClick: () => void;
onDownloadClick: () => void;
onShareClick: () => void;
+ onCleanUncategorizedClick: () => void;
}
const QuickOptions: React.FC<QuickOptionsProps> = ({
onEmptyTrashClick,
onDownloadClick,
onShareClick,
+ onCleanUncategorizedClick,
collectionSummary,
isDownloadInProgress,
}) => (
@@ -735,6 +756,11 @@ const QuickOptions: React.FC<QuickOptionsProps> = ({
onClick={onDownloadClick}
/>
))}
+ {showCleanUncategorizedQuickOption(collectionSummary) && (
+ <CleanUncategorizedQuickOption
+ onClick={onCleanUncategorizedClick}
+ />
+ )}
{showShareQuickOption(collectionSummary) && (
<ShareQuickOption
collectionSummary={collectionSummary}
@@ -755,6 +781,19 @@ const EmptyTrashQuickOption: React.FC<OptionProps> = ({ onClick }) => (
</Tooltip>
);
+const showCleanUncategorizedQuickOption = ({
+ type,
+ fileCount,
+}: CollectionSummary) => type == "uncategorized" && fileCount > 0;
+
+const CleanUncategorizedQuickOption: React.FC<OptionProps> = ({ onClick }) => (
+ <Tooltip title={t("clean_uncategorized")}>
+ <IconButton onClick={onClick}>
+ <HugeiconsIcon icon={CleanIcon} size={22} strokeWidth={1.5} />
+ </IconButton>
+ </Tooltip>
+);
+
const showDownloadQuickOption = ({ type, attributes }: CollectionSummary) =>
type == "album" ||
type == "folder" ||
diff --git a/web/packages/base/locales/en-US/translation.json b/web/packages/base/locales/en-US/translation.json
index 835ceb3315..f8a8549214 100644
--- a/web/packages/base/locales/en-US/translation.json
+++ b/web/packages/base/locales/en-US/translation.json
@@ -79,6 +79,10 @@
"download_album": "Download album",
"download_favorites": "Download favorites",
"download_uncategorized": "Download uncategorized",
+ "clean_uncategorized": "Clean uncategorized",
+ "clean_uncategorized_message": "This will remove all files from Uncategorized that are present in other albums.",
+ "clean_uncategorized_success_one": "1 file removed",
+ "clean_uncategorized_success_other": "{{count, number}} files removed",
"download_hidden_items": "Download hidden items",
"audio": "Audio",
"more": "More",
@@ -674,7 +678,6 @@
"download_complete": "Download complete",
"downloading_album": "Downloading {{name}}",
"creating_zip": "Creating ZIP for {{name}}",
- "preparing": "Preparing",
"downloading": "Downloading",
"preparing_part": "Preparing Part {{part}}",
"downloading_part": "Downloading Part {{part}}",
diff --git a/web/packages/new/photos/services/collection.ts b/web/packages/new/photos/services/collection.ts
index a00097017b..4383df7f64 100644
--- a/web/packages/new/photos/services/collection.ts
+++ b/web/packages/new/photos/services/collection.ts
@@ -1670,3 +1670,102 @@ export const movePendingRemovalActionsToUncategorized = async (
await moveFromCollection(collectionID, targetCollection, filesToMove);
}
};
+
+/**
+ * Remove files from the uncategorized collection if they exist in other
+ * user-owned albums.
+ *
+ * This is a cleanup operation that helps users remove duplicates from their
+ * uncategorized collection. Files that exist both in uncategorized and in
+ * other albums are moved out of uncategorized to one of those albums.
+ *
+ * Reads local state but does not modify it. The effects are on remote.
+ *
+ * @param uncategorizedCollection The user's uncategorized collection.
+ */
+export const cleanUncategorized = async (
+ uncategorizedCollection: Collection,
+): Promise<number> => {
+ const userID = ensureLocalUser().id;
+ const collections = await savedCollections();
+ const collectionFiles = await savedCollectionFiles();
+
+ // Get files in the uncategorized collection
+ const uncategorizedFiles = collectionFiles.filter(
+ (f) => f.collectionID == uncategorizedCollection.id,
+ );
+
+ if (!uncategorizedFiles.length) return 0;
+
+ // Build a map from file ID to the collections it belongs to (excluding
+ // uncategorized itself)
+ const fileIDToCollectionIDs = new Map<number, number[]>();
+ for (const file of collectionFiles) {
+ if (file.collectionID == uncategorizedCollection.id) continue;
+ const existing = fileIDToCollectionIDs.get(file.id);
+ if (existing) {
+ existing.push(file.collectionID);
+ } else {
+ fileIDToCollectionIDs.set(file.id, [file.collectionID]);
+ }
+ }
+
+ // Filter to only user-owned normal collections (not hidden, not shared)
+ const userOwnedCollectionIDs = new Set(
+ collections
+ .filter(
+ (c) =>
+ c.owner.id == userID &&
+ c.type != "uncategorized" &&
+ !isHiddenCollection(c),
+ )
+ .map((c) => c.id),
+ );
+
+ const collectionsByID = new Map(collections.map((c) => [c.id, c]));
+
+ // Find files that exist in other user-owned collections
+ const filesToClean = uncategorizedFiles.filter((file) => {
+ const otherCollectionIDs = fileIDToCollectionIDs.get(file.id);
+ if (!otherCollectionIDs) return false;
+ // Check if any of these collections are user-owned normal collections
+ return otherCollectionIDs.some((cid) =>
+ userOwnedCollectionIDs.has(cid),
+ );
+ });
+
+ if (!filesToClean.length) return 0;
+
+ // Group files by their target collection for efficient batching
+ const filesByTargetCollection = new Map<number, EnteFile[]>();
+ for (const file of filesToClean) {
+ const otherCollectionIDs = fileIDToCollectionIDs.get(file.id)!;
+ const targetCollectionID = otherCollectionIDs.find((cid) =>
+ userOwnedCollectionIDs.has(cid),
+ );
+ if (!targetCollectionID) continue;
+
+ const existing = filesByTargetCollection.get(targetCollectionID) ?? [];
+ existing.push(file);
+ filesByTargetCollection.set(targetCollectionID, existing);
+ }
+
+ // Move files in batches per target collection
+ let cleanedCount = 0;
+ for (const [targetCollectionID, files] of filesByTargetCollection) {
+ const targetCollection = collectionsByID.get(targetCollectionID);
+ if (!targetCollection) continue;
+
+ // Move files from uncategorized to the target collection.
+ // This effectively removes them from uncategorized since they already
+ // exist in the target.
+ await moveFromCollection(
+ uncategorizedCollection.id,
+ targetCollection,
+ files,
+ );
+ cleanedCount += files.length;
+ }
+
+ return cleanedCount;
+};