// @flow import React from 'react'; import ReactDOM from 'react-dom'; import keydown from 'react-keydown'; import { observer } from 'mobx-react'; import _ from 'lodash'; import Flex from 'components/Flex'; import { withRouter } from 'react-router'; import { searchUrl } from 'utils/routeHelpers'; import styled from 'styled-components'; import ArrowKeyNavigation from 'boundless-arrow-key-navigation'; import CenteredContent from 'components/CenteredContent'; import LoadingIndicator from 'components/LoadingIndicator'; import SearchField from './components/SearchField'; import SearchStore from './SearchStore'; import DocumentPreview from 'components/DocumentPreview'; import PageTitle from 'components/PageTitle'; type Props = { history: Object, match: Object, 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)` 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 { firstDocument: HTMLElement; props: Props; store: SearchStore; constructor(props: Props) { super(props); this.store = new SearchStore(); this.updateSearchResults(); } 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(() => { this.store.search(this.props.match.params.query); }, 250); updateQuery = query => { this.props.history.replace(searchUrl(query)); }; setFirstDocumentRef = ref => { this.firstDocument = ref; }; get title() { const query = this.store.searchTerm; const title = 'Search'; if (query) return `${query} - ${title}`; return title; } render() { const query = this.props.match.params.query; const hasResults = this.store.documents.length > 0; return ( {this.store.isFetching && } {this.props.notFound &&

Not Found

We're unable to find the page you're accessing.


} {this.store.documents.map((document, index) => ( index === 0 && this.setFirstDocumentRef(ref)} key={document.id} document={document} highlight={this.store.searchTerm} showCollection /> ))}
); } } export default withRouter(Search);