// @flow import React, { useState } from 'react'; import FileSelector from 'component/common/file-selector'; import Button from 'component/button'; import FileThumbnail from 'component/fileThumbnail'; import * as MODALS from 'constants/modal_types'; import { serializeFileObj } from 'util/file'; import { tusIsSessionLocked } from 'util/tus'; type Props = { uploadItem: FileUploadItem, doPublishResume: (any) => void, doUpdateUploadRemove: (string, any) => void, doOpenModal: (string, {}) => void, }; export default function WebUploadItem(props: Props) { const { uploadItem, doPublishResume, doUpdateUploadRemove, doOpenModal } = props; const { params, file, fileFingerprint, progress, status, resumable, uploader } = uploadItem; const [showFileSelector, setShowFileSelector] = useState(false); const locked = tusIsSessionLocked(params.guid); function handleFileChange(newFile: WebFile, clearName = true) { if (serializeFileObj(newFile) === fileFingerprint) { setShowFileSelector(false); doPublishResume({ ...params, file_path: newFile }); if (!params.guid) { // Can remove this if-block after January 2022. doUpdateUploadRemove('', params); } } else { doOpenModal(MODALS.CONFIRM, { title: __('Invalid file'), subtitle: __('It appears to be a different or modified file.'), body:
{__('Please select the same file from the initial upload.')}
, onConfirm: (closeModal) => closeModal(), hideCancel: true, }); } } function handleCancel() { doOpenModal(MODALS.CONFIRM, { title: __('Cancel upload'), subtitle: __('Cancel and remove the selected upload?'), body: params.name ?{`lbry://${params.name}`}
: undefined, onConfirm: (closeModal) => { if (tusIsSessionLocked(params.guid)) { // Corner-case: it's possible for the upload to resume in another tab // after the modal has appeared. Make a final lock-check here. // We can invoke a toast here, but just do nothing for now. // The upload status should make things obvious. } else { if (uploader) { if (resumable) { // $FlowFixMe - couldn't resolve to TusUploader manually. uploader.abort(true); // TUS } else { uploader.abort(); // XHR } } // The second parameter (params) can be removed after January 2022. doUpdateUploadRemove(params.guid, params); } closeModal(); }, }); } function resolveProgressStr() { if (locked) { return __('File being uploaded in another tab or window.'); } if (!uploader) { return __('Stopped.'); } if (resumable) { if (status) { switch (status) { case 'retry': return __('Retrying...'); case 'error': return __('Failed.'); case 'conflict': return __('Stopped. Duplicate session detected.'); default: return status; } } else { const progressInt = parseInt(progress); return progressInt === 100 ? __('Processing...') : __('Uploading...'); } } else { return __('Uploading...'); } } function getRetryButton() { if (!resumable || locked) { return null; } if (uploader) { // Should still be uploading. Don't show. return null; } else { // Refreshed or connection broken. const isFileActive = file instanceof File; return (