Skip to content

armgit / ente

Public Imported from GitHub
0

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

thread

Manav Rathi17ea9dfaaae9
diff --git a/web/apps/photos/src/components/PhotoFrame.tsx b/web/apps/photos/src/components/PhotoFrame.tsx
index 579a00e7bb..b66e4819cd 100644
--- a/web/apps/photos/src/components/PhotoFrame.tsx
+++ b/web/apps/photos/src/components/PhotoFrame.tsx
@@ -65,11 +65,24 @@ export interface PhotoFrameProps {
modePlus?: GalleryBarMode | "search";
files: EnteFile[];
syncWithRemote: () => Promise<void>;
- favItemIds?: Set<number>;
setSelected: (
selected: SelectedState | ((selected: SelectedState) => SelectedState),
) => void;
selected: SelectedState;
+ /**
+ * File IDs of all the files that the user has marked as a favorite.
+ *
+ * Not set in the context of the shared albums app.
+ */
+ favoriteFileIDs?: Set<number>;
+ /**
+ * Callback to invoke when the in-memory favorite status of a file is
+ * changed. For more details, see {@link unsyncedFavoriteUpdates} in the
+ * gallery reducer's documentation.
+ *
+ * Not set in the context of the shared albums app.
+ */
+ addUnsyncedFavoriteUpdate?: (fileID: number, isFavorite: boolean) => void;
markTempDeleted?: (tempDeletedFiles: EnteFile[]) => void;
/** This will be set if mode is not "people". */
activeCollectionID: number;
@@ -91,9 +104,10 @@ const PhotoFrame = ({
modePlus,
files,
syncWithRemote,
- favItemIds,
setSelected,
selected,
+ favoriteFileIDs,
+ addUnsyncedFavoriteUpdate,
markTempDeleted,
activeCollectionID,
activePersonID,
@@ -303,7 +317,7 @@ const PhotoFrame = ({
}
activeCollectionID={activeCollectionID}
showPlaceholder={isScrolling}
- isFav={favItemIds?.has(item.id)}
+ isFav={favoriteFileIDs?.has(item.id)}
/>
);
@@ -485,7 +499,6 @@ const PhotoFrame = ({
onClose={handleClose}
gettingData={getSlideData}
forceConvertItem={forceConvertItem}
- favItemIds={favItemIds}
markTempDeleted={markTempDeleted}
isTrashCollection={activeCollectionID === TRASH_SECTION}
isInHiddenSection={isInHiddenSection}
@@ -496,6 +509,7 @@ const PhotoFrame = ({
setFilesDownloadProgressAttributesCreator
}
onSelectPerson={onSelectPerson}
+ {...{ favoriteFileIDs, addUnsyncedFavoriteUpdate }}
/>
</Container>
);
diff --git a/web/apps/photos/src/components/PhotoViewer/index.tsx b/web/apps/photos/src/components/PhotoViewer/index.tsx
index 6b94315460..7a5033a143 100644
--- a/web/apps/photos/src/components/PhotoViewer/index.tsx
+++ b/web/apps/photos/src/components/PhotoViewer/index.tsx
@@ -42,7 +42,7 @@ import {
type ButtonProps,
type CircularProgressProps,
} from "@mui/material";
-import type { DisplayFile } from "components/PhotoFrame";
+import type { DisplayFile, PhotoFrameProps } from "components/PhotoFrame";
import { t } from "i18next";
import { GalleryContext } from "pages/gallery";
import Photoswipe from "photoswipe";
@@ -114,7 +114,10 @@ const CaptionContainer = styled("div")(({ theme }) => ({
backdropFilter: `blur(${theme.colors.blur.base})`,
}));
-export interface PhotoViewerProps {
+export type PhotoViewerProps = Pick<
+ PhotoFrameProps,
+ "favoriteFileIDs" | "addUnsyncedFavoriteUpdate"
+> & {
isOpen: boolean;
items: any[];
currentIndex?: number;
@@ -122,7 +125,6 @@ export interface PhotoViewerProps {
gettingData: (instance: any, index: number, item: EnteFile) => void;
forceConvertItem: (instance: any, index: number, item: EnteFile) => void;
id?: string;
- favItemIds: Set<number>;
markTempDeleted?: (tempDeletedFiles: EnteFile[]) => void;
isTrashCollection: boolean;
isInHiddenSection: boolean;
@@ -131,10 +133,11 @@ export interface PhotoViewerProps {
collectionNameMap: Map<number, string>;
setFilesDownloadProgressAttributesCreator: SetFilesDownloadProgressAttributesCreator;
onSelectPerson?: FileInfoProps["onSelectPerson"];
-}
+};
function PhotoViewer(props: PhotoViewerProps) {
- const { id, forceConvertItem } = props;
+ const { id, forceConvertItem, favoriteFileIDs, addUnsyncedFavoriteUpdate } =
+ props;
const galleryContext = useContext(GalleryContext);
const { showLoadingBar, hideLoadingBar, showMiniDialog } =
@@ -148,7 +151,6 @@ function PhotoViewer(props: PhotoViewerProps) {
const pswpElement = useRef<HTMLDivElement>();
const [photoSwipe, setPhotoSwipe] =
useState<Photoswipe<Photoswipe.Options>>();
- const [isFav, setIsFav] = useState(false);
const [showInfo, setShowInfo] = useState(false);
const [exif, setExif] = useState<
| {
@@ -339,10 +341,6 @@ function PhotoViewer(props: PhotoViewerProps) {
exifCopy.current = exif;
}, [exif]);
- function updateFavButton(file: EnteFile) {
- setIsFav(isInFav(file));
- }
-
function updateIsOwnFile(file: EnteFile) {
const isOwnFile =
!publicCollectionGalleryContext.credentials &&
@@ -474,7 +472,6 @@ function PhotoViewer(props: PhotoViewerProps) {
for (const videoTag of videoTags) {
videoTag.pause();
}
- updateFavButton(currItem);
updateIsOwnFile(currItem);
updateConversionFailedNotification(currItem);
updateExif(currItem);
@@ -512,13 +509,6 @@ function PhotoViewer(props: PhotoViewerProps) {
}
handleCloseInfo();
};
- const isInFav = (file: DisplayFile) => {
- const { favItemIds } = props;
- if (favItemIds && file) {
- return favItemIds.has(file.id);
- }
- return false;
- };
const onFavClick = async (file: DisplayFile) => {
try {
@@ -530,15 +520,20 @@ function PhotoViewer(props: PhotoViewerProps) {
) {
return;
}
- const { favItemIds } = props;
- if (!isInFav(file)) {
- favItemIds.add(file.id);
- addToFavorites(file);
- setIsFav(true);
+ // Whe get here when we're showing the favorites scaffolding, and so
+ // we can assert the presence of the favoriteFileIDs.
+ if (favoriteFileIDs!.has(file.id)) {
+ addUnsyncedFavoriteUpdate(file.id, true);
+ void addToFavorites(file).catch((e: unknown) => {
+ log.error("Failed to add favorite", e);
+ addUnsyncedFavoriteUpdate(file.id, undefined);
+ });
} else {
- favItemIds.delete(file.id);
- removeFromFavorites(file);
- setIsFav(false);
+ addUnsyncedFavoriteUpdate(file.id, false);
+ void removeFromFavorites(file).catch((e: unknown) => {
+ log.error("Failed to remove favorite", e);
+ addUnsyncedFavoriteUpdate(file.id, undefined);
+ });
}
needUpdate.current = true;
} catch (e) {
@@ -761,6 +756,11 @@ function PhotoViewer(props: PhotoViewerProps) {
);
const scheduleUpdate = () => (needUpdate.current = true);
+
+ const isFav =
+ photoSwipe.currItem &&
+ !!favoriteFileIDs?.has((photoSwipe.currItem as EnteFile).id);
+
return (
<>
<div
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 4c2550a32a..5c21b338d1 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -1033,9 +1033,16 @@ export default function Gallery() {
modePlus={isInSearchMode ? "search" : barMode}
files={filteredFiles}
syncWithRemote={syncWithRemote}
- favItemIds={state.favoriteFileIDs}
setSelected={setSelected}
selected={selected}
+ favoriteFileIDs={state.favoriteFileIDs}
+ addUnsyncedFavoriteUpdate={(fileID, isFavorite) =>
+ dispatch({
+ type: "addUnsyncedFavoriteUpdate",
+ fileID,
+ isFavorite,
+ })
+ }
markTempDeleted={(files) =>
dispatch({ type: "markTempDeleted", files })
}