Files
outline/app/scenes/Search/Search.js
Tom Moor 8cbcb77486 Base model refactor (#810)
* Big upgrades

* WIP: Stash

* Stash, 30 flow errors left

* Downgrade mobx

* WIP

* When I understand the difference between class and instance methods

* 💚

* Fixes: File import
Model saving edge cases
pinning and starring docs
Collection editing
Upgrade mobx devtools

* Notification settings saving works

* Disabled settings

* Document mailer

* Working notifications

* Colletion created notification
Ensure not notified for own actions

* Tidy up

* Document updated event only for document creation
Add indexes
Notification setting on user creation

* Commentary

* Fixed: Notification setting on signup

* Fix document move / duplicate stale data
Add BaseModel.refresh method

* Fixes: Title in sidebar not updated after editing document

* 💚

* Improve / restore error handling
Better handle offline errors

* 👕
2018-12-04 22:24:30 -08:00

228 lines
6.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @flow
import * as React from 'react';
import ReactDOM from 'react-dom';
import keydown from 'react-keydown';
import Waypoint from 'react-waypoint';
import { observable, action } from 'mobx';
import { observer, inject } from 'mobx-react';
import { debounce } from 'lodash';
import { withRouter } from 'react-router-dom';
import styled from 'styled-components';
import ArrowKeyNavigation from 'boundless-arrow-key-navigation';
import type { SearchResult } from 'types';
import { DEFAULT_PAGINATION_LIMIT } from 'stores/BaseStore';
import DocumentsStore from 'stores/DocumentsStore';
import { searchUrl } from 'utils/routeHelpers';
import Flex from 'shared/components/Flex';
import Empty from 'components/Empty';
import CenteredContent from 'components/CenteredContent';
import LoadingIndicator from 'components/LoadingIndicator';
import DocumentPreview from 'components/DocumentPreview';
import PageTitle from 'components/PageTitle';
import SearchField from './components/SearchField';
type Props = {
history: Object,
match: Object,
documents: DocumentsStore,
notFound: ?boolean,
};
const Container = styled(CenteredContent)`
> div {
position: relative;
height: 100%;
}
`;
const ResultsWrapper = styled(Flex)`
position: absolute;
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%;
`;
const ResultList = styled(Flex)`
margin-bottom: 150px;
opacity: ${props => (props.visible ? '1' : '0')};
transition: all 400ms cubic-bezier(0.65, 0.05, 0.36, 1);
`;
const StyledArrowKeyNavigation = styled(ArrowKeyNavigation)`
display: flex;
flex-direction: column;
flex: 1;
`;
@observer
class Search extends React.Component<Props> {
firstDocument: ?DocumentPreview;
@observable results: SearchResult[] = [];
@observable query: string = '';
@observable offset: number = 0;
@observable allowLoadMore: boolean = true;
@observable isFetching: boolean = false;
@observable pinToTop: boolean = false;
componentDidMount() {
this.handleQueryChange();
}
componentDidUpdate(prevProps) {
if (prevProps.match.params.query !== this.props.match.params.query) {
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);
if (element instanceof HTMLElement) element.focus();
}
}
};
handleQueryChange = () => {
const query = this.props.match.params.query;
this.query = query ? query : '';
this.results = [];
this.offset = 0;
this.allowLoadMore = true;
// To prevent "no results" showing before debounce kicks in
if (this.query) this.isFetching = true;
this.fetchResultsDebounced();
};
@action
loadMoreResults = async () => {
// Don't paginate if there aren't more results or were in the middle of fetching
if (!this.allowLoadMore || this.isFetching) return;
// Fetch more results
await this.fetchResults();
};
@action
fetchResults = async () => {
this.isFetching = true;
if (this.query) {
try {
const results = await this.props.documents.search(this.query, {
offset: this.offset,
limit: DEFAULT_PAGINATION_LIMIT,
});
this.results = this.results.concat(results);
if (this.results.length > 0) this.pinToTop = true;
if (results.length === 0 || results.length < DEFAULT_PAGINATION_LIMIT) {
this.allowLoadMore = false;
} else {
this.offset += DEFAULT_PAGINATION_LIMIT;
}
} catch (e) {
console.error('Something went wrong');
}
} else {
this.results = [];
this.pinToTop = false;
}
this.isFetching = false;
};
fetchResultsDebounced = debounce(this.fetchResults, 350, {
leading: false,
trailing: true,
});
updateLocation = query => {
this.props.history.replace(searchUrl(query));
};
setFirstDocumentRef = ref => {
this.firstDocument = ref;
};
get title() {
const query = this.query;
const title = 'Search';
if (query) return `${query} - ${title}`;
return title;
}
render() {
const { documents, notFound } = this.props;
const showEmpty =
!this.isFetching && this.query && this.results.length === 0;
return (
<Container auto>
<PageTitle title={this.title} />
{this.isFetching && <LoadingIndicator />}
{notFound && (
<div>
<h1>Not Found</h1>
<Empty>We were unable to find the page youre looking for.</Empty>
</div>
)}
<ResultsWrapper pinToTop={this.pinToTop} column auto>
<SearchField
onKeyDown={this.handleKeyDown}
onChange={this.updateLocation}
defaultValue={this.query}
/>
{showEmpty && <Empty>No matching documents.</Empty>}
<ResultList column visible={this.pinToTop}>
<StyledArrowKeyNavigation
mode={ArrowKeyNavigation.mode.VERTICAL}
defaultActiveChildIndex={0}
>
{this.results.map((result, index) => {
const document = documents.data.get(result.document.id);
if (!document) return null;
return (
<DocumentPreview
ref={ref => index === 0 && this.setFirstDocumentRef(ref)}
key={document.id}
document={document}
highlight={this.query}
context={result.context}
showCollection
/>
);
})}
</StyledArrowKeyNavigation>
{this.allowLoadMore && (
<Waypoint key={this.offset} onEnter={this.loadMoreResults} />
)}
</ResultList>
</ResultsWrapper>
</Container>
);
}
}
export default withRouter(inject('documents')(Search));