2017-09-10 21:28:37 +00:00
|
|
|
// @flow
|
2018-05-05 23:16:08 +00:00
|
|
|
import * as React from 'react';
|
2017-09-10 21:28:37 +00:00
|
|
|
import { observer } from 'mobx-react';
|
|
|
|
import invariant from 'invariant';
|
|
|
|
import styled from 'styled-components';
|
2018-05-03 04:51:39 +00:00
|
|
|
import { GoToIcon } from 'outline-icons';
|
2017-10-29 22:02:24 +00:00
|
|
|
import Flex from 'shared/components/Flex';
|
2017-09-10 21:28:37 +00:00
|
|
|
|
2017-10-02 06:42:17 +00:00
|
|
|
import Document from 'models/Document';
|
|
|
|
|
2017-09-13 06:30:18 +00:00
|
|
|
const ResultWrapper = styled.div`
|
2017-09-24 21:55:03 +00:00
|
|
|
display: flex;
|
|
|
|
margin-bottom: 10px;
|
2017-09-13 06:30:18 +00:00
|
|
|
|
2018-06-10 02:10:30 +00:00
|
|
|
color: ${props => props.theme.text};
|
2017-09-24 21:55:03 +00:00
|
|
|
cursor: default;
|
|
|
|
`;
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
const StyledGoToIcon = styled(GoToIcon)``;
|
2017-09-13 06:30:18 +00:00
|
|
|
|
2018-11-07 05:58:32 +00:00
|
|
|
const ResultWrapperLink = styled(ResultWrapper.withComponent('a'))`
|
2017-09-24 21:55:03 +00:00
|
|
|
height: 32px;
|
|
|
|
padding-top: 3px;
|
|
|
|
padding-left: 5px;
|
|
|
|
|
|
|
|
&:hover,
|
|
|
|
&:active,
|
|
|
|
&:focus {
|
|
|
|
margin-left: 0px;
|
|
|
|
border-radius: 2px;
|
2018-06-10 02:10:30 +00:00
|
|
|
background: ${props => props.theme.black};
|
|
|
|
color: ${props => props.theme.smokeLight};
|
2017-09-24 21:55:03 +00:00
|
|
|
outline: none;
|
|
|
|
cursor: pointer;
|
|
|
|
|
2017-10-21 21:05:20 +00:00
|
|
|
${StyledGoToIcon} {
|
2018-06-10 02:10:30 +00:00
|
|
|
fill: ${props => props.theme.white};
|
2017-09-24 21:55:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 06:30:18 +00:00
|
|
|
`;
|
|
|
|
|
2017-09-10 21:28:37 +00:00
|
|
|
type Props = {
|
2017-10-02 01:36:44 +00:00
|
|
|
result: Object,
|
2017-10-02 06:42:17 +00:00
|
|
|
document?: Document,
|
|
|
|
onSuccess?: Function,
|
2017-09-10 21:28:37 +00:00
|
|
|
ref?: Function,
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@observer
|
2018-05-05 23:16:08 +00:00
|
|
|
class PathToDocument extends React.Component<Props> {
|
|
|
|
handleClick = async (ev: SyntheticEvent<*>) => {
|
2017-10-02 06:42:17 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
const { document, result, onSuccess } = this.props;
|
|
|
|
|
|
|
|
invariant(onSuccess && document, 'onSuccess unavailable');
|
|
|
|
|
|
|
|
if (result.type === 'document') {
|
|
|
|
await document.move(result.id);
|
|
|
|
} else if (
|
|
|
|
result.type === 'collection' &&
|
|
|
|
result.id === document.collection.id
|
|
|
|
) {
|
|
|
|
await document.move(null);
|
|
|
|
} else {
|
|
|
|
throw new Error('Not implemented yet');
|
|
|
|
}
|
|
|
|
onSuccess();
|
|
|
|
};
|
|
|
|
|
2017-09-10 21:28:37 +00:00
|
|
|
render() {
|
2017-10-02 06:42:17 +00:00
|
|
|
const { result, document, ref } = this.props;
|
|
|
|
const Component = document ? ResultWrapperLink : ResultWrapper;
|
2017-10-02 01:36:44 +00:00
|
|
|
|
2017-10-02 06:42:17 +00:00
|
|
|
if (!result) return <div />;
|
2017-09-10 21:28:37 +00:00
|
|
|
|
|
|
|
return (
|
2018-11-07 05:58:32 +00:00
|
|
|
<Component ref={ref} onClick={this.handleClick} selectable href>
|
2017-10-02 01:36:44 +00:00
|
|
|
{result.path
|
|
|
|
.map(doc => <span key={doc.id}>{doc.title}</span>)
|
2017-10-21 21:05:20 +00:00
|
|
|
.reduce((prev, curr) => [prev, <StyledGoToIcon />, curr])}
|
2017-11-10 22:14:30 +00:00
|
|
|
{document && (
|
2017-09-10 21:28:37 +00:00
|
|
|
<Flex>
|
|
|
|
{' '}
|
2017-11-10 22:14:30 +00:00
|
|
|
<StyledGoToIcon /> {document.title}
|
|
|
|
</Flex>
|
|
|
|
)}
|
2017-09-10 21:28:37 +00:00
|
|
|
</Component>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PathToDocument;
|