// @flow import React, { useState } from 'react'; import { FormField } from 'component/common/form'; import FileSelector from 'component/common/file-selector'; import Button from 'component/button'; import { SPEECH_URLS } from 'lbry-redux'; import uuid from 'uuid/v4'; const accept = '.png, .jpg, .jpeg, .gif'; const SOURCE_URL = 'url'; const SOURCE_UPLOAD = 'upload'; const SPEECH_READY = 'READY'; const SPEECH_UPLOADING = 'UPLOADING'; type Props = { assetName: string, currentValue: ?string, onUpdate: string => void, recommended: string, }; function SelectAsset(props: Props) { const { onUpdate, assetName, currentValue, recommended } = props; const [assetSource, setAssetSource] = useState(SOURCE_URL); const [pathSelected, setPathSelected] = useState(''); const [fileSelected, setFileSelected] = useState(null); const [uploadStatus, setUploadStatus] = useState(SPEECH_READY); const [error, setError] = useState(); function doUploadAsset(file) { const uploadError = (error = '') => { setError(error); }; const setUrl = path => { setUploadStatus(SPEECH_READY); onUpdate(path); setAssetSource(SOURCE_URL); }; setUploadStatus(SPEECH_UPLOADING); const data = new FormData(); const name = uuid(); data.append('name', name); data.append('file', file); return fetch(SPEECH_URLS.SPEECH_PUBLISH, { method: 'POST', body: data, }) .then(response => response.json()) .then(json => (json.success ? setUrl(`${json.data.serveUrl}`) : uploadError(json.message))) .catch(err => uploadError(err.message)); } return ( setAssetSource(e.target.value)} label={__(assetName + ' source')} > {assetSource === SOURCE_UPLOAD && (
{error &&
{error}
} {!pathSelected && ( { if (file.name) { setPathSelected(file.path || file.name); setFileSelected(file); } }} accept={accept} /> )} {pathSelected && (
{`...${pathSelected.slice(-18)}`} {uploadStatus}{' '} {' '}
)}
)} {assetSource === SOURCE_URL && ( { onUpdate(e.target.value); }} /> )}
); } export default SelectAsset;