File size: 9,798 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
import page from '@automattic/calypso-router';
import { CompactCard } from '@automattic/components';
import {
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
} from '@wordpress/components';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import { trim, flatMap } from 'lodash';
import PropTypes from 'prop-types';
import * as React from 'react';
import { connect } from 'react-redux';
import DocumentHead from 'calypso/components/data/document-head';
import NavigationHeader from 'calypso/components/navigation-header';
import SearchInput from 'calypso/components/search';
import { addQueryArgs } from 'calypso/lib/url';
import withDimensions from 'calypso/lib/with-dimensions';
import BlankSuggestions from 'calypso/reader/components/reader-blank-suggestions';
import ReaderMain from 'calypso/reader/components/reader-main';
import { READER_SEARCH_POPULAR_SITES } from 'calypso/reader/follow-sources';
import { getSearchPlaceholderText } from 'calypso/reader/search/utils';
import SearchFollowButton from 'calypso/reader/search-stream/search-follow-button';
import { recordAction } from 'calypso/reader/stats';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import { recordReaderTracksEvent } from 'calypso/state/reader/analytics/actions';
import {
SORT_BY_RELEVANCE,
SORT_BY_LAST_UPDATED,
} from 'calypso/state/reader/feed-searches/actions';
import { getReaderAliasedFollowFeedUrl } from 'calypso/state/reader/follows/selectors';
import { getTransformedStreamItems } from 'calypso/state/reader/streams/selectors';
import ReaderPopularSitesSidebar from '../stream/reader-popular-sites-sidebar';
import PostResults from './post-results';
import SearchStreamHeader, { SEARCH_TYPES } from './search-stream-header';
import SiteResults from './site-results';
import Suggestion from './suggestion';
import SuggestionProvider from './suggestion-provider';
import './style.scss';
const WIDE_DISPLAY_CUTOFF = 660;
const updateQueryArg = ( params ) =>
page.replace( addQueryArgs( params, window.location.pathname + window.location.search ) );
const pickSort = ( sort ) => ( sort === 'date' ? SORT_BY_LAST_UPDATED : SORT_BY_RELEVANCE );
class SearchStream extends React.Component {
static propTypes = {
query: PropTypes.string,
streamKey: PropTypes.string,
};
state = {
feeds: [],
};
componentDidUpdate( prevProps ) {
if ( prevProps.query !== this.props.query ) {
this.resetSearchFeeds();
}
}
resetSearchFeeds = () => {
this.setState( { feeds: [] } );
};
setSearchFeeds = ( feeds ) => {
this.setState( { feeds: feeds } );
};
getTitle = ( props = this.props ) => props.query || props.translate( 'Search' );
updateQuery = ( newValue ) => {
this.scrollToTop();
// Remove whitespace from newValue and limit to 1024 characters
const trimmedValue = trim( newValue ).substring( 0, 1024 );
if (
( trimmedValue !== '' && trimmedValue.length > 1 && trimmedValue !== this.props.query ) ||
newValue === ''
) {
updateQueryArg( { q: trimmedValue } );
}
};
scrollToTop = () => {
window.scrollTo( 0, 0 );
};
onChangeSortPicker = ( sort ) => {
if ( typeof sort !== 'string' ) {
return;
}
switch ( sort ) {
case 'date':
recordAction( 'search_page_clicked_date_sort' );
break;
case 'relevance':
recordAction( 'search_page_clicked_relevance_sort' );
break;
}
if ( recordReaderTracksEvent ) {
this.props.recordReaderTracksEvent( 'calypso_reader_clicked_search_sort', {
query: this.props.query,
sort,
} );
}
updateQueryArg( { sort } );
};
trackTagsPageLinkClick = () => {
recordAction( 'clicked_reader_search_tags_page_link' );
this.props.recordReaderTracksEvent( 'calypso_reader_search_tags_page_link_clicked' );
};
handleFixedAreaMounted = ( ref ) => ( this.fixedAreaRef = ref );
handleSearchTypeSelection = ( searchType ) => updateQueryArg( { show: searchType } );
render() {
const { query, translate, searchType, suggestions, isLoggedIn } = this.props;
const sortOrder = this.props.sort;
const wideDisplay = this.props.width > WIDE_DISPLAY_CUTOFF;
const toggleGroupControlClasses = wideDisplay
? 'search-stream__sort-picker is-wide'
: 'search-stream__sort-picker';
// Hide posts and sites if the only result has no feed ID. This can happen when searching
// for a specific site to add a rss to your feed. Originally added in
// https://github.com/Automattic/wp-calypso/pull/78555.
const hidePostsAndSites =
this.state.feeds && this.state.feeds?.length === 1 && ! this.state.feeds[ 0 ].feed_ID;
let searchPlaceholderText = this.props.searchPlaceholderText;
if ( ! searchPlaceholderText ) {
searchPlaceholderText = getSearchPlaceholderText();
}
const documentTitle = translate( '%s ‹ Reader', {
args: this.getTitle(),
comment: '%s is the section name. For example: "My Likes"',
} );
const TEXT_RELEVANCE_SORT = translate( 'Relevance', {
comment: 'A sort order, showing the most relevant posts first.',
} );
const TEXT_DATE_SORT = translate( 'Date', {
comment: 'A sort order, showing the most recent posts first.',
} );
const searchStreamResultsClasses = clsx( 'search-stream__results', 'is-two-columns' );
const singleColumnResultsClasses = clsx( 'search-stream__single-column-results', {
'is-post-results': searchType === SEARCH_TYPES.POSTS && query,
} );
const suggestionList = flatMap( suggestions, ( suggestion ) => [
<Suggestion
suggestion={ suggestion.text }
source="search"
sort={ sortOrder === 'date' ? sortOrder : undefined }
railcar={ suggestion.railcar }
key={ 'suggestion-' + suggestion.text }
/>,
', ',
] ).slice( 0, -1 );
const fixedAreaHeight = this.fixedAreaRef && this.fixedAreaRef.clientHeight;
/* eslint-disable jsx-a11y/no-autofocus */
return (
<div>
<DocumentHead title={ documentTitle } />
<NavigationHeader
title={ translate( 'Search' ) }
subtitle={ translate( 'Search for specific topics, authors, or blogs.' ) }
/>
<div className="search-stream__fixed-area" ref={ this.handleFixedAreaMounted }>
<CompactCard className="search-stream__input-card">
<SearchInput
onSearch={ this.updateQuery }
onSearchClose={ this.scrollToTop }
onSearchOpen={ this.resetSearchFeeds }
autoFocus={ this.props.autoFocusInput }
delaySearch
delayTimeout={ 500 }
placeholder={ searchPlaceholderText }
initialValue={ query || '' }
value={ query || '' }
/>
</CompactCard>
<SearchFollowButton query={ query } feeds={ this.state.feeds } />
{ query && (
<div className={ toggleGroupControlClasses }>
<ToggleGroupControl
hideLabelFromVision
isBlock
label=""
value={ sortOrder }
onChange={ this.onChangeSortPicker }
__nextHasNoMarginBottom
__next40pxDefaultSize
>
<ToggleGroupControlOption label={ TEXT_RELEVANCE_SORT } value="relevance" />
<ToggleGroupControlOption label={ TEXT_DATE_SORT } value="date" />
</ToggleGroupControl>
</div>
) }
{ ! query && (
<BlankSuggestions
suggestions={ suggestionList }
trackTagsPageLinkClick={ this.trackTagsPageLinkClick }
/>
) }
{ ! hidePostsAndSites && (
<SearchStreamHeader
selected={ searchType }
onSelection={ this.handleSearchTypeSelection }
wideDisplay={ wideDisplay }
isLoggedIn={ isLoggedIn }
/>
) }
</div>
{ /* { isLoggedIn && <SpacerDiv domTarget={ this.fixedAreaRef } /> } */ }
{ ! hidePostsAndSites && wideDisplay && (
<div className={ searchStreamResultsClasses }>
<div className="search-stream__post-results">
<PostResults { ...this.props } fixedHeaderHeight={ fixedAreaHeight } />
</div>
<div className="search-stream__site-results">
{ query && (
<SiteResults
query={ query }
sort={ pickSort( sortOrder ) }
onReceiveSearchResults={ this.setSearchFeeds }
/>
) }
{ ! query && (
<ReaderPopularSitesSidebar
items={ this.props.items }
followSource={ READER_SEARCH_POPULAR_SITES }
/>
) }
</div>
</div>
) }
{ ! hidePostsAndSites && ! wideDisplay && (
<div className={ singleColumnResultsClasses }>
{ ( searchType === SEARCH_TYPES.POSTS && (
<PostResults { ...this.props } fixedHeaderHeight={ fixedAreaHeight } />
) ) ||
( query && (
<SiteResults
query={ query }
sort={ pickSort( sortOrder ) }
onReceiveSearchResults={ this.setSearchFeeds }
/>
) ) || (
<ReaderPopularSitesSidebar
items={ this.props.items }
followSource={ READER_SEARCH_POPULAR_SITES }
/>
) }
</div>
) }
</div>
);
/* eslint-enable jsx-a11y/no-autofocus */
}
}
/* eslint-disable */
// wrapping with Main so that we can use withWidth helper to pass down whole width of Main
const wrapWithMain = ( Component ) => ( props ) => (
<ReaderMain className="search-stream search-stream__with-sites" wideLayout>
<Component { ...props } />
</ReaderMain>
);
/* eslint-enable */
export default connect(
( state, ownProps ) => ( {
readerAliasedFollowFeedUrl:
ownProps.query && getReaderAliasedFollowFeedUrl( state, ownProps.query ),
isLoggedIn: isUserLoggedIn( state ),
items: getTransformedStreamItems( state, {
streamKey: ownProps.streamKey,
recsStreamKey: ownProps.recsStreamKey,
} ),
} ),
{
recordReaderTracksEvent,
}
)( localize( SuggestionProvider( wrapWithMain( withDimensions( SearchStream ) ) ) ) );
|