diff --git a/apps/photos/src/components/Collections/CollectionDownloadProgress.tsx b/apps/photos/src/components/Collections/CollectionDownloadProgress.tsx deleted file mode 100644 index 7b91b35c6..000000000 --- a/apps/photos/src/components/Collections/CollectionDownloadProgress.tsx +++ /dev/null @@ -1,160 +0,0 @@ -import Notification from 'components/Notification'; -import { t } from 'i18next'; -import isElectron from 'is-electron'; -import { AppContext } from 'pages/_app'; -import { GalleryContext } from 'pages/gallery'; -import { useContext } from 'react'; -import ElectronAPIs from '@ente/shared/electron'; - -export interface CollectionDownloadProgressAttributes { - success: number; - failed: number; - total: number; - collectionName: string; - collectionID: number; - isHidden: boolean; - downloadDirPath: string; - canceller: AbortController; -} - -interface CollectionDownloadProgressProps { - attributesList: CollectionDownloadProgressAttributes[]; - setAttributesList: (value: CollectionDownloadProgressAttributes[]) => void; -} - -export const isCollectionDownloadCompleted = ( - attributes: CollectionDownloadProgressAttributes -) => { - return ( - attributes && - attributes.success + attributes.failed === attributes.total - ); -}; - -export const isCollectionDownloadCompletedWithErrors = ( - attributes: CollectionDownloadProgressAttributes -) => { - return ( - attributes && - attributes.failed > 0 && - isCollectionDownloadCompleted(attributes) - ); -}; - -export const isCollectionDownloadCancelled = ( - attributes: CollectionDownloadProgressAttributes -) => { - return attributes && attributes.canceller?.signal?.aborted; -}; - -export const CollectionDownloadProgress: React.FC = - ({ attributesList, setAttributesList }) => { - const appContext = useContext(AppContext); - const galleryContext = useContext(GalleryContext); - - if (!attributesList) { - return <>; - } - - const onClose = (collectionID: number) => { - setAttributesList( - attributesList.filter( - (attr) => attr.collectionID !== collectionID - ) - ); - }; - - const confirmCancelUpload = ( - attributes: CollectionDownloadProgressAttributes - ) => { - appContext.setDialogMessage({ - title: t('STOP_DOWNLOADS_HEADER'), - content: t('STOP_ALL_DOWNLOADS_MESSAGE'), - proceed: { - text: t('YES_STOP_DOWNLOADS'), - variant: 'critical', - action: () => { - attributes?.canceller.abort(); - onClose(attributes.collectionID); - }, - }, - close: { - text: t('NO'), - variant: 'secondary', - action: () => {}, - }, - }); - }; - - const handleClose = - (attributes: CollectionDownloadProgressAttributes) => () => { - if (isCollectionDownloadCompleted(attributes)) { - onClose(attributes.collectionID); - } else { - confirmCancelUpload(attributes); - } - }; - - const handleOnClick = (collectionID: number) => () => { - const attributes = attributesList.find( - (attr) => attr.collectionID === collectionID - ); - if (isElectron()) { - ElectronAPIs.openDirectory(attributes.downloadDirPath); - } else { - if (attributes.isHidden) { - galleryContext.openHiddenSection(() => { - galleryContext.setActiveCollectionID( - attributes.collectionID - ); - }); - } else { - galleryContext.setActiveCollectionID( - attributes.collectionID - ); - } - } - }; - - return ( - <> - {attributesList.map((attributes, index) => ( - - ))} - - ); - }; diff --git a/apps/photos/src/components/FilesDownloadProgress.tsx b/apps/photos/src/components/FilesDownloadProgress.tsx new file mode 100644 index 000000000..5c1285486 --- /dev/null +++ b/apps/photos/src/components/FilesDownloadProgress.tsx @@ -0,0 +1,154 @@ +import Notification from 'components/Notification'; +import { t } from 'i18next'; +import isElectron from 'is-electron'; +import { AppContext } from 'pages/_app'; +import { GalleryContext } from 'pages/gallery'; +import { useContext } from 'react'; +import ElectronAPIs from '@ente/shared/electron'; + +export interface FilesDownloadProgressAttributes { + id: number; + success: number; + failed: number; + total: number; + folderName: string; + collectionID: number; + isHidden: boolean; + downloadDirPath: string; + canceller: AbortController; +} + +interface FilesDownloadProgressProps { + attributesList: FilesDownloadProgressAttributes[]; + setAttributesList: (value: FilesDownloadProgressAttributes[]) => void; +} + +export const isFilesDownloadCompleted = ( + attributes: FilesDownloadProgressAttributes +) => { + return ( + attributes && + attributes.success + attributes.failed === attributes.total + ); +}; + +export const isFilesDownloadCompletedWithErrors = ( + attributes: FilesDownloadProgressAttributes +) => { + return ( + attributes && + attributes.failed > 0 && + isFilesDownloadCompleted(attributes) + ); +}; + +export const isFilesDownloadCancelled = ( + attributes: FilesDownloadProgressAttributes +) => { + return attributes && attributes.canceller?.signal?.aborted; +}; + +export const FilesDownloadProgress: React.FC = ({ + attributesList, + setAttributesList, +}) => { + const appContext = useContext(AppContext); + const galleryContext = useContext(GalleryContext); + + if (!attributesList) { + return <>; + } + + const onClose = (collectionID: number) => { + setAttributesList( + attributesList.filter((attr) => attr.collectionID !== collectionID) + ); + }; + + const confirmCancelUpload = ( + attributes: FilesDownloadProgressAttributes + ) => { + appContext.setDialogMessage({ + title: t('STOP_DOWNLOADS_HEADER'), + content: t('STOP_ALL_DOWNLOADS_MESSAGE'), + proceed: { + text: t('YES_STOP_DOWNLOADS'), + variant: 'critical', + action: () => { + attributes?.canceller.abort(); + onClose(attributes.collectionID); + }, + }, + close: { + text: t('NO'), + variant: 'secondary', + action: () => {}, + }, + }); + }; + + const handleClose = (attributes: FilesDownloadProgressAttributes) => () => { + if (isFilesDownloadCompleted(attributes)) { + onClose(attributes.collectionID); + } else { + confirmCancelUpload(attributes); + } + }; + + const handleOnClick = (collectionID: number) => () => { + const attributes = attributesList.find( + (attr) => attr.collectionID === collectionID + ); + if (isElectron()) { + ElectronAPIs.openDirectory(attributes.downloadDirPath); + } else { + if (attributes.isHidden) { + galleryContext.openHiddenSection(() => { + galleryContext.setActiveCollectionID( + attributes.collectionID + ); + }); + } else { + galleryContext.setActiveCollectionID(attributes.collectionID); + } + } + }; + + return ( + <> + {attributesList.map((attributes, index) => ( + + ))} + + ); +};