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

227 lines
6.0 KiB
JavaScript
Raw Normal View History

2017-05-12 00:23:56 +00:00
// @flow
2017-11-12 18:55:13 +00:00
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import keydown from 'react-keydown';
2017-12-04 00:50:50 +00:00
import Waypoint from 'react-waypoint';
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-12-04 00:50:50 +00:00
import DocumentsStore, {
DEFAULT_PAGINATION_LIMIT,
} from 'stores/DocumentsStore';
2017-09-04 21:48:56 +00:00
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)`
2017-12-04 00:50:50 +00:00
margin-bottom: 150px;
2017-07-02 00:16:18 +00:00
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
2017-11-12 18:55:13 +00:00
class Search extends 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-11-12 18:55:13 +00:00
@observable query: string = '';
2017-12-04 00:50:50 +00:00
@observable offset: number = 0;
@observable allowLoadMore: boolean = true;
2017-09-04 21:48:56 +00:00
@observable isFetching = false;
componentDidMount() {
2017-11-12 18:55:13 +00:00
this.handleQueryChange();
2016-07-19 07:30:47 +00:00
}
componentDidUpdate(prevProps) {
if (prevProps.match.params.query !== this.props.match.params.query) {
2017-11-12 18:55:13 +00:00
this.handleQueryChange();
}
}
@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);
2017-11-12 18:55:13 +00:00
if (element instanceof HTMLElement) element.focus();
}
}
};
2017-11-12 18:55:13 +00:00
handleQueryChange = () => {
const query = this.props.match.params.query;
this.query = query ? decodeURIComponent(query) : '';
2017-12-04 00:50:50 +00:00
this.allowLoadMore = true;
// To prevent "no results" showing before debounce kicks in
if (this.query) this.isFetching = true;
2017-11-12 18:55:13 +00:00
this.fetchResultsDebounced();
};
2017-12-04 00:50:50 +00:00
fetchResultsDebounced = _.debounce(this.fetchResults, 350, {
leading: false,
trailing: true,
});
@action
loadMoreResults = async () => {
// Don't paginate if there aren't more results or we're in the middle of fetching
if (!this.allowLoadMore || this.isFetching) return;
// Fetch more results
await this.fetchResults();
};
2017-11-10 22:14:30 +00:00
@action
2017-11-12 18:55:13 +00:00
fetchResults = async () => {
2017-09-04 21:48:56 +00:00
this.isFetching = true;
2017-11-12 18:55:13 +00:00
if (this.query) {
2017-09-04 21:48:56 +00:00
try {
2017-12-04 00:50:50 +00:00
const newResults = await this.props.documents.search(this.query, {
offset: this.offset,
limit: DEFAULT_PAGINATION_LIMIT,
});
this.resultIds = this.resultIds.concat(newResults);
if (
newResults.length === 0 ||
newResults.length < DEFAULT_PAGINATION_LIMIT
) {
this.allowLoadMore = false;
} else {
this.offset += DEFAULT_PAGINATION_LIMIT;
}
2017-09-04 21:48:56 +00:00
} catch (e) {
console.error('Something went wrong');
}
} else {
this.resultIds = [];
}
this.isFetching = false;
};
2017-11-12 18:55:13 +00:00
updateLocation = query => {
this.props.history.replace(searchUrl(query));
};
setFirstDocumentRef = ref => {
this.firstDocument = ref;
};
get title() {
2017-11-12 18:55:13 +00:00
const query = this.query;
const title = 'Search';
if (query) return `${query} - ${title}`;
return title;
}
2016-07-18 03:59:32 +00:00
render() {
const { documents, notFound } = this.props;
2017-09-04 21:48:56 +00:00
const hasResults = this.resultIds.length > 0;
2017-11-12 18:55:13 +00:00
const showEmpty = !this.isFetching && this.query && !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
onKeyDown={this.handleKeyDown}
2017-11-12 18:55:13 +00:00
onChange={this.updateLocation}
value={this.query}
/>
2017-11-12 18:55:13 +00:00
{showEmpty && <Empty>No matching documents.</Empty>}
2017-12-04 00:50:50 +00:00
<ResultList column 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}
2017-11-12 18:55:13 +00:00
highlight={this.query}
showCollection
/>
);
2017-09-04 21:48:56 +00:00
})}
2017-07-12 08:05:28 +00:00
</StyledArrowKeyNavigation>
2017-12-04 00:50:50 +00:00
{this.allowLoadMore && (
<Waypoint key={this.offset} onEnter={this.loadMoreResults} />
)}
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));