// @flow import * as React from 'react'; import { observable } from 'mobx'; import { inject, observer } from 'mobx-react'; import { Redirect } from 'react-router-dom'; import styled from 'styled-components'; import { MoreIcon } from 'outline-icons'; import Modal from 'components/Modal'; import CollectionPermissions from 'scenes/CollectionPermissions'; import getDataTransferFiles from 'utils/getDataTransferFiles'; import importFile from 'utils/importFile'; import Collection from 'models/Collection'; import UiStore from 'stores/UiStore'; import DocumentsStore from 'stores/DocumentsStore'; import { DropdownMenu, DropdownMenuItem } from 'components/DropdownMenu'; type Props = { label?: React.Node, onOpen?: () => *, onClose?: () => *, ui: UiStore, documents: DocumentsStore, collection: Collection, }; @observer class CollectionMenu extends React.Component { file: ?HTMLInputElement; @observable permissionsModalOpen: boolean = false; @observable redirectTo: ?string; componentDidUpdate() { this.redirectTo = undefined; } onNewDocument = (ev: SyntheticEvent<*>) => { ev.preventDefault(); const { collection } = this.props; this.redirectTo = `${collection.url}/new`; }; onImportDocument = (ev: SyntheticEvent<*>) => { ev.preventDefault(); // simulate a click on the file upload input element if (this.file) this.file.click(); }; onFilePicked = async (ev: SyntheticEvent<*>) => { const files = getDataTransferFiles(ev); try { const document = await importFile({ file: files[0], documents: this.props.documents, collectionId: this.props.collection.id, }); this.redirectTo = document.url; } catch (err) { this.props.ui.showToast(err.message); } }; onEdit = (ev: SyntheticEvent<*>) => { ev.preventDefault(); const { collection } = this.props; this.props.ui.setActiveModal('collection-edit', { collection }); }; onDelete = (ev: SyntheticEvent<*>) => { ev.preventDefault(); const { collection } = this.props; this.props.ui.setActiveModal('collection-delete', { collection }); }; onExport = (ev: SyntheticEvent<*>) => { ev.preventDefault(); const { collection } = this.props; this.props.ui.setActiveModal('collection-export', { collection }); }; onPermissions = (ev: SyntheticEvent<*>) => { ev.preventDefault(); this.permissionsModalOpen = true; }; handlePermissionsModalClose = () => { this.permissionsModalOpen = false; }; render() { if (this.redirectTo) return ; const { collection, label, onOpen, onClose } = this.props; return ( (this.file = ref)} onChange={this.onFilePicked} accept="text/markdown, text/plain" /> } onOpen={onOpen} onClose={onClose} > {collection && ( New document Import document
Edit… Permissions… Export…
)} Delete…
); } } const HiddenInput = styled.input` position: absolute; top: -100px; left: -100px; visibility: hidden; `; export default inject('ui', 'documents')(CollectionMenu);