2015-10-08 12:27:09 -04:00
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
2015-06-14 23:53:32 -08:00
// See License.txt for license information.
2015-11-19 21:12:56 -05:00
import SearchStore from '../stores/search_store.jsx' ;
import UserStore from '../stores/user_store.jsx' ;
import SearchBox from './search_bar.jsx' ;
import * as Utils from '../utils/utils.jsx' ;
import SearchResultsHeader from './search_results_header.jsx' ;
import SearchResultsItem from './search_results_item.jsx' ;
2015-06-14 23:53:32 -08:00
2016-02-01 17:23:45 -03:00
import { FormattedMessage , FormattedHTMLMessage } from 'mm-intl' ;
2015-09-02 09:45:12 -07:00
function getStateFromStores ( ) {
2015-10-26 15:16:14 -04:00
return { results : SearchStore . getSearchResults ( ) } ;
2015-09-02 09:45:12 -07:00
}
2015-07-13 04:23:24 -07:00
2015-09-02 09:45:12 -07:00
export default class SearchResults extends React . Component {
constructor ( props ) {
super ( props ) ;
2015-06-14 23:53:32 -08:00
2015-09-02 09:45:12 -07:00
this . mounted = false ;
2015-06-14 23:53:32 -08:00
2015-09-02 09:45:12 -07:00
this . onChange = this . onChange . bind ( this ) ;
this . resize = this . resize . bind ( this ) ;
2015-10-16 12:32:19 +03:00
this . handleResize = this . handleResize . bind ( this ) ;
2015-06-14 23:53:32 -08:00
2015-10-16 12:32:19 +03:00
const state = getStateFromStores ( ) ;
state . windowWidth = Utils . windowWidth ( ) ;
state . windowHeight = Utils . windowHeight ( ) ;
this . state = state ;
2015-06-14 23:53:32 -08:00
}
2015-09-02 09:45:12 -07:00
componentDidMount ( ) {
this . mounted = true ;
2015-10-26 15:16:14 -04:00
SearchStore . addSearchChangeListener ( this . onChange ) ;
2015-06-14 23:53:32 -08:00
this . resize ( ) ;
2015-10-16 12:32:19 +03:00
window . addEventListener ( 'resize' , this . handleResize ) ;
2015-09-02 09:45:12 -07:00
}
componentDidUpdate ( ) {
2015-06-14 23:53:32 -08:00
this . resize ( ) ;
2015-09-02 09:45:12 -07:00
}
componentWillUnmount ( ) {
2015-10-26 15:16:14 -04:00
SearchStore . removeSearchChangeListener ( this . onChange ) ;
2015-09-02 09:45:12 -07:00
this . mounted = false ;
2015-10-16 12:32:19 +03:00
window . removeEventListener ( 'resize' , this . handleResize ) ;
}
handleResize ( ) {
this . setState ( {
windowWidth : Utils . windowWidth ( ) ,
windowHeight : Utils . windowHeight ( )
} ) ;
2015-09-02 09:45:12 -07:00
}
onChange ( ) {
if ( this . mounted ) {
2015-06-14 23:53:32 -08:00
var newState = getStateFromStores ( ) ;
2015-11-12 11:19:59 -05:00
if ( ! Utils . areObjectsEqual ( newState , this . state ) ) {
2015-06-14 23:53:32 -08:00
this . setState ( newState ) ;
}
}
2015-09-02 09:45:12 -07:00
}
resize ( ) {
$ ( '#search-items-container' ) . scrollTop ( 0 ) ;
2015-10-16 12:32:19 +03:00
if ( this . state . windowWidth > 768 ) {
2015-10-15 22:54:15 +05:00
$ ( '#search-items-container' ) . perfectScrollbar ( ) ;
}
2015-09-02 09:45:12 -07:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 09:45:12 -07:00
render ( ) {
2015-06-14 23:53:32 -08:00
var results = this . state . results ;
var currentId = UserStore . getCurrentId ( ) ;
2015-09-02 09:45:12 -07:00
var searchForm = null ;
if ( currentId ) {
searchForm = < SearchBox / > ;
}
2015-07-13 04:23:24 -07:00
var noResults = ( ! results || ! results . order || ! results . order . length ) ;
2015-10-26 15:16:14 -04:00
var searchTerm = SearchStore . getSearchTerm ( ) ;
2015-06-14 23:53:32 -08:00
2015-09-02 09:45:12 -07:00
var ctls = null ;
2015-11-18 09:33:08 -05:00
if ( ! searchTerm && noResults ) {
ctls = (
< div className = 'sidebar--right__subheader' >
2016-02-01 17:23:45 -03:00
< FormattedHTMLMessage
id = 'search_results.usage'
defaultMessage = '<ul><li>Use <b>"quotation marks"</b> to search for phrases</li><li>Use <b>from:</b> to find posts from specific users and <b>in:</b> to find posts in specific channels</li></ul>'
/ >
2015-11-18 09:33:08 -05:00
< / div >
) ;
} else if ( noResults ) {
2015-11-03 19:56:46 +05:00
ctls =
(
< div className = 'sidebar--right__subheader' >
2016-02-01 17:23:45 -03:00
< h4 >
< FormattedMessage
id = 'search_results.noResults'
defaultMessage = 'NO RESULTS'
/ >
< / h4 >
< FormattedHTMLMessage
id = 'search_results.because'
defaultMessage = ' < ul >
< li > If you & # 39 ; re searching a partial phrase ( ex . searching "rea" , looking for "reach" or "reaction" ) , append a * to your search term < / li >
< li > Due to the volume of results , two letter searches and common words like "this" , "a" and "is" won & # 39 ; t appear in search results < / li >
< / ul > '
/ >
2015-11-03 19:56:46 +05:00
< / div >
) ;
2015-09-02 09:45:12 -07:00
} else {
ctls = results . order . map ( function mymap ( id ) {
var post = results . posts [ id ] ;
return (
< SearchResultsItem
key = { post . id }
post = { post }
term = { searchTerm }
isMentionSearch = { this . props . isMentionSearch }
/ >
) ;
} , this ) ;
}
2015-07-13 04:23:24 -07:00
2015-09-02 09:45:12 -07:00
return (
< div className = 'sidebar--right__content' >
< div className = 'search-bar__container sidebar--right__search-header' > { searchForm } < / div >
< div className = 'sidebar-right__body' >
< SearchResultsHeader isMentionSearch = { this . props . isMentionSearch } / >
< div
id = 'search-items-container'
className = 'search-items-container'
>
{ ctls }
2015-06-14 23:53:32 -08:00
< / div >
< / div >
< / div >
) ;
}
2015-09-02 09:45:12 -07:00
}
SearchResults . propTypes = {
isMentionSearch : React . PropTypes . bool
} ;