@uppy/status-bar
Version:
A progress bar for Uppy, with many bells and whistles.
190 lines • 5.72 kB
JavaScript
import { h } from 'preact';
import classNames from 'classnames';
import statusBarStates from './StatusBarStates.js';
import calculateProcessingProgress from './calculateProcessingProgress.js';
import { UploadBtn, RetryBtn, CancelBtn, PauseResumeButton, DoneBtn, ProgressBarProcessing, ProgressBarError, ProgressBarUploading, ProgressBarComplete } from "./Components.js";
const {
STATE_ERROR,
STATE_WAITING,
STATE_PREPROCESSING,
STATE_UPLOADING,
STATE_POSTPROCESSING,
STATE_COMPLETE
} = statusBarStates;
export default function StatusBarUI(_ref) {
let {
newFiles,
allowNewUpload,
isUploadInProgress,
isAllPaused,
resumableUploads,
error,
hideUploadButton = undefined,
hidePauseResumeButton = false,
hideCancelButton = false,
hideRetryButton = false,
recoveredState,
uploadState,
totalProgress,
files,
supportsUploadProgress,
hideAfterFinish = false,
isSomeGhost,
doneButtonHandler = undefined,
isUploadStarted,
i18n,
startUpload,
uppy,
isAllComplete,
showProgressDetails = undefined,
numUploads,
complete,
totalSize,
totalETA,
totalUploadedSize
} = _ref;
function getProgressValue() {
switch (uploadState) {
case STATE_POSTPROCESSING:
case STATE_PREPROCESSING:
{
const progress = calculateProcessingProgress(files);
if (progress.mode === 'determinate') {
return progress.value * 100;
}
return totalProgress;
}
case STATE_ERROR:
{
return null;
}
case STATE_UPLOADING:
{
if (!supportsUploadProgress) {
return null;
}
return totalProgress;
}
default:
return totalProgress;
}
}
function getIsIndeterminate() {
switch (uploadState) {
case STATE_POSTPROCESSING:
case STATE_PREPROCESSING:
{
const {
mode
} = calculateProcessingProgress(files);
return mode === 'indeterminate';
}
case STATE_UPLOADING:
{
if (!supportsUploadProgress) {
return true;
}
return false;
}
default:
return false;
}
}
const progressValue = getProgressValue();
const width = progressValue != null ? progressValue : 100;
const showUploadBtn = !error && newFiles && (!isUploadInProgress && !isAllPaused || recoveredState) && allowNewUpload && !hideUploadButton;
const showCancelBtn = !hideCancelButton && uploadState !== STATE_WAITING && uploadState !== STATE_COMPLETE;
const showPauseResumeBtn = resumableUploads && !hidePauseResumeButton && uploadState === STATE_UPLOADING;
const showRetryBtn = error && !isAllComplete && !hideRetryButton;
const showDoneBtn = doneButtonHandler && uploadState === STATE_COMPLETE;
const progressClassNames = classNames('uppy-StatusBar-progress', {
'is-indeterminate': getIsIndeterminate()
});
const statusBarClassNames = classNames('uppy-StatusBar', `is-${uploadState}`, {
'has-ghosts': isSomeGhost
});
const progressBarStateEl = (() => {
switch (uploadState) {
case STATE_PREPROCESSING:
case STATE_POSTPROCESSING:
return h(ProgressBarProcessing, {
progress: calculateProcessingProgress(files)
});
case STATE_COMPLETE:
return h(ProgressBarComplete, {
i18n: i18n
});
case STATE_ERROR:
return h(ProgressBarError, {
error: error,
i18n: i18n,
numUploads: numUploads,
complete: complete
});
case STATE_UPLOADING:
return h(ProgressBarUploading, {
i18n: i18n,
supportsUploadProgress: supportsUploadProgress,
totalProgress: totalProgress,
showProgressDetails: showProgressDetails,
isUploadStarted: isUploadStarted,
isAllComplete: isAllComplete,
isAllPaused: isAllPaused,
newFiles: newFiles,
numUploads: numUploads,
complete: complete,
totalUploadedSize: totalUploadedSize,
totalSize: totalSize,
totalETA: totalETA,
startUpload: startUpload
});
default:
return null;
}
})();
const atLeastOneAction = showUploadBtn || showRetryBtn || showPauseResumeBtn || showCancelBtn || showDoneBtn;
const thereIsNothingInside = !atLeastOneAction && !progressBarStateEl;
const isHidden = thereIsNothingInside || uploadState === STATE_COMPLETE && hideAfterFinish;
if (isHidden) {
return null;
}
return h("div", {
className: statusBarClassNames
}, h("div", {
className: progressClassNames,
style: {
width: `${width}%`
},
role: "progressbar",
"aria-label": `${width}%`,
"aria-valuetext": `${width}%`,
"aria-valuemin": 0,
"aria-valuemax": 100,
"aria-valuenow": progressValue
}), progressBarStateEl, h("div", {
className: "uppy-StatusBar-actions"
}, showUploadBtn ? h(UploadBtn, {
newFiles: newFiles,
isUploadStarted: isUploadStarted,
recoveredState: recoveredState,
i18n: i18n,
isSomeGhost: isSomeGhost,
startUpload: startUpload,
uploadState: uploadState
}) : null, showRetryBtn ? h(RetryBtn, {
i18n: i18n,
uppy: uppy
}) : null, showPauseResumeBtn ? h(PauseResumeButton, {
isAllPaused: isAllPaused,
i18n: i18n,
isAllComplete: isAllComplete,
resumableUploads: resumableUploads,
uppy: uppy
}) : null, showCancelBtn ? h(CancelBtn, {
i18n: i18n,
uppy: uppy
}) : null, showDoneBtn ? h(DoneBtn, {
i18n: i18n,
doneButtonHandler: doneButtonHandler
}) : null));
}