Skip to content

armgit / ente

Public Imported from GitHub
0

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

refactor(photos):embed-duplicates-in-gallery

Aswin Asok69606f5d431d
diff --git a/web/apps/photos/src/pages/duplicates.tsx b/web/apps/photos/src/components/gallery/DuplicatesView.tsx
similarity index 95%
rename from web/apps/photos/src/pages/duplicates.tsx
rename to web/apps/photos/src/components/gallery/DuplicatesView.tsx
index f83bbd73d3..1f016c17fa 100644
--- a/web/apps/photos/src/pages/duplicates.tsx
+++ b/web/apps/photos/src/components/gallery/DuplicatesView.tsx
@@ -18,7 +18,6 @@ import {
Tooltip,
Typography,
} from "@mui/material";
-import { useRedirectIfNeedsCredentials } from "ente-accounts/components/utils/use-redirect";
import { CenteredFill, SpacedRow } from "ente-base/components/containers";
import { ActivityErrorIndicator } from "ente-base/components/ErrorIndicator";
import { ActivityIndicator } from "ente-base/components/mui/ActivityIndicator";
@@ -41,7 +40,6 @@ import {
type ThumbnailGridLayoutParams,
} from "ente-new/photos/components/utils/thumbnail-grid-layout";
import { t } from "i18next";
-import { useRouter } from "next/router";
import React, {
memo,
useCallback,
@@ -56,23 +54,36 @@ import {
type ListChildComponentProps,
} from "react-window";
-const Page: React.FC = () => {
+interface DuplicatesViewProps {
+ onBack: () => void;
+ onRemoteFilesPull: () => Promise<void>;
+}
+
+export function DuplicatesView({
+ onBack,
+ onRemoteFilesPull,
+}: DuplicatesViewProps) {
const { onGenericError } = useBaseContext();
const [state, dispatch] = useReducer(dedupReducer, initialDedupState);
- useRedirectIfNeedsCredentials("/duplicates");
-
useEffect(() => {
+ let isCurrent = true;
dispatch({ type: "analyze" });
void deduceDuplicates()
- .then((duplicateGroups) =>
- dispatch({ type: "analysisCompleted", duplicateGroups }),
- )
+ .then((duplicateGroups) => {
+ if (isCurrent)
+ dispatch({ type: "analysisCompleted", duplicateGroups });
+ })
.catch((e: unknown) => {
- log.error("Failed to detect duplicates", e);
- dispatch({ type: "analysisFailed" });
+ if (isCurrent) {
+ log.error("Failed to detect duplicates", e);
+ dispatch({ type: "analysisFailed" });
+ }
});
+ return () => {
+ isCurrent = false;
+ };
}, []);
const handleRemoveDuplicates = useCallback(() => {
@@ -81,6 +92,7 @@ const Page: React.FC = () => {
state.duplicateGroups,
(progress: number) =>
dispatch({ type: "setDedupeProgress", progress }),
+ onRemoteFilesPull,
)
.then((removedGroupIDs) =>
dispatch({ type: "dedupeCompleted", removedGroupIDs }),
@@ -90,7 +102,7 @@ const Page: React.FC = () => {
onGenericError(e);
dispatch({ type: "dedupeFailed" });
});
- }, [state.duplicateGroups, onGenericError]);
+ }, [state.duplicateGroups, onGenericError, onRemoteFilesPull]);
const contents = (() => {
switch (state.analysisStatus) {
@@ -121,8 +133,9 @@ const Page: React.FC = () => {
})();
return (
- <Stack sx={{ height: "100vh" }}>
+ <Stack sx={{ flex: 1, minHeight: 0 }}>
<Navbar
+ onBack={onBack}
sortOrder={state.sortOrder}
onChangeSortOrder={(sortOrder) =>
dispatch({ type: "changeSortOrder", sortOrder })
@@ -132,9 +145,7 @@ const Page: React.FC = () => {
{contents}
</Stack>
);
-};
-
-export default Page;
+}
type SortOrder = "prunableCount" | "prunableSize";
@@ -274,18 +285,18 @@ const deducePrunableCountAndSize = (duplicateGroups: DuplicateGroup[]) => {
};
interface NavbarProps {
+ onBack: () => void;
sortOrder: SortOrder;
onChangeSortOrder: (sortOrder: SortOrder) => void;
onDeselectAll: () => void;
}
const Navbar: React.FC<NavbarProps> = ({
+ onBack,
sortOrder,
onChangeSortOrder,
onDeselectAll,
}) => {
- const router = useRouter();
-
return (
<Stack
direction="row"
@@ -302,7 +313,7 @@ const Navbar: React.FC<NavbarProps> = ({
minWidth: "100px",
}}
>
- <IconButton onClick={router.back}>
+ <IconButton onClick={onBack} aria-label={t("go_back")}>
<ArrowBackIcon />
</IconButton>
</Box>
diff --git a/web/apps/photos/src/components/sidebar/FreeUpSpace.tsx b/web/apps/photos/src/components/sidebar/FreeUpSpace.tsx
index c4eaef7427..0aa8d154f2 100644
--- a/web/apps/photos/src/components/sidebar/FreeUpSpace.tsx
+++ b/web/apps/photos/src/components/sidebar/FreeUpSpace.tsx
@@ -6,7 +6,6 @@ import {
import { RowCard } from "ente-base/components/v2/RowCard";
import type { SidebarActionID } from "ente-new/photos/services/search/types";
import { t } from "i18next";
-import { useRouter } from "next/router";
import React, { useCallback, useEffect } from "react";
export type FreeUpSpaceAction = Extract<
@@ -16,6 +15,7 @@ export type FreeUpSpaceAction = Extract<
type FreeUpSpaceProps = NestedSidebarDrawerVisibilityProps & {
onShowLargeFiles: () => void;
+ onShowDuplicates: () => void;
pendingAction?: FreeUpSpaceAction;
onActionHandled?: (action?: FreeUpSpaceAction) => void;
};
@@ -27,9 +27,8 @@ export const FreeUpSpace: React.FC<FreeUpSpaceProps> = ({
pendingAction,
onActionHandled,
onShowLargeFiles,
+ onShowDuplicates,
}) => {
- const router = useRouter();
-
const handleRootClose = useCallback(() => {
onClose();
onRootClose();
@@ -37,8 +36,8 @@ export const FreeUpSpace: React.FC<FreeUpSpaceProps> = ({
const handleDeduplicate = useCallback(() => {
onRootClose();
- void router.push("/duplicates");
- }, [onRootClose, router]);
+ onShowDuplicates();
+ }, [onRootClose, onShowDuplicates]);
const handleLargeFiles = useCallback(() => {
onRootClose();
diff --git a/web/apps/photos/src/components/sidebar/Sidebar.tsx b/web/apps/photos/src/components/sidebar/Sidebar.tsx
index 36c1efebe5..78aa26565e 100644
--- a/web/apps/photos/src/components/sidebar/Sidebar.tsx
+++ b/web/apps/photos/src/components/sidebar/Sidebar.tsx
@@ -92,6 +92,7 @@ type SidebarProps = ModalVisibilityProps & {
pendingAction?: SidebarActionID;
onActionHandled?: (actionID: SidebarActionID) => void;
onShowLargeFiles: () => void;
+ onShowDuplicates: () => void;
onShowPlanSelector: () => void;
onShowCollectionSummary: (
collectionSummaryID: number,
@@ -114,6 +115,7 @@ export const Sidebar: React.FC<SidebarProps> = ({
onActionHandled,
onShowPlanSelector,
onShowLargeFiles,
+ onShowDuplicates,
onShowCollectionSummary,
collectionNameByID,
onAuthenticateUser,
@@ -440,6 +442,7 @@ export const Sidebar: React.FC<SidebarProps> = ({
{freeUpSpaceVisibilityProps.open && (
<FreeUpSpace
onShowLargeFiles={onShowLargeFiles}
+ onShowDuplicates={onShowDuplicates}
{...freeUpSpaceVisibilityProps}
onRootClose={onClose}
pendingAction={pendingFreeUpSpaceAction}
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 42adeee43f..8c96f77784 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -35,6 +35,7 @@ import {
SearchResultsHeader,
type RemotePullOpts,
} from "@/components/gallery";
+import { DuplicatesView } from "@/components/gallery/DuplicatesView";
import { LargeFilesView } from "@/components/gallery/LargeFilesView";
import {
NormalNavbarContents,
@@ -191,7 +192,10 @@ const Page: React.FC = () => {
const isOffline = useIsOffline();
const [state, dispatch] = useGalleryReducer();
- const [isLargeFilesOpen, setIsLargeFilesOpen] = useState(false);
+ const [cleanupView, setCleanupView] = useState<
+ "large-files" | "duplicates"
+ >();
+ const isCleanupViewOpen = cleanupView !== undefined;
const [isFirstLoad, setIsFirstLoad] = useState(false);
const [isContextMenuOpen, setIsContextMenuOpen] = useState(false);
const [trashFilesDialog, setTrashFilesDialog] = useState<{
@@ -830,16 +834,16 @@ const Page: React.FC = () => {
const keyboardShortcutHandlerRef = useRef({
selectAll,
clearSelection,
- isLargeFilesOpen,
+ isCleanupViewOpen,
});
useEffect(() => {
keyboardShortcutHandlerRef.current = {
selectAll,
clearSelection,
- isLargeFilesOpen,
+ isCleanupViewOpen,
};
- }, [selectAll, clearSelection, isLargeFilesOpen]);
+ }, [selectAll, clearSelection, isCleanupViewOpen]);
const showSessionExpiredDialog = useCallback(
() => showMiniDialog(sessionExpiredDialogAttributes(logout)),
@@ -926,7 +930,7 @@ const Page: React.FC = () => {
const setupSelectAllKeyBoardShortcutHandler = () => {
const handleKeyUp = (e: KeyboardEvent) => {
- if (keyboardShortcutHandlerRef.current.isLargeFilesOpen) return;
+ if (keyboardShortcutHandlerRef.current.isCleanupViewOpen) return;
switch (e.key) {
case "Escape":
keyboardShortcutHandlerRef.current.clearSelection();
@@ -1399,7 +1403,7 @@ const Page: React.FC = () => {
const handleShowCollectionSummaryWithID = useCallback(
(collectionSummaryID: number | undefined) => {
- setIsLargeFilesOpen(false);
+ setCleanupView(undefined);
// Museum deletes collection contents asynchronously; pull on entry.
if (collectionSummaryID == PseudoCollectionID.trash) {
void remoteFilesPull();
@@ -1435,21 +1439,33 @@ const Page: React.FC = () => {
[authenticateUser, handleShowCollectionSummaryWithID, barMode],
);
- const handleShowLargeFiles = useCallback(() => {
- setSelected({
- ownCount: 0,
- count: 0,
- collectionID: 0,
- context: undefined,
- });
- closeCollectionMap();
- closeSlideshow();
- dispatch({ type: "exitSearch", shouldExitSearchMode: true });
- setIsLargeFilesOpen(true);
- }, [closeCollectionMap, closeSlideshow]);
-
- const handleCloseLargeFiles = useCallback(() => {
- setIsLargeFilesOpen(false);
+ const handleShowCleanupView = useCallback(
+ (view: "large-files" | "duplicates") => {
+ setSelected({
+ ownCount: 0,
+ count: 0,
+ collectionID: 0,
+ context: undefined,
+ });
+ closeCollectionMap();
+ closeSlideshow();
+ dispatch({ type: "exitSearch", shouldExitSearchMode: true });
+ setCleanupView(view);
+ },
+ [closeCollectionMap, closeSlideshow],
+ );
+
+ const handleShowLargeFiles = useCallback(
+ () => handleShowCleanupView("large-files"),
+ [handleShowCleanupView],
+ );
+ const handleShowDuplicates = useCallback(
+ () => handleShowCleanupView("duplicates"),
+ [handleShowCleanupView],
+ );
+
+ const handleCloseCleanupView = useCallback(() => {
+ setCleanupView(undefined);
dispatch({ type: "showAll" });
}, []);
@@ -1929,7 +1945,7 @@ const Page: React.FC = () => {
message={
watchFolderView ? t("watch_folder_dropzone_hint") : undefined
}
- disabled={shouldDisableDropzone || !!slideshow || isLargeFilesOpen}
+ disabled={shouldDisableDropzone || !!slideshow || isCleanupViewOpen}
onDrop={setDragAndDropFiles}
>
{blockingLoad && <TranslucentLoadingOverlay />}
@@ -1965,7 +1981,7 @@ const Page: React.FC = () => {
{...trashFilesDialog}
/>
)}
- {!isLargeFilesOpen && (
+ {!isCleanupViewOpen && (
<>
<NavbarBase
sx={[
@@ -2115,6 +2131,7 @@ const Page: React.FC = () => {
{...sidebarVisibilityProps}
onClose={handleSidebarClose}
onShowLargeFiles={handleShowLargeFiles}
+ onShowDuplicates={handleShowDuplicates}
normalCollectionSummaries={normalCollectionSummaries}
uncategorizedCollectionSummaryID={
state.uncategorizedCollectionSummaryID
@@ -2136,9 +2153,14 @@ const Page: React.FC = () => {
title={t("add_a_person")}
onSelectPerson={handleContextMenuSelectPerson}
/>
- {isLargeFilesOpen ? (
+ {cleanupView === "duplicates" ? (
+ <DuplicatesView
+ onBack={handleCloseCleanupView}
+ onRemoteFilesPull={remoteFilesPull}
+ />
+ ) : cleanupView === "large-files" ? (
<LargeFilesView
- onBack={handleCloseLargeFiles}
+ onBack={handleCloseCleanupView}
onRemoteFilesPull={remoteFilesPull}
/>
) : !isInSearchMode &&
diff --git a/web/apps/photos/src/services/dedup.ts b/web/apps/photos/src/services/dedup.ts
index 8f0a5ffb96..2bc56ec9cd 100644
--- a/web/apps/photos/src/services/dedup.ts
+++ b/web/apps/photos/src/services/dedup.ts
@@ -10,7 +10,6 @@ import {
savedNormalCollections,
} from "ente-new/photos/services/collection";
import { savedCollectionFiles } from "ente-new/photos/services/photos-fdb";
-import { pullFiles } from "./pull";
export interface DuplicateGroup {
id: string;
@@ -112,6 +111,7 @@ export const deduceDuplicates = async () => {
export const removeSelectedDuplicateGroups = async (
duplicateGroups: DuplicateGroup[],
onProgress: (progress: number) => void,
+ onRemoteFilesPull: () => Promise<void>,
) => {
const selectedDuplicateGroups = duplicateGroups.filter((g) => g.isSelected);
@@ -156,7 +156,7 @@ export const removeSelectedDuplicateGroups = async (
tickProgress();
}
- await pullFiles();
+ await onRemoteFilesPull();
tickProgress();
return new Set(selectedDuplicateGroups.map((g) => g.id));
diff --git a/web/apps/photos/tests/dedup.test.ts b/web/apps/photos/tests/dedup.test.ts
new file mode 100644
index 0000000000..fac7c2e3c5
--- /dev/null
+++ b/web/apps/photos/tests/dedup.test.ts
@@ -0,0 +1,148 @@
+import type { EnteFile } from "ente-media/file";
+import { beforeEach, describe, expect, test, vi } from "vitest";
+import {
+ removeSelectedDuplicateGroups,
+ type DuplicateGroup,
+} from "../src/services/dedup";
+
+const { addToCollection, moveToTrash, savedNormalCollections } = vi.hoisted(
+ () => ({
+ addToCollection: vi.fn(),
+ moveToTrash: vi.fn(),
+ savedNormalCollections: vi.fn(),
+ }),
+);
+vi.mock("ente-new/photos/services/collection", () => ({
+ addToCollection,
+ moveToTrash,
+ savedNormalCollections,
+}));
+vi.mock("ente-new/photos/services/photos-fdb", () => ({}));
+vi.mock("ente-accounts/services/user", () => ({}));
+
+const duplicateGroup = (): DuplicateGroup => ({
+ id: "group-1",
+ items: [
+ {
+ file: { id: 1 } as EnteFile,
+ collectionIDs: new Set([11, 12]),
+ collectionName: "A",
+ },
+ {
+ file: {
+ id: 2,
+ pubMagicMetadata: { data: { caption: "Keep this copy" } },
+ } as EnteFile,
+ collectionIDs: new Set([12]),
+ collectionName: "B",
+ },
+ {
+ file: { id: 3 } as EnteFile,
+ collectionIDs: new Set([13]),
+ collectionName: "C",
+ },
+ ],
+ itemSize: 100,
+ prunableCount: 2,
+ prunableSize: 200,
+ isSelected: true,
+});
+
+const collections = [{ id: 11 }, { id: 12 }, { id: 13 }];
+
+describe("removeSelectedDuplicateGroups", () => {
+ beforeEach(() => {
+ vi.resetAllMocks();
+ savedNormalCollections.mockResolvedValue(collections);
+ });
+
+ test("preserves album membership before trashing selected duplicates and awaits gallery refresh", async () => {
+ const group = duplicateGroup();
+ const untouched = {
+ ...duplicateGroup(),
+ id: "unselected",
+ isSelected: false,
+ };
+ const progress = vi.fn();
+ let finishRefresh!: () => void;
+ const refresh = vi.fn(
+ () =>
+ new Promise<void>((resolve) => {
+ finishRefresh = resolve;
+ }),
+ );
+ const removal = removeSelectedDuplicateGroups(
+ [group, untouched],
+ progress,
+ refresh,
+ );
+ await vi.waitFor(() => expect(refresh).toHaveBeenCalledOnce());
+ expect(addToCollection.mock.calls).toEqual([
+ [collections[0], [group.items[1]!.file]],
+ [collections[2], [group.items[1]!.file]],
+ ]);
+ expect(moveToTrash).toHaveBeenCalledExactlyOnceWith([
+ group.items[0]!.file,
+ group.items[2]!.file,
+ ]);
+ expect(addToCollection.mock.invocationCallOrder[1]).toBeLessThan(
+ moveToTrash.mock.invocationCallOrder[0]!,
+ );
+ expect(moveToTrash.mock.invocationCallOrder[0]).toBeLessThan(
+ refresh.mock.invocationCallOrder[0]!,
+ );
+ expect(progress.mock.calls).toEqual([[25], [50], [75]]);
+ finishRefresh();
+ await expect(removal).resolves.toEqual(new Set([group.id]));
+ expect(progress).toHaveBeenLastCalledWith(100);
+ });
+
+ test("does not trash files when preserving album membership fails", async () => {
+ const failure = new Error("Add failed");
+ addToCollection.mockRejectedValue(failure);
+ const refresh = vi.fn();
+ await expect(
+ removeSelectedDuplicateGroups([duplicateGroup()], vi.fn(), refresh),
+ ).rejects.toBe(failure);
+ expect(moveToTrash).not.toHaveBeenCalled();
+ expect(refresh).not.toHaveBeenCalled();
+ });
+
+ test("does not refresh when trashing fails", async () => {
+ const failure = new Error("Trash failed");
+ moveToTrash.mockRejectedValue(failure);
+ const refresh = vi.fn();
+ await expect(
+ removeSelectedDuplicateGroups([duplicateGroup()], vi.fn(), refresh),
+ ).rejects.toBe(failure);
+ expect(refresh).not.toHaveBeenCalled();
+ });
+
+ test("does not report completion when gallery refresh fails", async () => {
+ const failure = new Error("Refresh failed");
+ const refresh = vi.fn().mockRejectedValue(failure);
+ const progress = vi.fn();
+ await expect(
+ removeSelectedDuplicateGroups(
+ [duplicateGroup()],
+ progress,
+ refresh,
+ ),
+ ).rejects.toBe(failure);
+ expect(progress).not.toHaveBeenCalledWith(100);
+ });
+
+ test("only refreshes when no groups are selected", async () => {
+ const refresh = vi.fn();
+ await expect(
+ removeSelectedDuplicateGroups(
+ [{ ...duplicateGroup(), isSelected: false }],
+ vi.fn(),
+ refresh,
+ ),
+ ).resolves.toEqual(new Set());
+ expect(addToCollection).not.toHaveBeenCalled();
+ expect(moveToTrash).not.toHaveBeenCalled();
+ expect(refresh).toHaveBeenCalledOnce();
+ });
+});