Skip to content

armgit / ente

Public Imported from GitHub
0

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

Simplify public album selection helpers

Anand Baburajanfe2e39a95010
diff --git a/web/apps/albums/src/pages/index.tsx b/web/apps/albums/src/pages/index.tsx
index e3b1368169..0e85a6fce3 100644
--- a/web/apps/albums/src/pages/index.tsx
+++ b/web/apps/albums/src/pages/index.tsx
@@ -130,10 +130,7 @@ export default function PublicCollectionGallery() {
[],
);
const [selected, setSelected] = useState<SelectedState>({
- ownCount: 0,
count: 0,
- collectionID: 0,
- context: undefined,
});
// TODO: Can we convert these to state
@@ -439,10 +436,7 @@ export default function PublicCollectionGallery() {
return;
}
setSelected({
- ownCount: 0,
count: 0,
- collectionID: 0,
- context: undefined,
});
};
diff --git a/web/apps/albums/vendor/photos/components/FileList.tsx b/web/apps/albums/vendor/photos/components/FileList.tsx
index aa47d20b96..3bfc39ae90 100644
--- a/web/apps/albums/vendor/photos/components/FileList.tsx
+++ b/web/apps/albums/vendor/photos/components/FileList.tsx
@@ -193,7 +193,7 @@ export interface FileListProps {
selected: SelectedState | ((selected: SelectedState) => SelectedState),
) => void;
selected: SelectedState;
- /** The collection context used by the selection helpers. */
+ /** A stable key used to reset the virtualized list when the file set changes. */
activeCollectionID: number;
/**
* Called when the user activates the thumbnail at the given {@link index}.
@@ -424,15 +424,8 @@ export const FileList: React.FC<FileListProps> = ({
}, [annotatedFiles, selected]);
const handleSelectMulti = useMemo(
- () =>
- handleSelectCreatorMulti(
- setSelected,
- undefined,
- undefined,
- activeCollectionID,
- undefined,
- ),
- [setSelected, activeCollectionID],
+ () => handleSelectCreatorMulti(setSelected),
+ [setSelected],
);
const onChangeSelectAllCheckBox = useCallback(
@@ -459,25 +452,13 @@ export const FileList: React.FC<FileListProps> = ({
);
const handleSelect = useMemo(
- () =>
- handleSelectCreator(
- setSelected,
- undefined,
- undefined,
- activeCollectionID,
- undefined,
- setRangeStartIndex,
- ),
- [setSelected, activeCollectionID],
+ () => handleSelectCreator(setSelected, setRangeStartIndex),
+ [setSelected],
);
- const isSelectionContextMatching = useMemo(() => {
- return selected.collectionID === activeCollectionID;
- }, [activeCollectionID, selected.collectionID]);
-
const isFileSelected = useCallback(
- (file: EnteFile) => isSelectionContextMatching && !!selected[file.id],
- [isSelectionContextMatching, selected],
+ (file: EnteFile) => !!selected[file.id],
+ [selected],
);
const haveSelection = selected.count > 0;
diff --git a/web/apps/albums/vendor/photos/utils/file/index.ts b/web/apps/albums/vendor/photos/utils/file/index.ts
index b1821fd676..01449aaec5 100644
--- a/web/apps/albums/vendor/photos/utils/file/index.ts
+++ b/web/apps/albums/vendor/photos/utils/file/index.ts
@@ -1,31 +1,8 @@
-import type { AddSaveGroup } from "ente-gallery/components/utils/save-groups";
-import { downloadAndSaveFiles } from "ente-gallery/services/save";
import type { EnteFile } from "ente-media/file";
-import { fileFileName, ItemVisibility } from "ente-media/file-metadata";
-import { FileType } from "ente-media/file-type";
-import { type SelectionContext } from "ente-new/photos/components/gallery";
-import { type FileOp } from "ente-new/photos/components/SelectedFileOptions";
-import {
- addToFavoritesCollection,
- deleteFromTrash,
- hideFiles,
- moveToTrash,
- removeFromFavoritesCollection,
-} from "ente-new/photos/services/collection";
-import { updateFilesVisibility } from "ente-new/photos/services/file";
-import { t } from "i18next";
export interface SelectedState {
[k: number]: boolean;
- ownCount: number;
count: number;
- collectionID: number | undefined;
- /**
- * The context in which the selection was made. Only set by newer code if
- * there is an active selection (older code continues to rely on the
- * {@link collectionID} logic).
- */
- context: SelectionContext | undefined;
}
export type SetSelectedState = React.Dispatch<
React.SetStateAction<SelectedState>
@@ -44,68 +21,3 @@ export function getSelectedFiles(
return files.filter((file) => selectedFilesIDs.has(file.id));
}
-
-export const performFileOp = async (
- op: FileOp,
- files: EnteFile[],
- onAddSaveGroup: AddSaveGroup,
- markTempDeleted: (files: EnteFile[]) => void,
- clearTempDeleted: () => void,
- markTempHidden: (files: EnteFile[]) => void,
- clearTempHidden: () => void,
- fixCreationTime: (files: EnteFile[]) => void,
-) => {
- switch (op) {
- case "download": {
- const singleFile = files.length === 1 ? files[0] : undefined;
- const title =
- singleFile?.metadata.fileType === FileType.livePhoto
- ? fileFileName(singleFile)
- : t("files_count", { count: files.length });
- await downloadAndSaveFiles(files, title, onAddSaveGroup);
- break;
- }
- case "fixTime":
- fixCreationTime(files);
- break;
- case "favorite":
- await addToFavoritesCollection(files);
- break;
- case "unfavorite":
- await removeFromFavoritesCollection(files);
- break;
- case "archive":
- await updateFilesVisibility(files, ItemVisibility.archived);
- break;
- case "unarchive":
- await updateFilesVisibility(files, ItemVisibility.visible);
- break;
- case "hide":
- try {
- markTempHidden(files);
- await hideFiles(files);
- } catch (e) {
- clearTempHidden();
- throw e;
- }
- break;
- case "trash":
- try {
- markTempDeleted(files);
- await moveToTrash(files);
- } catch (e) {
- clearTempDeleted();
- throw e;
- }
- break;
- case "deletePermanently":
- try {
- markTempDeleted(files);
- await deleteFromTrash(files.map((file) => file.id));
- } catch (e) {
- clearTempDeleted();
- throw e;
- }
- break;
- }
-};
diff --git a/web/apps/albums/vendor/photos/utils/photoFrame/index.ts b/web/apps/albums/vendor/photos/utils/photoFrame/index.ts
index 672c85d2b7..7273b083be 100644
--- a/web/apps/albums/vendor/photos/utils/photoFrame/index.ts
+++ b/web/apps/albums/vendor/photos/utils/photoFrame/index.ts
@@ -1,19 +1,12 @@
// TODO: Audit this file
-import type { SelectionContext } from "ente-new/photos/components/gallery";
-import type { GalleryBarMode } from "ente-new/photos/components/gallery/reducer";
import type { SelectedState, SetSelectedState } from "utils/file";
-// TODO: All this is unnecessarily complex, and needs reworking.
export const handleSelectCreator =
(
setSelected: SetSelectedState,
- mode: GalleryBarMode | undefined,
- userID: number | undefined,
- activeCollectionID: number,
- activePersonID: string | undefined,
setRangeStartIndex: (index: number | undefined) => void,
) =>
- ({ id, ownerID }: { id: number; ownerID: number }, index?: number) =>
+ ({ id }: { id: number }, index?: number) =>
(checked: boolean) => {
if (typeof index != "undefined") {
if (checked) {
@@ -22,73 +15,28 @@ export const handleSelectCreator =
setRangeStartIndex(undefined);
}
}
- setSelected((_selected) => {
- const { selected, newContext } = createSelectedAndContext(
- mode,
- activeCollectionID,
- activePersonID,
- _selected,
- );
-
- const handleCounterChange = (count: number) => {
- if (selected[id] === checked) {
- return count;
- }
- if (checked) {
- return count + 1;
- } else {
- return count - 1;
- }
- };
-
- const handleAllCounterChange = () => {
- if (ownerID === userID) {
- return {
- ownCount: handleCounterChange(selected.ownCount),
- count: handleCounterChange(selected.count),
- };
- } else {
- return { count: handleCounterChange(selected.count) };
- }
- };
+ setSelected((selected) => {
return {
...selected,
[id]: checked,
- collectionID: activeCollectionID,
- context: newContext,
- ...handleAllCounterChange(),
+ count: nextSelectedCount(selected, id, checked),
};
});
};
export const handleSelectCreatorMulti =
- (
- setSelected: SetSelectedState,
- mode: GalleryBarMode | undefined,
- userID: number | undefined,
- activeCollectionID: number,
- activePersonID: string | undefined,
- ) =>
- (files: { id: number; ownerID: number }[]) =>
+ (setSelected: SetSelectedState) =>
+ (files: { id: number }[]) =>
(checked: boolean) => {
- setSelected((_selected) => {
- const { selected, newContext } = createSelectedAndContext(
- mode,
- activeCollectionID,
- activePersonID,
- _selected,
- );
-
+ setSelected((selected) => {
const newSelected = { ...selected };
let newCount = selected.count;
- let newOwnCount = selected.ownCount;
if (checked) {
for (const file of files) {
if (!newSelected[file.id]) {
newSelected[file.id] = true;
newCount++;
- if (file.ownerID === userID) newOwnCount++;
}
}
} else {
@@ -96,7 +44,6 @@ export const handleSelectCreatorMulti =
if (newSelected[file.id]) {
newSelected[file.id] = false;
newCount--;
- if (file.ownerID === userID) newOwnCount--;
}
}
}
@@ -104,89 +51,20 @@ export const handleSelectCreatorMulti =
return {
...newSelected,
count: newCount,
- ownCount: newOwnCount,
- collectionID: activeCollectionID,
- context: newContext,
};
});
};
-const createSelectedAndContext = (
- mode: GalleryBarMode | undefined,
-
- activeCollectionID: number,
- activePersonID: string | undefined,
+const nextSelectedCount = (
selected: SelectedState,
+ id: number,
+ checked: boolean,
) => {
- if (!mode) {
- // Retain older behavior for non-gallery call sites.
- if (selected.collectionID !== activeCollectionID) {
- selected = {
- ownCount: 0,
- count: 0,
- collectionID: 0,
- context: undefined,
- };
- }
- } else if (!selected.context) {
- // Gallery will specify a mode, but a fresh selection starts off
- // without a context, so fill it in with the current context.
- selected = {
- ...selected,
- context:
- mode == "people"
- ? { mode, personID: activePersonID! }
- : { mode, collectionID: activeCollectionID },
- };
- } else {
- // Both mode and context are defined.
- if (selected.context.mode != mode) {
- // Clear selection if mode has changed.
- selected = {
- ownCount: 0,
- count: 0,
- collectionID: 0,
- context:
- mode == "people"
- ? { mode, personID: activePersonID! }
- : { mode, collectionID: activeCollectionID },
- };
- } else {
- if (selected.context.mode == "people") {
- if (selected.context.personID != activePersonID) {
- // Clear selection if person has changed.
- selected = {
- ownCount: 0,
- count: 0,
- collectionID: 0,
- context: {
- mode: selected.context.mode,
- personID: activePersonID!,
- },
- };
- }
- } else {
- if (selected.context.collectionID != activeCollectionID) {
- // Clear selection if collection has changed.
- selected = {
- ownCount: 0,
- count: 0,
- collectionID: 0,
- context: {
- mode: selected.context.mode,
- collectionID: activeCollectionID,
- },
- };
- }
- }
- }
+ if (selected[id] === checked) {
+ return selected.count;
}
-
- const newContext: SelectionContext | undefined = !mode
- ? undefined
- : mode == "people"
- ? { mode, personID: activePersonID! }
- : { mode, collectionID: activeCollectionID };
-
- return { selected, newContext };
+ if (checked) {
+ return selected.count + 1;
+ }
+ return selected.count - 1;
};