Skip to content

armgit / ente

Public Imported from GitHub
0

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

Also for favs

Manav Rathi60d3cad540c2
diff --git a/web/apps/photos/src/components/PhotoFrame.tsx b/web/apps/photos/src/components/PhotoFrame.tsx
index b814fa9d01..2a90699152 100644
--- a/web/apps/photos/src/components/PhotoFrame.tsx
+++ b/web/apps/photos/src/components/PhotoFrame.tsx
@@ -278,6 +278,7 @@ const PhotoFrame = ({
file,
true,
);
+ // See: [Note: File viewer update and dispatch]
onMarkUnsyncedFavoriteUpdate(file.id, !isFavorite);
}
: undefined;
@@ -287,6 +288,7 @@ const PhotoFrame = ({
return onMarkTempDeleted
? async (file: EnteFile) => {
await moveToTrash([file]);
+ // See: [Note: File viewer update and dispatch]
onMarkTempDeleted?.([file]);
}
: undefined;
diff --git a/web/packages/gallery/components/viewer/FileViewer.tsx b/web/packages/gallery/components/viewer/FileViewer.tsx
index b85980f2f1..481c899e6f 100644
--- a/web/packages/gallery/components/viewer/FileViewer.tsx
+++ b/web/packages/gallery/components/viewer/FileViewer.tsx
@@ -128,6 +128,8 @@ export type FileViewerProps = ModalVisibilityProps & {
* in the file actions.
*
* See also {@link favoriteFileIDs}.
+ *
+ * See also: [Note: File viewer update and dispatch]
*/
onToggleFavorite?: (file: EnteFile) => Promise<void>;
/**
@@ -135,6 +137,8 @@ export type FileViewerProps = ModalVisibilityProps & {
*
* If this is not provided then the delete button will not be shown
* in the file actions.
+ *
+ * See also: [Note: File viewer update and dispatch]
*/
onDelete?: (file: EnteFile) => Promise<void>;
/**
@@ -226,8 +230,13 @@ const FileViewer: React.FC<FileViewerProps> = ({
useModalVisibility();
// Callbacks to be invoked (only once) the next time we get an update to the
- // `files` prop.
- const [, setOnNextFilesUpdate] = useState<(() => void)[]>([]);
+ // `files` or `favoriteFileIDs` props.
+ //
+ // Both of those trace their way back to the same reducer, so they get
+ // updated in tandem. When we delete files, only the `files` prop gets
+ // updated, while when we toggle favoriets, only the `favoriteFileIDs` prop
+ // gets updated.
+ const [, setOnNextFilesOrFavoritesUpdate] = useState<(() => void)[]>([]);
// If `true`, then we need to trigger a sync with remote when we close.
const [, setNeedsSync] = useState(false);
@@ -236,8 +245,8 @@ const FileViewer: React.FC<FileViewerProps> = ({
* Add a callback to be fired (only once) the next time we get an update to
* the `files` prop.
*/
- const awaitNextFilesUpdate = useCallback((cb: () => void) => {
- setOnNextFilesUpdate((cbs) => cbs.concat(cb));
+ const awaitNextFilesOrFavoritesUpdate = useCallback((cb: () => void) => {
+ setOnNextFilesOrFavoritesUpdate((cbs) => cbs.concat(cb));
}, []);
const handleClose = useCallback(() => {
@@ -300,10 +309,14 @@ const FileViewer: React.FC<FileViewerProps> = ({
const handleDelete = async () => {
const file = activeAnnotatedFile!.file;
await onDelete(file);
- // Be careful here. This works currently, deterministically, but it
- // relies on the assumption that `onDelete` will asynchronously result
- // in updates to the `files` prop.
- awaitNextFilesUpdate(() => {
+ // [Note: File viewer update and dispatch]
+ //
+ // This relies on the assumption that `onDelete` will asynchronously
+ // result in updates to the `files` prop. Currently that indeed is what
+ // happens as the last call in the `onDelete` implementation is a call
+ // to a dispatcher, but we need to be careful about preserving this
+ // assumption when changing `onDelete` implementation in the future.
+ awaitNextFilesOrFavoritesUpdate(() => {
// Refreshing the current slide after the current file has gone will
// show the subsequent slide (since that will move down to the current
// index). TODO: What if it's the last file?
@@ -408,11 +421,26 @@ const FileViewer: React.FC<FileViewerProps> = ({
);
const toggleFavorite = useCallback(
- ({ file }: FileViewerAnnotatedFile) =>
- onToggleFavorite!(file)
- .then(handleNeedsRemoteSync)
- .catch(onGenericError),
- [onToggleFavorite, handleNeedsRemoteSync, onGenericError],
+ ({ file }: FileViewerAnnotatedFile) => {
+ return new Promise((resolve) => {
+ // See: [Note: File viewer update and dispatch]
+ onToggleFavorite!(file)
+ .then(
+ () => awaitNextFilesOrFavoritesUpdate(resolve),
+ (e: unknown) => {
+ onGenericError(e);
+ resolve();
+ },
+ )
+ .finally(handleNeedsRemoteSync);
+ });
+ },
+ [
+ onToggleFavorite,
+ onGenericError,
+ awaitNextFilesOrFavoritesUpdate,
+ handleNeedsRemoteSync,
+ ],
);
// Initial value of delegate.
@@ -428,13 +456,13 @@ const FileViewer: React.FC<FileViewerProps> = ({
delegate.toggleFavorite = toggleFavorite;
}, [getFiles, isFavorite, toggleFavorite]);
- // Notify the listeners, if any, for updates to files.
+ // Notify the listeners, if any, for updates to files or favorites.
useEffect(() => {
- setOnNextFilesUpdate((cbs) => {
+ setOnNextFilesOrFavoritesUpdate((cbs) => {
cbs.forEach((cb) => cb());
return [];
});
- }, [files]);
+ }, [files, favoriteFileIDs]);
useEffect(() => {
if (open) {
diff --git a/web/packages/gallery/components/viewer/photoswipe.ts b/web/packages/gallery/components/viewer/photoswipe.ts
index a67469df61..2661d2100f 100644
--- a/web/packages/gallery/components/viewer/photoswipe.ts
+++ b/web/packages/gallery/components/viewer/photoswipe.ts
@@ -524,26 +524,14 @@ export class FileViewerPhotoSwipe {
this.pendingFavoriteUpdates.add(af.file.id);
buttonElement.disabled = true;
await delegate.toggleFavorite(af);
- // [Note: File viewer action setTimeout TODO]
- //
- // TODO: This can be improved in two ways:
- //
- // 1. We currently have a setTimeout to ensure that the
- // updated `favoriteFileIDs` have made their way to our
- // delegate before we query for the status again.
- // Obviously, this is hacky. Note that a timeout of 0
- // (i.e., just deferring till the next tick) isn't enough
- // here, for reasons I need to investigate more (hence
- // this TODO).
- //
- // 2. We reload the entire slide instead of just updating
- // the button state. This is because there are two
- // buttons, instead of a single button toggling between
- // two states (e.g. like the zoom button). This aspect is
- // easier to improve: A single button can be achieved by
- // moving the fill as a layer.
- await new Promise((r) => setTimeout(r, 100));
this.pendingFavoriteUpdates.delete(af.file.id);
+ // TODO: We reload the entire slide instead of just updating
+ // the button state. This is because there are two buttons,
+ // instead of a single button toggling between two states
+ // e.g. like the zoom button.
+ //
+ // To fix this, a single button can be achieved by moving
+ // the fill of the heart as a layer.
this.refreshCurrentSlideContent();
};