// @flow
import React from 'react';
import { observable } from 'mobx';
import { observer, inject } from 'mobx-react';
import Flex from 'components/Flex';
import styled from 'styled-components';
import { color, fontWeight } from 'styles/constants';
import SidebarLink from './SidebarLink';
import DropToImport from 'components/DropToImport';
import PlusIcon from 'components/Icon/PlusIcon';
import CollectionIcon from 'components/Icon/CollectionIcon';
import CollectionMenu from 'menus/CollectionMenu';
import CollectionsStore from 'stores/CollectionsStore';
import UiStore from 'stores/UiStore';
import Document from 'models/Document';
import DocumentsStore from 'stores/DocumentsStore';
import { type NavigationNode } from 'types';
type Props = {
history: Object,
collections: CollectionsStore,
documents: DocumentsStore,
activeDocument: ?Document,
onCreateCollection: () => void,
activeDocumentRef: HTMLElement => void,
ui: UiStore,
};
@observer class SidebarCollections extends React.PureComponent {
props: Props;
render() {
const {
history,
collections,
activeDocument,
ui,
activeDocumentRef,
documents,
} = this.props;
return (
{collections.orderedData.map(collection => (
))}
{collections.isLoaded &&
}
>
New collection…
}
);
}
}
@observer class CollectionLink extends React.Component {
dropzoneRef;
@observable menuOpen = false;
handleImport = () => {
this.dropzoneRef.open();
};
render() {
const {
history,
collection,
activeDocument,
ui,
activeDocumentRef,
prefetchDocument,
} = this.props;
const expanded = collection.id === ui.activeCollectionId;
return (
(this.dropzoneRef = ref)}
>
}
>
{collection.name}
(this.menuOpen = true)}
onClose={() => (this.menuOpen = false)}
onImport={this.handleImport}
open={this.menuOpen}
/>
{expanded &&
{collection.documents.map(document => (
))}
}
);
}
}
type DocumentLinkProps = {
document: NavigationNode,
history: Object,
activeDocument: ?Document,
activeDocumentRef: HTMLElement => void,
prefetchDocument: (documentId: string) => void,
depth: number,
};
const DocumentLink = observer(
({
document,
activeDocument,
activeDocumentRef,
prefetchDocument,
depth,
}: DocumentLinkProps) => {
const isActiveDocument =
activeDocument && activeDocument.id === document.id;
const showChildren = !!(activeDocument &&
(activeDocument.pathToDocument
.map(entry => entry.id)
.includes(document.id) ||
isActiveDocument));
const handleMouseEnter = (event: SyntheticEvent) => {
event.stopPropagation();
event.preventDefault();
prefetchDocument(document.id);
};
return (
{document.children.map(childDocument => (
))}
: undefined
}
>
{document.title}
);
}
);
const CollectionAction = styled.a`
position: absolute;
right: 0;
color: ${color.slate};
svg { opacity: .75; }
&:hover {
svg { opacity: 1; }
}
`;
const StyledDropToImport = styled(DropToImport)`
${CollectionAction} {
display: ${props => (props.menuOpen ? 'inline' : 'none')};
}
&:hover {
${CollectionAction} {
display: inline;
}
}
`;
const Header = styled(Flex)`
font-size: 12px;
font-weight: ${fontWeight.semiBold};
text-transform: uppercase;
color: ${color.slate};
letter-spacing: 0.04em;
margin-bottom: 4px;
`;
const Children = styled(Flex)`
margin-left: 20px;
`;
export default inject('collections', 'ui', 'documents')(SidebarCollections);