lbry-desktop/ui/modal/modalFileSelection/view.jsx
Franco Montenegro 329d434c83
Allow only images in modal image uploader. (#7672)
* Allow only images in modal image uploader.

* Set file path and mime in file selector.

* Refactor WebFile.

* Update get-file-from-path to work with folders; fix file-list component.

* Get rid of File | string for filePath property in components.

* Show instant preview while updating channel thumbnail.

* Fix publish.

* Add jpeg and svg to image filter.
2022-09-02 12:43:35 -04:00

78 lines
2.1 KiB
JavaScript

// @flow
import * as ICONS from 'constants/icons';
import * as PAGES from 'constants/pages';
import React from 'react';
import { Modal } from 'modal/modal';
import { withRouter } from 'react-router';
import Card from 'component/common/card';
import Button from 'component/button';
import FileList from 'component/common/file-list';
type Props = {
files: Array<File>,
hideModal: () => void,
updatePublishForm: ({}) => void,
history: {
location: { pathname: string },
push: (string) => void,
},
};
const PUBLISH_URL = `/$/${PAGES.UPLOAD}`;
const ModalFileSelection = (props: Props) => {
const { history, files, hideModal, updatePublishForm } = props;
const [selectedFile, setSelectedFile] = React.useState(null);
const navigateToPublish = React.useCallback(() => {
// Navigate only if location is not publish area:
// - Prevent spam in history
if (history.location.pathname !== PUBLISH_URL) {
history.push(PUBLISH_URL);
}
}, [history]);
function handleCloseModal() {
hideModal();
setSelectedFile(null);
}
function handleSubmit() {
updatePublishForm({ filePath: selectedFile });
handleCloseModal();
navigateToPublish();
}
const handleFileChange = (file?: File) => {
// $FlowFixMe
setSelectedFile(file);
};
return (
<Modal isOpen type="card" onAborted={handleCloseModal} onConfirmed={handleCloseModal}>
<Card
icon={ICONS.PUBLISH}
title={__('Choose a file')}
subtitle={__('Only one file is allowed, choose wisely:')}
actions={
<div>
<div>
<FileList files={files} onChange={handleFileChange} />
</div>
<div className="section__actions">
<Button
disabled={!selectedFile || !files || !files.length}
button="primary"
label={__('Accept')}
onClick={handleSubmit}
/>
<Button button="link" label={__('Cancel')} onClick={handleCloseModal} />
</div>
</div>
}
/>
</Modal>
);
};
export default withRouter(ModalFileSelection);