File size: 3,131 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
import page from '@automattic/calypso-router';
import { SearchControl } from '@wordpress/components';
import { useViewportMatch } from '@wordpress/compose';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import { useDispatch, useSelector } from 'react-redux';
import { setQueryArgs } from 'calypso/lib/query-args';
import scrollTo from 'calypso/lib/scroll-to';
import { useLocalizedPlugins } from 'calypso/my-sites/plugins/utils';
import { recordGoogleEvent } from 'calypso/state/analytics/actions';
import { getSelectedSite } from 'calypso/state/ui/selectors';
import { useTermsSuggestions } from '../use-terms-suggestions';
import './style.scss';

const SearchBoxHeader = ( props ) => {
	const {
		searchTerm,
		title,
		subtitle,
		isSticky,
		stickySearchBoxRef,
		categoriesRef,
		renderTitleInH1,
		searchTerms,
	} = props;

	const isDesktop = useViewportMatch( 'large' );
	const translate = useTranslate();
	const dispatch = useDispatch();
	const selectedSite = useSelector( getSelectedSite );
	const { localizePath } = useLocalizedPlugins();

	const recordSearchEvent = ( eventName ) => {
		dispatch( recordGoogleEvent( 'PluginsBrowser', eventName ) );
	};

	const classNames = [ 'search-box-header' ];

	if ( isSticky ) {
		classNames.push( 'fixed-top' );
	}

	return (
		<div className={ clsx( classNames ) }>
			{ renderTitleInH1 && <h1 className="search-box-header__header">{ title }</h1> }
			{ ! renderTitleInH1 && <div className="search-box-header__header">{ title }</div> }
			{ subtitle && <p className="search-box-header__subtitle">{ subtitle }</p> }
			<div className="search-box-header__search">
				<SearchControl
					__nextHasNoMarginBottom
					value={ searchTerm }
					className={ clsx( 'search-box-header__searchbox', {
						'components-search-control--mobile': ! isDesktop,
					} ) }
					placeholder={ translate( 'Try searching "%(searchTermSuggestion)s"', {
						args: { searchTermSuggestion: useTermsSuggestions( searchTerms ) || 'ecommerce' },
						textOnly: true,
					} ) }
					onChange={ ( newValue ) => {
						// Only update URL on clear
						if ( newValue === '' ) {
							recordSearchEvent( 'SearchCleared' );
							page.show( localizePath( `/plugins/${ selectedSite?.slug || '' }` ) );
							setQueryArgs( {} );
						}
					} }
					onKeyDown={ ( event ) => {
						if ( event.key === 'Enter' && event.target ) {
							event.preventDefault();
							const value = event.target.value;
							recordSearchEvent( 'SearchInitiated' );
							page.show( localizePath( `/plugins/${ selectedSite?.slug || '' }` ) );
							setQueryArgs( value ? { s: value } : {} );

							if ( value && categoriesRef?.current ) {
								scrollTo( {
									x: 0,
									y:
										categoriesRef.current.getBoundingClientRect().y - // Get to the top of categories
										categoriesRef.current.getBoundingClientRect().height, // But don't show the categories
									duration: 300,
								} );
							}
						}
					} }
				/>
			</div>
			<div className="search-box-header__sticky-ref" ref={ stickySearchBoxRef }></div>
		</div>
	);
};

export default SearchBoxHeader;