import type { State, Uppy, UppyFile } from '@uppy/core' import type { Body, I18n, Meta } from '@uppy/core/utils' import type { ComponentChildren } from 'preact' const uploadStates = { STATE_ERROR: 'error', STATE_WAITING: 'waiting', STATE_PREPROCESSING: 'preprocessing', STATE_UPLOADING: 'uploading', STATE_POSTPROCESSING: 'postprocessing', STATE_COMPLETE: 'complete', STATE_PAUSED: 'paused', } as const export type UploadState = (typeof uploadStates)[keyof typeof uploadStates] function getUploadingState( isAllErrored: boolean | undefined, isAllComplete: boolean, isAllPaused: boolean, files: Record> = {}, ): UploadState { if (isAllErrored) { return uploadStates.STATE_ERROR } if (isAllComplete) { return uploadStates.STATE_COMPLETE } if (isAllPaused) { return uploadStates.STATE_PAUSED } let state: UploadState = uploadStates.STATE_WAITING const fileIDs = Object.keys(files) for (let i = 0; i < fileIDs.length; i++) { const { progress } = files[fileIDs[i] as keyof typeof files] // If ANY files are being uploaded right now, show the uploading state. if (progress.uploadStarted && !progress.uploadComplete) { return uploadStates.STATE_UPLOADING } // If files are being preprocessed AND postprocessed at this time, we show the // preprocess state. The uploading state is handled by the early return above. if (progress.preprocess) { state = uploadStates.STATE_PREPROCESSING } // If NO files are being preprocessed or uploaded right now, but some files are // being postprocessed, show the postprocess state. if (progress.postprocess && state !== uploadStates.STATE_PREPROCESSING) { state = uploadStates.STATE_POSTPROCESSING } } return state } interface UploadStatusProps { files: State['files'] i18n: I18n isAllComplete: boolean isAllErrored?: boolean | undefined isAllPaused: boolean inProgressNotPausedFiles: UppyFile[] newFiles: UppyFile[] processingFiles: UppyFile[] } function UploadStatus({ files, i18n, isAllComplete, isAllErrored, isAllPaused, inProgressNotPausedFiles, newFiles, processingFiles, }: UploadStatusProps): ComponentChildren { const uploadingState = getUploadingState( isAllErrored, isAllComplete, isAllPaused, files, ) switch (uploadingState) { case 'uploading': return i18n('uploadingXFiles', { smart_count: inProgressNotPausedFiles.length, }) case 'preprocessing': case 'postprocessing': return i18n('processingXFiles', { smart_count: processingFiles.length }) case 'paused': return i18n('uploadPaused') case 'waiting': return i18n('xFilesSelected', { smart_count: newFiles.length }) case 'complete': return i18n('uploadComplete') case 'error': return i18n('error') } } interface PanelTopBarProps extends UploadStatusProps { allowNewUpload: boolean i18n: I18n hideCancelButton: boolean maxNumberOfFiles: number | null totalFileCount: number toggleAddFilesPanel: (enabled: boolean) => void uppy: Uppy } function PanelTopBar( props: PanelTopBarProps, ): ComponentChildren { const { i18n, isAllComplete, hideCancelButton, maxNumberOfFiles, toggleAddFilesPanel, totalFileCount, uppy, } = props let { allowNewUpload } = props // TODO maybe this should be done in ../Dashboard.js, then just pass that down as `allowNewUpload` if (allowNewUpload && maxNumberOfFiles) { allowNewUpload = totalFileCount < maxNumberOfFiles } return (
{!isAllComplete && !hideCancelButton ? ( ) : (
)}
{allowNewUpload ? ( ) : (
)}
) } export default PanelTopBar