Skip to content

armgit / ente

Public Imported from GitHub
0

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

Sketch

Manav Rathi9020907b56ab
diff --git a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
index ea797f5045..594113060b 100644
--- a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
+++ b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
@@ -5,7 +5,11 @@ import {
} from "components/Collections/CollectionShare";
import type { TimeStampListItem } from "components/FileList";
import { useModalVisibility } from "ente-base/components/utils/modal";
-import { type FilesDownloadProgressAttributes } from "ente-gallery/services/save";
+import {
+ isFilesDownloadCancelled,
+ isSaveComplete,
+ type SaveGroup,
+} from "ente-gallery/services/save";
import type { Collection } from "ente-media/collection";
import {
GalleryBarImpl,
@@ -22,10 +26,6 @@ import {
import { includes } from "ente-utils/type-guards";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { sortCollectionSummaries } from "services/collectionService";
-import {
- isFilesDownloadCancelled,
- isFilesDownloadCompleted,
-} from "../DownloadProgress";
import { AlbumCastDialog } from "./AlbumCastDialog";
import {
CollectionHeader,
@@ -47,7 +47,7 @@ type GalleryBarAndListHeaderProps = Omit<
activeCollection: Collection;
setActiveCollectionID: (collectionID: number) => void;
setPhotoListHeader: (value: TimeStampListItem) => void;
- filesDownloadProgressAttributesList: FilesDownloadProgressAttributes[];
+ filesDownloadProgressAttributesList: SaveGroup[];
} & Pick<
CollectionHeaderProps,
"setFilesDownloadProgressAttributesCreator" | "onRemotePull"
@@ -131,7 +131,7 @@ export const GalleryBarAndListHeader: React.FC<
return (
attributes &&
!isFilesDownloadCancelled(attributes) &&
- !isFilesDownloadCompleted(attributes)
+ !isSaveComplete(attributes)
);
}, [activeCollectionID, filesDownloadProgressAttributesList]);
diff --git a/web/apps/photos/src/components/DownloadProgress.tsx b/web/apps/photos/src/components/DownloadStatusNotifications.tsx
similarity index 55%
rename from web/apps/photos/src/components/DownloadProgress.tsx
rename to web/apps/photos/src/components/DownloadStatusNotifications.tsx
index 87e628287c..7c0c188438 100644
--- a/web/apps/photos/src/components/DownloadProgress.tsx
+++ b/web/apps/photos/src/components/DownloadStatusNotifications.tsx
@@ -1,13 +1,23 @@
-// TODO: Audit this file
-/* eslint-disable react-refresh/only-export-components */
import { useBaseContext } from "ente-base/context";
-import type { FilesDownloadProgressAttributes } from "ente-gallery/services/save";
+import {
+ isSaveComplete,
+ isSaveCompleteWithErrors,
+ isSaveStarted,
+ type SaveGroup,
+} from "ente-gallery/services/save";
import { Notification } from "ente-new/photos/components/Notification";
import { t } from "i18next";
-interface FilesDownloadProgressProps {
- attributesList: FilesDownloadProgressAttributes[];
- setAttributesList: (value: FilesDownloadProgressAttributes[]) => void;
+interface DownloadStatusNotificationsProps {
+ /**
+ * A list of user-initiated downloads for which a status should be shown.
+ *
+ * An entry is added to this list when the user initiates the download, and
+ * remains here until the user explicitly closes the corresponding
+ * {@link Notification} component that was showing the save group's status.
+ */
+ saveGroups: SaveGroup[];
+ setAttributesList: (value: SaveGroup[]) => void;
/**
* Called when the hidden section should be shown.
*
@@ -29,56 +39,25 @@ interface FilesDownloadProgressProps {
onShowCollection?: (collectionID: number) => void;
}
-export const isFilesDownloadStarted = (
- attributes: FilesDownloadProgressAttributes,
-) => {
- return attributes && attributes.total > 0;
-};
-
-export const isFilesDownloadCompleted = (
- attributes: FilesDownloadProgressAttributes,
-) => {
- return (
- attributes &&
- attributes.success + attributes.failed === attributes.total
- );
-};
-
-const isFilesDownloadCompletedWithErrors = (
- attributes: FilesDownloadProgressAttributes,
-) => {
- return (
- attributes &&
- attributes.failed > 0 &&
- isFilesDownloadCompleted(attributes)
- );
-};
-
-export const isFilesDownloadCancelled = (
- attributes: FilesDownloadProgressAttributes,
-) => {
- return attributes?.canceller?.signal?.aborted;
-};
-
-export const FilesDownloadProgress: React.FC<FilesDownloadProgressProps> = ({
- attributesList,
+/**
+ * A component that shows a list of notifications, one each for an active
+ * user-initiated download.
+ */
+export const DownloadStatusNotifications: React.FC<
+ DownloadStatusNotificationsProps
+> = ({
+ saveGroups,
setAttributesList,
onShowHiddenSection,
onShowCollection,
}) => {
const { showMiniDialog } = useBaseContext();
- if (!attributesList) {
- return <></>;
- }
-
const onClose = (id: number) => {
- setAttributesList(attributesList.filter((attr) => attr.id !== id));
+ setAttributesList(saveGroups.filter((attr) => attr.id !== id));
};
- const confirmCancelDownload = (
- attributes: FilesDownloadProgressAttributes,
- ) => {
+ const confirmCancelDownload = (attributes: SaveGroup) => {
showMiniDialog({
title: t("stop_downloads_title"),
message: t("stop_downloads_message"),
@@ -94,8 +73,8 @@ export const FilesDownloadProgress: React.FC<FilesDownloadProgressProps> = ({
});
};
- const handleClose = (attributes: FilesDownloadProgressAttributes) => () => {
- if (isFilesDownloadCompleted(attributes)) {
+ const handleClose = (attributes: SaveGroup) => () => {
+ if (isSaveComplete(attributes)) {
onClose(attributes.id);
} else {
confirmCancelDownload(attributes);
@@ -105,7 +84,7 @@ export const FilesDownloadProgress: React.FC<FilesDownloadProgressProps> = ({
const createHandleOnClick =
(id: number, onShowCollection: (collectionID: number) => void) =>
() => {
- const attributes = attributesList.find((attr) => attr.id === id);
+ const attributes = saveGroups.find((attr) => attr.id === id);
const electron = globalThis.electron;
if (electron) {
electron.openDirectory(attributes.downloadDirPath);
@@ -120,40 +99,43 @@ export const FilesDownloadProgress: React.FC<FilesDownloadProgressProps> = ({
}
};
+ if (!saveGroups) {
+ return <></>;
+ }
+
const notifications: React.ReactNode[] = [];
+
let visibleIndex = 0;
- for (const attributes of attributesList) {
+ for (const group of saveGroups) {
// Skip attempted downloads of empty albums, which had no effect.
- if (!isFilesDownloadStarted(attributes)) continue;
+ if (!isSaveStarted(group)) continue;
const index = visibleIndex++;
notifications.push(
<Notification
- key={attributes.id}
+ key={group.id}
horizontal="left"
sx={{ "&&": { bottom: `${index * 80 + 20}px` } }}
- open={isFilesDownloadStarted(attributes)}
- onClose={handleClose(attributes)}
+ open={isSaveStarted(group)}
+ onClose={handleClose(group)}
keepOpenOnClick
attributes={{
- color: isFilesDownloadCompletedWithErrors(attributes)
+ color: isSaveCompleteWithErrors(group)
? "critical"
: "secondary",
- title: isFilesDownloadCompletedWithErrors(attributes)
+ title: isSaveCompleteWithErrors(group)
? t("download_failed")
- : isFilesDownloadCompleted(attributes)
+ : isSaveComplete(group)
? t("download_complete")
- : t("downloading_album", {
- name: attributes.folderName,
- }),
- caption: isFilesDownloadCompleted(attributes)
- ? attributes.folderName
+ : t("downloading_album", { name: group.folderName }),
+ caption: isSaveComplete(group)
+ ? group.folderName
: t("download_progress", {
- count: attributes.success + attributes.failed,
- total: attributes.total,
+ count: group.success + group.failed,
+ total: group.total,
}),
onClick: onShowCollection
- ? createHandleOnClick(attributes.id, onShowCollection)
+ ? createHandleOnClick(group.id, onShowCollection)
: undefined,
}}
/>,
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 334910b2d8..e0e95c0dc7 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -5,9 +5,9 @@ import { IconButton, Stack, Typography } from "@mui/material";
import { AuthenticateUser } from "components/AuthenticateUser";
import { GalleryBarAndListHeader } from "components/Collections/GalleryBarAndListHeader";
import {
- FilesDownloadProgress,
+ DownloadStatusNotifications,
type FilesDownloadProgressAttributes,
-} from "components/DownloadProgress";
+} from "components/DownloadStatusNotifications";
import { type TimeStampListItem } from "components/FileList";
import { FileListWithViewer } from "components/FileListWithViewer";
import { FixCreationTime } from "components/FixCreationTime";
@@ -995,8 +995,8 @@ const Page: React.FC = () => {
)!
}
/>
- <FilesDownloadProgress
- attributesList={filesDownloadProgressAttributesList}
+ <DownloadStatusNotifications
+ saveGroups={filesDownloadProgressAttributesList}
setAttributesList={setFilesDownloadProgressAttributesList}
onShowHiddenSection={handleShowHiddenSection}
onShowCollection={handleShowCollection}
diff --git a/web/apps/photos/src/pages/shared-albums.tsx b/web/apps/photos/src/pages/shared-albums.tsx
index 85865797df..030562840f 100644
--- a/web/apps/photos/src/pages/shared-albums.tsx
+++ b/web/apps/photos/src/pages/shared-albums.tsx
@@ -6,7 +6,7 @@ import DownloadIcon from "@mui/icons-material/Download";
import FileDownloadOutlinedIcon from "@mui/icons-material/FileDownloadOutlined";
import { Box, Button, IconButton, Stack, styled, Tooltip } from "@mui/material";
import Typography from "@mui/material/Typography";
-import { FilesDownloadProgress } from "components/DownloadProgress";
+import { DownloadStatusNotifications } from "components/DownloadStatusNotifications";
import type { TimeStampListItem } from "components/FileList";
import { FileListWithViewer } from "components/FileListWithViewer";
import { Upload } from "components/Upload";
@@ -51,7 +51,7 @@ import { downloadManager } from "ente-gallery/services/download";
import {
downloadCollectionFiles,
downloadSelectedFiles,
- type FilesDownloadProgressAttributes,
+ type SaveGroup,
type SetFilesDownloadProgressAttributes,
type SetFilesDownloadProgressAttributesCreator,
} from "ente-gallery/services/save";
@@ -133,7 +133,7 @@ export default function PublicCollectionGallery() {
const [
filesDownloadProgressAttributesList,
setFilesDownloadProgressAttributesList,
- ] = useState<FilesDownloadProgressAttributes[]>([]);
+ ] = useState<SaveGroup[]>([]);
const setFilesDownloadProgressAttributesCreator: SetFilesDownloadProgressAttributesCreator =
useCallback((folderName, collectionID, isHidden) => {
@@ -572,8 +572,8 @@ export default function PublicCollectionGallery() {
onShowSessionExpiredDialog={showPublicLinkExpiredMessage}
{...{ dragAndDropFiles }}
/>
- <FilesDownloadProgress
- attributesList={filesDownloadProgressAttributesList}
+ <DownloadStatusNotifications
+ saveGroups={filesDownloadProgressAttributesList}
setAttributesList={setFilesDownloadProgressAttributesList}
/>
</FullScreenDropZone>
diff --git a/web/packages/gallery/services/save.ts b/web/packages/gallery/services/save.ts
index 627f76e8a8..1d35e4a7a5 100644
--- a/web/packages/gallery/services/save.ts
+++ b/web/packages/gallery/services/save.ts
@@ -27,32 +27,60 @@ import {
import { wait } from "ente-utils/promise";
/**
- * An object that keeps track of progress of the download of a set of files.
+ * An object that keeps track of progress of a user-initiated download of a set
+ * of files to the user's device.
*
- * This "download" is distinct from the downloads the app does from remote. What
- * we're doing here is perhaps more accurately but too verbosely described as "a
- * user initiated download of files to the user's device", aka "saving them". In
- * contrast, the app does the "download the file from remote" internal action,
- * e.g. for showing to the user or performing some indexing etc.
+ * This "download" is distinct from the downloads the app does from remote (e.g.
+ * when the user is viewing them).
+ *
+ * What we're doing here is perhaps more accurately described "a user initiated
+ * download of files to the user's device", but that is too long, so we instead
+ * refer to this process as "saving them".
+ *
+ * Note however that the app's UI itself takes the user perspective, so the
+ * upper (UI) layers use the word "download", while this implementation layer
+ * uses the word "save", and there is an unavoidable incongruity in the middle.
*/
-export interface FilesDownloadProgressAttributes {
+export interface SaveGroup {
+ /**
+ * A unique identifier of this set of saves.
+ */
id: number;
+ /**
+ * The total number of files to save to the user's device.
+ */
+ total: number;
+ /**
+ * The number of files that have already been save.
+ */
success: number;
+ /**
+ * The number of failures.
+ */
failed: number;
- total: number;
folderName: string;
collectionID: number;
isHidden: boolean;
downloadDirPath: string;
+ /**
+ * An {@link AbortController} that can be used to cancel the save.
+ */
canceller: AbortController;
}
+export const isSaveStarted = (group: SaveGroup) => group.total > 0;
+
+export const isSaveComplete = ({ total, success, failed }: SaveGroup) =>
+ total == success + failed;
+
+export const isSaveCompleteWithErrors = (group: SaveGroup) =>
+ group.failed > 0 && isSaveComplete(group);
+
+export const isSaveCancelled = (group: SaveGroup) =>
+ group.canceller.signal.aborted;
+
export type SetFilesDownloadProgressAttributes = (
- value:
- | Partial<FilesDownloadProgressAttributes>
- | ((
- prev: FilesDownloadProgressAttributes,
- ) => FilesDownloadProgressAttributes),
+ value: Partial<SaveGroup> | ((prev: SaveGroup) => SaveGroup),
) => void;
export type SetFilesDownloadProgressAttributesCreator = (