1 import { ProgressBarStatus } from '../components/TransferManager/ProgressBar';
2 import type { Transfer, TransferSummary, TransfersStats } from '../components/TransferManager/transfer';
3 import { TransferState } from '../components/TransferManager/transfer';
5 export const isTransferFinished = ({ state }: { state: TransferState }) =>
6 [TransferState.Error, TransferState.Canceled, TransferState.Skipped, TransferState.Done].includes(state);
8 export const isTransferActive = ({ state }: { state: TransferState }) =>
10 TransferState.Pending,
11 TransferState.Initializing,
12 TransferState.Conflict,
13 TransferState.SignatureIssue,
14 TransferState.Progress,
15 TransferState.Finalizing,
18 export const isTransferFailed = ({ state }: { state: TransferState }) =>
19 [TransferState.Error, TransferState.Canceled].includes(state);
21 export const isTransferDone = ({ state }: { state: TransferState }) => state === TransferState.Done;
23 export const isTransferError = ({ state }: { state: TransferState }) =>
24 state === TransferState.Error || state === TransferState.NetworkError;
26 export const isTransferCanceled = ({ state }: { state: TransferState }) => state === TransferState.Canceled;
28 export const isTransferSkipped = ({ state }: { state: TransferState }) => state === TransferState.Skipped;
30 export const isTransferConflict = ({ state }: { state: TransferState }) => state === TransferState.Conflict;
32 export const isTransferSignatureIssue = ({ state }: { state: TransferState }) => state === TransferState.SignatureIssue;
34 export const isTransferProgress = ({ state }: { state: TransferState }) => state === TransferState.Progress;
36 export const isTransferInitializing = ({ state }: { state: TransferState }) => state === TransferState.Initializing;
38 export const isTransferManuallyPaused = ({ state }: { state: TransferState }) => state === TransferState.Paused;
40 export const isTransferPaused = ({ state }: { state: TransferState }) =>
41 state === TransferState.Paused || state === TransferState.NetworkError;
43 export const isTransferPausedByConnection = ({ state }: { state: TransferState }) =>
44 state === TransferState.NetworkError;
46 export const isTransferPending = ({ state }: { state: TransferState }) => state === TransferState.Pending;
48 export const isTransferFinalizing = ({ state }: { state: TransferState }) => state === TransferState.Finalizing;
50 export const isTransferOngoing = ({ state }: { state: TransferState }) => {
53 TransferState.Canceled,
54 TransferState.Skipped,
56 TransferState.Finalizing,
60 export const isTransferCancelError = (error: Error) => error.name === 'TransferCancel' || error.name === 'AbortError';
61 export const isPhotosDisabledUploadError = (error: Error) => error.name === 'PhotosUploadDisabled';
62 export const isTransferRetry = (error: Error) => error.name === 'TransferRetry';
63 export const isTransferSkipError = (error: Error) => error.name === 'TransferSkipped';
65 export const getProgressBarStatus = (transferState: TransferState): ProgressBarStatus => {
69 [TransferState.Done]: ProgressBarStatus.Success,
70 [TransferState.Canceled]: ProgressBarStatus.Disabled,
71 [TransferState.Error]: ProgressBarStatus.Error,
73 )[transferState] || ProgressBarStatus.Running
77 export const calculateProgress = (stats: TransfersStats, transfers: Transfer[]) => {
79 * Returns a transfer summary while compensating for empty files.
80 * We consider empty files as 1 byte to avoid constant 0% progress
81 * along the trasfer with multiple empty files.
83 function transferSummaryReducer(acc: TransferSummary, transfer: Transfer): TransferSummary {
84 const emptyFileCompensationInBytes = 1;
85 if (transfer.meta.size === 0) {
86 if (transfer.state === TransferState.Done) {
87 acc.progress += emptyFileCompensationInBytes;
89 acc.size += emptyFileCompensationInBytes;
91 acc.size += transfer.meta.size || 0;
93 (transfer.state === TransferState.Done ? transfer.meta.size : stats[transfer.id]?.progress) || 0;
99 const { progress, size } = transfers.reduce(transferSummaryReducer, { progress: 0, size: 0 });
101 return Math.floor(100 * (progress / (size || 1)));