2017-05-12 00:23:56 +00:00
|
|
|
|
// @flow
|
|
|
|
|
import React from 'react';
|
2017-06-01 03:23:09 +00:00
|
|
|
|
import ReactDOM from 'react-dom';
|
2017-07-16 16:24:45 +00:00
|
|
|
|
import keydown from 'react-keydown';
|
2017-09-13 06:30:18 +00:00
|
|
|
|
import { observable, action } from 'mobx';
|
2017-09-04 21:48:56 +00:00
|
|
|
|
import { observer, inject } from 'mobx-react';
|
2016-08-12 12:56:39 +00:00
|
|
|
|
import _ from 'lodash';
|
2017-09-04 21:48:56 +00:00
|
|
|
|
import DocumentsStore from 'stores/DocumentsStore';
|
|
|
|
|
|
2017-10-31 07:03:17 +00:00
|
|
|
|
import { withRouter } from 'react-router-dom';
|
2017-05-22 06:02:53 +00:00
|
|
|
|
import { searchUrl } from 'utils/routeHelpers';
|
2017-06-01 03:23:09 +00:00
|
|
|
|
import styled from 'styled-components';
|
|
|
|
|
import ArrowKeyNavigation from 'boundless-arrow-key-navigation';
|
2016-07-19 07:30:47 +00:00
|
|
|
|
|
2017-10-18 06:21:22 +00:00
|
|
|
|
import Empty from 'components/Empty';
|
2017-10-29 22:02:24 +00:00
|
|
|
|
import Flex from 'shared/components/Flex';
|
2017-07-12 08:05:28 +00:00
|
|
|
|
import CenteredContent from 'components/CenteredContent';
|
2017-07-16 16:24:45 +00:00
|
|
|
|
import LoadingIndicator from 'components/LoadingIndicator';
|
2017-05-10 07:02:11 +00:00
|
|
|
|
import SearchField from './components/SearchField';
|
2016-07-18 03:59:32 +00:00
|
|
|
|
|
2017-05-10 07:02:11 +00:00
|
|
|
|
import DocumentPreview from 'components/DocumentPreview';
|
2017-05-26 19:56:10 +00:00
|
|
|
|
import PageTitle from 'components/PageTitle';
|
2017-05-10 07:02:11 +00:00
|
|
|
|
|
2017-05-12 00:23:56 +00:00
|
|
|
|
type Props = {
|
2017-05-22 06:02:53 +00:00
|
|
|
|
history: Object,
|
|
|
|
|
match: Object,
|
2017-09-04 21:48:56 +00:00
|
|
|
|
documents: DocumentsStore,
|
2017-05-17 07:11:13 +00:00
|
|
|
|
notFound: ?boolean,
|
2017-05-12 00:23:56 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-06-01 03:23:09 +00:00
|
|
|
|
const Container = styled(CenteredContent)`
|
2017-07-12 08:11:34 +00:00
|
|
|
|
> div {
|
|
|
|
|
position: relative;
|
2017-07-13 06:37:46 +00:00
|
|
|
|
height: 100%;
|
2017-07-12 08:11:34 +00:00
|
|
|
|
}
|
2017-06-01 03:23:09 +00:00
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const ResultsWrapper = styled(Flex)`
|
|
|
|
|
position: absolute;
|
2017-07-02 00:16:18 +00:00
|
|
|
|
transition: all 300ms cubic-bezier(0.65, 0.05, 0.36, 1);
|
2017-06-01 03:23:09 +00:00
|
|
|
|
top: ${props => (props.pinToTop ? '0%' : '50%')};
|
|
|
|
|
margin-top: ${props => (props.pinToTop ? '40px' : '-75px')};
|
|
|
|
|
width: 100%;
|
|
|
|
|
`;
|
|
|
|
|
|
2017-07-02 00:16:18 +00:00
|
|
|
|
const ResultList = styled(Flex)`
|
|
|
|
|
opacity: ${props => (props.visible ? '1' : '0')};
|
|
|
|
|
transition: all 400ms cubic-bezier(0.65, 0.05, 0.36, 1);
|
|
|
|
|
`;
|
|
|
|
|
|
2017-07-12 08:05:28 +00:00
|
|
|
|
const StyledArrowKeyNavigation = styled(ArrowKeyNavigation)`
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
flex: 1;
|
|
|
|
|
`;
|
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
|
@observer
|
|
|
|
|
class Search extends React.Component {
|
2017-06-01 03:23:09 +00:00
|
|
|
|
firstDocument: HTMLElement;
|
2017-05-12 00:23:56 +00:00
|
|
|
|
props: Props;
|
2016-07-19 07:30:47 +00:00
|
|
|
|
|
2017-10-18 06:21:22 +00:00
|
|
|
|
@observable resultIds: string[] = []; // Document IDs
|
2017-09-04 21:48:56 +00:00
|
|
|
|
@observable searchTerm: ?string = null;
|
|
|
|
|
@observable isFetching = false;
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
2017-05-22 06:02:53 +00:00
|
|
|
|
this.updateSearchResults();
|
2016-07-19 07:30:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-22 06:02:53 +00:00
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
|
if (prevProps.match.params.query !== this.props.match.params.query) {
|
|
|
|
|
this.updateSearchResults();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-16 16:24:45 +00:00
|
|
|
|
@keydown('esc')
|
|
|
|
|
goBack() {
|
|
|
|
|
this.props.history.goBack();
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-22 06:02:53 +00:00
|
|
|
|
handleKeyDown = ev => {
|
2017-07-16 16:24:45 +00:00
|
|
|
|
// Escape
|
2017-05-22 06:02:53 +00:00
|
|
|
|
if (ev.which === 27) {
|
|
|
|
|
ev.preventDefault();
|
2017-07-16 16:24:45 +00:00
|
|
|
|
this.goBack();
|
2017-05-22 06:02:53 +00:00
|
|
|
|
}
|
2017-07-16 16:24:45 +00:00
|
|
|
|
|
2017-06-01 03:23:09 +00:00
|
|
|
|
// Down
|
|
|
|
|
if (ev.which === 40) {
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
if (this.firstDocument) {
|
|
|
|
|
const element = ReactDOM.findDOMNode(this.firstDocument);
|
|
|
|
|
// $FlowFixMe
|
|
|
|
|
if (element && element.focus) element.focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-22 06:02:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
updateSearchResults = _.debounce(() => {
|
2017-09-04 21:48:56 +00:00
|
|
|
|
this.search(this.props.match.params.query);
|
2017-05-22 06:02:53 +00:00
|
|
|
|
}, 250);
|
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
|
@action
|
|
|
|
|
search = async (query: string) => {
|
2017-09-04 21:48:56 +00:00
|
|
|
|
this.searchTerm = query;
|
|
|
|
|
this.isFetching = true;
|
|
|
|
|
|
|
|
|
|
if (query) {
|
|
|
|
|
try {
|
2017-09-13 06:30:18 +00:00
|
|
|
|
this.resultIds = await this.props.documents.search(query);
|
2017-09-04 21:48:56 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Something went wrong');
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.resultIds = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.isFetching = false;
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-22 06:02:53 +00:00
|
|
|
|
updateQuery = query => {
|
|
|
|
|
this.props.history.replace(searchUrl(query));
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-01 03:23:09 +00:00
|
|
|
|
setFirstDocumentRef = ref => {
|
|
|
|
|
this.firstDocument = ref;
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-16 16:24:45 +00:00
|
|
|
|
get title() {
|
2017-09-04 21:48:56 +00:00
|
|
|
|
const query = this.searchTerm;
|
2017-07-16 16:24:45 +00:00
|
|
|
|
const title = 'Search';
|
|
|
|
|
if (query) return `${query} - ${title}`;
|
|
|
|
|
return title;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-18 03:59:32 +00:00
|
|
|
|
render() {
|
2017-10-18 06:21:22 +00:00
|
|
|
|
const { documents, notFound } = this.props;
|
2017-05-26 04:59:35 +00:00
|
|
|
|
const query = this.props.match.params.query;
|
2017-09-04 21:48:56 +00:00
|
|
|
|
const hasResults = this.resultIds.length > 0;
|
2017-10-18 06:21:22 +00:00
|
|
|
|
const showEmpty = !this.isFetching && this.searchTerm && !hasResults;
|
2016-07-19 07:30:47 +00:00
|
|
|
|
|
2016-07-18 03:59:32 +00:00
|
|
|
|
return (
|
2017-06-25 00:14:36 +00:00
|
|
|
|
<Container auto>
|
2017-07-16 16:24:45 +00:00
|
|
|
|
<PageTitle title={this.title} />
|
2017-09-04 21:48:56 +00:00
|
|
|
|
{this.isFetching && <LoadingIndicator />}
|
2017-11-10 22:14:30 +00:00
|
|
|
|
{notFound && (
|
2017-06-25 00:14:36 +00:00
|
|
|
|
<div>
|
|
|
|
|
<h1>Not Found</h1>
|
2017-10-18 06:21:22 +00:00
|
|
|
|
<p>We’re unable to find the page you’re accessing.</p>
|
2017-11-10 22:14:30 +00:00
|
|
|
|
</div>
|
|
|
|
|
)}
|
2017-06-25 00:14:36 +00:00
|
|
|
|
<ResultsWrapper pinToTop={hasResults} column auto>
|
|
|
|
|
<SearchField
|
2017-09-04 21:48:56 +00:00
|
|
|
|
searchTerm={this.searchTerm}
|
2017-06-25 00:14:36 +00:00
|
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
|
onChange={this.updateQuery}
|
|
|
|
|
value={query || ''}
|
|
|
|
|
/>
|
2017-10-18 06:21:22 +00:00
|
|
|
|
{showEmpty && <Empty>Oop, no matching documents.</Empty>}
|
2017-07-02 00:16:18 +00:00
|
|
|
|
<ResultList visible={hasResults}>
|
2017-07-12 08:05:28 +00:00
|
|
|
|
<StyledArrowKeyNavigation
|
2017-07-02 00:16:18 +00:00
|
|
|
|
mode={ArrowKeyNavigation.mode.VERTICAL}
|
|
|
|
|
defaultActiveChildIndex={0}
|
|
|
|
|
>
|
2017-09-04 21:48:56 +00:00
|
|
|
|
{this.resultIds.map((documentId, index) => {
|
|
|
|
|
const document = documents.getById(documentId);
|
2017-09-23 22:58:34 +00:00
|
|
|
|
if (!document) return null;
|
|
|
|
|
return (
|
|
|
|
|
<DocumentPreview
|
|
|
|
|
innerRef={ref =>
|
2017-11-10 22:14:30 +00:00
|
|
|
|
index === 0 && this.setFirstDocumentRef(ref)
|
|
|
|
|
}
|
2017-09-23 22:58:34 +00:00
|
|
|
|
key={documentId}
|
|
|
|
|
document={document}
|
|
|
|
|
highlight={this.searchTerm}
|
|
|
|
|
showCollection
|
|
|
|
|
/>
|
|
|
|
|
);
|
2017-09-04 21:48:56 +00:00
|
|
|
|
})}
|
2017-07-12 08:05:28 +00:00
|
|
|
|
</StyledArrowKeyNavigation>
|
2017-07-02 00:16:18 +00:00
|
|
|
|
</ResultList>
|
2017-06-25 00:14:36 +00:00
|
|
|
|
</ResultsWrapper>
|
|
|
|
|
</Container>
|
2016-07-18 03:59:32 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-04 21:48:56 +00:00
|
|
|
|
export default withRouter(inject('documents')(Search));
|