Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move

Manav Rathic3831230e00b
diff --git a/web/apps/photos/src/components/FileList.tsx b/web/apps/photos/src/components/FileList.tsx
index a15fcfc6d1..2db2dd196f 100644
--- a/web/apps/photos/src/components/FileList.tsx
+++ b/web/apps/photos/src/components/FileList.tsx
@@ -20,8 +20,12 @@ import {
ListChildComponentProps,
areEqual,
} from "react-window";
-import { handleSelectCreatorMulti } from "utils/photoFrame";
+import {
+ handleSelectCreator,
+ handleSelectCreatorMulti,
+} from "utils/photoFrame";
import { PublicCollectionGalleryContext } from "utils/publicCollectionGallery";
+import PreviewCard from "./pages/gallery/PreviewCard";
export const DATE_CONTAINER_HEIGHT = 48;
export const SPACE_BTW_DATES = 44;
@@ -188,18 +192,28 @@ export interface FileListAnnotatedFile {
timelineDateString: string;
}
-type FileListProps = Pick<PhotoFrameProps, "mode" | "modePlus"> & {
+type FileListProps = Pick<
+ PhotoFrameProps,
+ | "mode"
+ | "modePlus"
+ | "selectable"
+ | "selected"
+ | "setSelected"
+ | "favoriteFileIDs"
+> & {
height: number;
width: number;
annotatedFiles: FileListAnnotatedFile[];
showAppDownloadBanner: boolean;
- getThumbnail: (
- file: FileListAnnotatedFile,
- index: number,
- isScrolling?: boolean,
- ) => React.JSX.Element;
activeCollectionID: number;
activePersonID?: string;
+ /**
+ * Called when the user activates the thumbnail at the given {@link index}.
+ *
+ * This corresponding file would be at the corresponding index of
+ * {@link annotatedFiles}.
+ */
+ onItemClick: (index: number) => void;
};
interface ItemData {
@@ -223,6 +237,7 @@ const createItemData = memoize(
) => React.JSX.Element,
): ItemData => ({ timeStampList, columns, shrinkRatio, renderListItem }),
);
+
const PhotoListRow = React.memo(
({
index,
@@ -255,9 +270,13 @@ export const FileList: React.FC<FileListProps> = ({
modePlus,
annotatedFiles,
showAppDownloadBanner,
- getThumbnail,
+ selectable,
+ selected,
+ setSelected,
activeCollectionID,
activePersonID,
+ favoriteFileIDs,
+ onItemClick,
}) => {
const galleryContext = useContext(GalleryContext);
const publicCollectionGalleryContext = useContext(
@@ -275,6 +294,10 @@ export const FileList: React.FC<FileListProps> = ({
const [checkedTimelineDateStrings, setCheckedTimelineDateStrings] =
useState(new Set());
+ const [rangeStart, setRangeStart] = useState(null);
+ const [currentHover, setCurrentHover] = useState(null);
+ const [isShiftKeyPressed, setIsShiftKeyPressed] = useState(false);
+
const fittableColumns = getFractionFittableColumns(width);
let columns = Math.floor(fittableColumns);
@@ -767,7 +790,7 @@ export const FileList: React.FC<FileListProps> = ({
});
}, [galleryContext.selectedFile]);
- const handleSelect = handleSelectCreatorMulti(
+ const handleSelectMulti = handleSelectCreatorMulti(
galleryContext.setSelectedFiles,
mode,
galleryContext?.user?.id,
@@ -791,9 +814,107 @@ export const FileList: React.FC<FileListProps> = ({
(item) => item.timelineDateString === date,
); // all files on a checked/unchecked day
- handleSelect(filesOnADay.map((af) => af.file))(isDateSelected);
+ handleSelectMulti(filesOnADay.map((af) => af.file))(isDateSelected);
+ };
+
+ const handleSelect = handleSelectCreator(
+ setSelected,
+ mode,
+ galleryContext.user?.id,
+ activeCollectionID,
+ activePersonID,
+ setRangeStart,
+ );
+
+ const onHoverOver = (index: number) => () => {
+ setCurrentHover(index);
+ };
+
+ const handleRangeSelect = (index: number) => () => {
+ if (typeof rangeStart !== "undefined" && rangeStart !== index) {
+ const direction =
+ (index - rangeStart) / Math.abs(index - rangeStart);
+ let checked = true;
+ for (
+ let i = rangeStart;
+ (index - i) * direction >= 0;
+ i += direction
+ ) {
+ checked = checked && !!selected[annotatedFiles[i].file.id];
+ }
+ for (
+ let i = rangeStart;
+ (index - i) * direction > 0;
+ i += direction
+ ) {
+ handleSelect(annotatedFiles[i].file)(!checked);
+ }
+ handleSelect(annotatedFiles[index].file, index)(!checked);
+ }
};
+ useEffect(() => {
+ const handleKeyDown = (e: KeyboardEvent) => {
+ if (e.key === "Shift") {
+ setIsShiftKeyPressed(true);
+ }
+ };
+
+ const handleKeyUp = (e: KeyboardEvent) => {
+ if (e.key === "Shift") {
+ setIsShiftKeyPressed(false);
+ }
+ };
+
+ document.addEventListener("keydown", handleKeyDown);
+ document.addEventListener("keyup", handleKeyUp);
+
+ return () => {
+ document.removeEventListener("keydown", handleKeyDown);
+ document.removeEventListener("keyup", handleKeyUp);
+ };
+ }, []);
+
+ useEffect(() => {
+ if (selected.count === 0) {
+ setRangeStart(null);
+ }
+ }, [selected]);
+
+ const getThumbnail = (
+ { file }: FileListAnnotatedFile,
+ index: number,
+ isScrolling: boolean,
+ ) => (
+ <PreviewCard
+ key={`tile-${file.id}-selected-${selected[file.id] ?? false}`}
+ file={file}
+ onClick={() => onItemClick(index)}
+ selectable={selectable}
+ onSelect={handleSelect(file, index)}
+ selected={
+ (!mode
+ ? selected.collectionID === activeCollectionID
+ : mode == selected.context?.mode &&
+ (selected.context.mode == "people"
+ ? selected.context.personID == activePersonID
+ : selected.context.collectionID ==
+ activeCollectionID)) && selected[file.id]
+ }
+ selectOnClick={selected.count > 0}
+ onHover={onHoverOver(index)}
+ onRangeSelect={handleRangeSelect(index)}
+ isRangeSelectActive={isShiftKeyPressed && selected.count > 0}
+ isInsSelectRange={
+ (index >= rangeStart && index <= currentHover) ||
+ (index >= currentHover && index <= rangeStart)
+ }
+ activeCollectionID={activeCollectionID}
+ showPlaceholder={isScrolling}
+ isFav={favoriteFileIDs?.has(file.id)}
+ />
+ );
+
const renderListItem = (
listItem: TimeStampListItem,
isScrolling: boolean,
diff --git a/web/apps/photos/src/components/PhotoFrame.tsx b/web/apps/photos/src/components/PhotoFrame.tsx
index 824ef0eb43..806a67ebab 100644
--- a/web/apps/photos/src/components/PhotoFrame.tsx
+++ b/web/apps/photos/src/components/PhotoFrame.tsx
@@ -10,7 +10,7 @@ import { moveToTrash, TRASH_SECTION } from "@/new/photos/services/collection";
import { styled } from "@mui/material";
import { t } from "i18next";
import { GalleryContext } from "pages/gallery";
-import { useCallback, useContext, useEffect, useMemo, useState } from "react";
+import { useCallback, useContext, useMemo, useState } from "react";
import AutoSizer from "react-virtualized-auto-sizer";
import {
addToFavorites,
@@ -22,9 +22,7 @@ import {
SetFilesDownloadProgressAttributesCreator,
} from "types/gallery";
import { downloadSingleFile } from "utils/file";
-import { handleSelectCreator } from "utils/photoFrame";
import { FileList, type FileListAnnotatedFile } from "./FileList";
-import PreviewCard from "./pages/gallery/PreviewCard";
const Container = styled("div")`
display: block;
@@ -93,6 +91,7 @@ export type PhotoFrameProps = Pick<
*/
modePlus?: GalleryBarMode | "search";
files: EnteFile[];
+ selectable?: boolean;
setSelected: (
selected: SelectedState | ((selected: SelectedState) => SelectedState),
) => void;
@@ -135,7 +134,6 @@ export type PhotoFrameProps = Pick<
isInIncomingSharedCollection?: boolean;
isInHiddenSection?: boolean;
setFilesDownloadProgressAttributesCreator?: SetFilesDownloadProgressAttributesCreator;
- selectable?: boolean;
/**
* Called when the visibility of the file viewer dialog changes.
*/
@@ -150,8 +148,9 @@ const PhotoFrame = ({
mode,
modePlus,
files,
- setSelected,
+ selectable,
selected,
+ setSelected,
favoriteFileIDs,
onMarkUnsyncedFavoriteUpdate,
onMarkTempDeleted,
@@ -164,7 +163,6 @@ const PhotoFrame = ({
isInIncomingSharedCollection,
isInHiddenSection,
setFilesDownloadProgressAttributesCreator,
- selectable,
onSetOpenFileViewer,
onSyncWithRemote,
onSelectCollection,
@@ -174,9 +172,6 @@ const PhotoFrame = ({
const [openFileViewer, setOpenFileViewer] = useState(false);
const [currentIndex, setCurrentIndex] = useState(0);
- const [rangeStart, setRangeStart] = useState(null);
- const [currentHover, setCurrentHover] = useState(null);
- const [isShiftKeyPressed, setIsShiftKeyPressed] = useState(false);
const annotatedFiles = useMemo(
(): FileListAnnotatedFile[] =>
@@ -245,118 +240,24 @@ const PhotoFrame = ({
[],
);
- useEffect(() => {
- const handleKeyDown = (e: KeyboardEvent) => {
- if (e.key === "Shift") {
- setIsShiftKeyPressed(true);
- }
- };
-
- const handleKeyUp = (e: KeyboardEvent) => {
- if (e.key === "Shift") {
- setIsShiftKeyPressed(false);
- }
- };
-
- document.addEventListener("keydown", handleKeyDown);
- document.addEventListener("keyup", handleKeyUp);
-
- return () => {
- document.removeEventListener("keydown", handleKeyDown);
- document.removeEventListener("keyup", handleKeyUp);
- };
- }, []);
-
- useEffect(() => {
- if (selected.count === 0) {
- setRangeStart(null);
- }
- }, [selected]);
-
- const handleSelect = handleSelectCreator(
- setSelected,
- mode,
- galleryContext.user?.id,
- activeCollectionID,
- activePersonID,
- setRangeStart,
- );
-
- const onHoverOver = (index: number) => () => {
- setCurrentHover(index);
- };
-
- const handleRangeSelect = (index: number) => () => {
- if (typeof rangeStart !== "undefined" && rangeStart !== index) {
- const direction =
- (index - rangeStart) / Math.abs(index - rangeStart);
- let checked = true;
- for (
- let i = rangeStart;
- (index - i) * direction >= 0;
- i += direction
- ) {
- checked = checked && !!selected[annotatedFiles[i].file.id];
- }
- for (
- let i = rangeStart;
- (index - i) * direction > 0;
- i += direction
- ) {
- handleSelect(annotatedFiles[i].file)(!checked);
- }
- handleSelect(annotatedFiles[index].file, index)(!checked);
- }
- };
-
- const getThumbnail = (
- { file }: FileListAnnotatedFile,
- index: number,
- isScrolling: boolean,
- ) => (
- <PreviewCard
- key={`tile-${file.id}-selected-${selected[file.id] ?? false}`}
- file={file}
- onClick={() => handleThumbnailClick(index)}
- selectable={selectable}
- onSelect={handleSelect(file, index)}
- selected={
- (!mode
- ? selected.collectionID === activeCollectionID
- : mode == selected.context?.mode &&
- (selected.context.mode == "people"
- ? selected.context.personID == activePersonID
- : selected.context.collectionID ==
- activeCollectionID)) && selected[file.id]
- }
- selectOnClick={selected.count > 0}
- onHover={onHoverOver(index)}
- onRangeSelect={handleRangeSelect(index)}
- isRangeSelectActive={isShiftKeyPressed && selected.count > 0}
- isInsSelectRange={
- (index >= rangeStart && index <= currentHover) ||
- (index >= currentHover && index <= rangeStart)
- }
- activeCollectionID={activeCollectionID}
- showPlaceholder={isScrolling}
- isFav={favoriteFileIDs?.has(file.id)}
- />
- );
-
return (
<Container>
<AutoSizer>
{({ height, width }) => (
<FileList
- width={width}
- height={height}
- getThumbnail={getThumbnail}
- mode={mode}
- modePlus={modePlus}
- annotatedFiles={annotatedFiles}
- activeCollectionID={activeCollectionID}
- activePersonID={activePersonID}
- showAppDownloadBanner={showAppDownloadBanner}
+ {...{
+ mode,
+ modePlus,
+ selectable,
+ selected,
+ setSelected,
+ activeCollectionID,
+ activePersonID,
+ showAppDownloadBanner,
+ favoriteFileIDs,
+ }}
+ {...{ width, height, annotatedFiles }}
+ onItemClick={handleThumbnailClick}
/>
)}
</AutoSizer>