// @flow import * as React from "react"; import { observable } from "mobx"; import { inject, observer } from "mobx-react"; import { Redirect } from "react-router-dom"; import { PlusIcon } from "outline-icons"; import { newDocumentUrl } from "utils/routeHelpers"; import CollectionsStore from "stores/CollectionsStore"; import DocumentsStore from "stores/DocumentsStore"; import PoliciesStore from "stores/PoliciesStore"; import { DropdownMenu, DropdownMenuItem, Header, } from "components/DropdownMenu"; import Button from "components/Button"; import CollectionIcon from "components/CollectionIcon"; type Props = { label?: React.Node, documents: DocumentsStore, collections: CollectionsStore, policies: PoliciesStore, }; @observer class NewDocumentMenu extends React.Component { @observable redirectTo: ?string; componentDidUpdate() { this.redirectTo = undefined; } handleNewDocument = (collectionId: string, options) => { this.redirectTo = newDocumentUrl(collectionId, options); }; onOpen = () => { const { collections } = this.props; if (collections.orderedData.length === 1) { this.handleNewDocument(collections.orderedData[0].id); } }; render() { if (this.redirectTo) return ; const { collections, documents, policies, label, ...rest } = this.props; const singleCollection = collections.orderedData.length === 1; return ( } small> New doc{singleCollection ? "" : "…"} ) } onOpen={this.onOpen} {...rest} >
Choose a collection
{collections.orderedData.map((collection) => { const can = policies.abilities(collection.id); return ( this.handleNewDocument(collection.id)} disabled={!can.update} >  {collection.name} ); })}
); } } export default inject("collections", "documents", "policies")(NewDocumentMenu);