This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/app/scenes/Search/Search.js

189 lines
4.9 KiB
JavaScript
Raw Normal View History

2017-05-12 00:23:56 +00:00
// @flow
import React from 'react';
import ReactDOM from 'react-dom';
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';
import { searchUrl } from 'utils/routeHelpers';
import styled from 'styled-components';
import ArrowKeyNavigation from 'boundless-arrow-key-navigation';
2016-07-19 07:30:47 +00:00
import Empty from 'components/Empty';
import Flex from 'shared/components/Flex';
2017-07-12 08:05:28 +00:00
import CenteredContent from 'components/CenteredContent';
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';
import PageTitle from 'components/PageTitle';
2017-05-10 07:02:11 +00:00
2017-05-12 00:23:56 +00:00
type Props = {
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
};
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
}
`;
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);
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 {
firstDocument: HTMLElement;
2017-05-12 00:23:56 +00:00
props: Props;
2016-07-19 07:30:47 +00:00
@observable resultIds: string[] = []; // Document IDs
2017-09-04 21:48:56 +00:00
@observable searchTerm: ?string = null;
@observable isFetching = false;
componentDidMount() {
this.updateSearchResults();
2016-07-19 07:30:47 +00:00
}
componentDidUpdate(prevProps) {
if (prevProps.match.params.query !== this.props.match.params.query) {
this.updateSearchResults();
}
}
@keydown('esc')
goBack() {
this.props.history.goBack();
}
handleKeyDown = ev => {
// Escape
if (ev.which === 27) {
ev.preventDefault();
this.goBack();
}
// Down
if (ev.which === 40) {
ev.preventDefault();
if (this.firstDocument) {
const element = ReactDOM.findDOMNode(this.firstDocument);
// $FlowFixMe
if (element && element.focus) element.focus();
}
}
};
updateSearchResults = _.debounce(() => {
2017-09-04 21:48:56 +00:00
this.search(this.props.match.params.query);
}, 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;
};
updateQuery = query => {
this.props.history.replace(searchUrl(query));
};
setFirstDocumentRef = ref => {
this.firstDocument = ref;
};
get title() {
2017-09-04 21:48:56 +00:00
const query = this.searchTerm;
const title = 'Search';
if (query) return `${query} - ${title}`;
return title;
}
2016-07-18 03:59:32 +00:00
render() {
const { documents, notFound } = this.props;
const query = this.props.match.params.query;
2017-09-04 21:48:56 +00:00
const hasResults = this.resultIds.length > 0;
const showEmpty = !this.isFetching && this.searchTerm && !hasResults;
2016-07-19 07:30:47 +00:00
2016-07-18 03:59:32 +00:00
return (
<Container auto>
<PageTitle title={this.title} />
2017-09-04 21:48:56 +00:00
{this.isFetching && <LoadingIndicator />}
2017-11-10 22:14:30 +00:00
{notFound && (
<div>
<h1>Not Found</h1>
<p>Were unable to find the page youre accessing.</p>
2017-11-10 22:14:30 +00:00
</div>
)}
<ResultsWrapper pinToTop={hasResults} column auto>
<SearchField
2017-09-04 21:48:56 +00:00
searchTerm={this.searchTerm}
onKeyDown={this.handleKeyDown}
onChange={this.updateQuery}
value={query || ''}
/>
{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);
if (!document) return null;
return (
<DocumentPreview
innerRef={ref =>
2017-11-10 22:14:30 +00:00
index === 0 && this.setFirstDocumentRef(ref)
}
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>
</ResultsWrapper>
</Container>
2016-07-18 03:59:32 +00:00
);
}
}
2017-09-04 21:48:56 +00:00
export default withRouter(inject('documents')(Search));