From bb161ad3a9782d6b565d0753aaf0f22239967853 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Thu, 2 Jan 2025 12:42:01 +0530 Subject: [PATCH] Rew --- .../src/components/PhotoViewer/index.tsx | 65 +++++++++---------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/web/apps/photos/src/components/PhotoViewer/index.tsx b/web/apps/photos/src/components/PhotoViewer/index.tsx index 1d41adbb00..4d070c6b4b 100644 --- a/web/apps/photos/src/components/PhotoViewer/index.tsx +++ b/web/apps/photos/src/components/PhotoViewer/index.tsx @@ -1,4 +1,5 @@ import { isDesktop } from "@/base/app"; +import { assertionFailed } from "@/base/assert"; import { FilledIconButton, type ButtonishProps } from "@/base/components/mui"; import { ActivityIndicator } from "@/base/components/mui/ActivityIndicator"; import { Overlay } from "@/base/components/mui/Container"; @@ -514,35 +515,37 @@ function PhotoViewer(props: PhotoViewerProps) { handleCloseInfo(); }; - const onFavClick = async (file: DisplayFile) => { - try { - if ( - !file || - props.isTrashCollection || - !isOwnFile || - props.isInHiddenSection - ) { - return; - } - // 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)) { - markUnsyncedFavoriteUpdate(file.id, true); - void addToFavorites(file).catch((e: unknown) => { - log.error("Failed to add favorite", e); - markUnsyncedFavoriteUpdate(file.id, undefined); - }); - } else { - markUnsyncedFavoriteUpdate(file.id, false); - void removeFromFavorites(file).catch((e: unknown) => { - log.error("Failed to remove favorite", e); - markUnsyncedFavoriteUpdate(file.id, undefined); - }); - } - needUpdate.current = true; - } catch (e) { - log.error("onFavClick failed", e); + const handleFavoriteClick = async () => { + const file = photoSwipe?.currItem as EnteFile; + if ( + !file || + props.isTrashCollection || + !isOwnFile || + props.isInHiddenSection + ) { + assertionFailed(); + return; } + + // Whe get here when we're showing the favorites scaffolding, and so + // we can assert the presence of the favoriteFileIDs. + const isFavorite = favoriteFileIDs!.has(file.id); + + if (!isFavorite) { + markUnsyncedFavoriteUpdate(file.id, true); + void addToFavorites(file).catch((e: unknown) => { + log.error("Failed to add favorite", e); + markUnsyncedFavoriteUpdate(file.id, undefined); + }); + } else { + markUnsyncedFavoriteUpdate(file.id, false); + void removeFromFavorites(file).catch((e: unknown) => { + log.error("Failed to remove favorite", e); + markUnsyncedFavoriteUpdate(file.id, undefined); + }); + } + + needUpdate.current = true; }; const trashFile = async (file: DisplayFile) => { @@ -933,11 +936,7 @@ function PhotoViewer(props: PhotoViewerProps) { : t("favorite_key") } className="pswp__button pswp__button--custom" - onClick={() => { - onFavClick( - photoSwipe?.currItem as EnteFile, - ); - }} + onClick={handleFavoriteClick} > {isFav ? (