2023-07-04 12:08:50 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2024-09-18 12:14:30 +00:00
|
|
|
import { useEffect, useState } from '@wordpress/element';
|
2023-08-09 13:04:35 +00:00
|
|
|
import { navigateTo, getNewPath, useQuery } from '@woocommerce/navigation';
|
2024-09-18 12:14:30 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
// eslint-disable-next-line @woocommerce/dependency-group
|
|
|
|
import { SearchControl } from '@wordpress/components';
|
|
|
|
// The @ts-ignore is needed because the SearchControl types are not exported from the @wordpress/components package,
|
|
|
|
// even though the component itself is. This is likely due to an older version of the package being used.
|
2023-07-04 12:08:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './search.scss';
|
2023-10-06 08:17:56 +00:00
|
|
|
import { MARKETPLACE_PATH } from '../constants';
|
2023-07-04 12:08:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search component.
|
|
|
|
*
|
|
|
|
* @return {JSX.Element} Search component.
|
|
|
|
*/
|
2023-08-10 07:16:56 +00:00
|
|
|
function Search(): JSX.Element {
|
2023-07-26 14:58:22 +00:00
|
|
|
const [ searchTerm, setSearchTerm ] = useState( '' );
|
2024-09-18 12:14:30 +00:00
|
|
|
const searchPlaceholder = __( 'Search Marketplace', 'woocommerce' );
|
2023-07-04 12:08:50 +00:00
|
|
|
|
2023-08-09 13:04:35 +00:00
|
|
|
const query = useQuery();
|
2023-07-04 12:08:50 +00:00
|
|
|
|
2023-08-09 13:04:35 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( query.term ) {
|
|
|
|
setSearchTerm( query.term );
|
2023-09-14 10:01:56 +00:00
|
|
|
} else {
|
|
|
|
setSearchTerm( '' );
|
2023-07-26 14:58:22 +00:00
|
|
|
}
|
2023-08-09 13:04:35 +00:00
|
|
|
}, [ query.term ] );
|
2023-07-04 12:08:50 +00:00
|
|
|
|
2023-08-09 13:04:35 +00:00
|
|
|
const runSearch = () => {
|
2024-09-18 12:14:30 +00:00
|
|
|
const newQuery: { term?: string; tab?: string } = query;
|
2023-08-09 13:04:35 +00:00
|
|
|
|
2024-09-18 12:14:30 +00:00
|
|
|
// If we're on 'Discover' or 'My subscriptions' when a search is initiated, move to the extensions tab
|
|
|
|
if ( ! newQuery.tab || newQuery.tab === 'my-subscriptions' ) {
|
|
|
|
newQuery.tab = 'extensions';
|
2023-10-17 16:32:23 +00:00
|
|
|
}
|
|
|
|
|
2024-09-18 12:14:30 +00:00
|
|
|
newQuery.term = searchTerm.trim();
|
|
|
|
|
2023-10-06 08:17:56 +00:00
|
|
|
// When the search term changes, we reset the query string on purpose.
|
2023-08-09 13:04:35 +00:00
|
|
|
navigateTo( {
|
2023-10-17 16:32:23 +00:00
|
|
|
url: getNewPath( newQuery, MARKETPLACE_PATH, {} ),
|
2023-08-09 13:04:35 +00:00
|
|
|
} );
|
2023-08-10 07:16:56 +00:00
|
|
|
|
2023-07-04 12:08:50 +00:00
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleKeyUp = ( event: { key: string } ) => {
|
|
|
|
if ( event.key === 'Enter' ) {
|
|
|
|
runSearch();
|
|
|
|
}
|
2023-08-09 13:04:35 +00:00
|
|
|
|
2023-07-26 14:58:22 +00:00
|
|
|
if ( event.key === 'Escape' ) {
|
|
|
|
setSearchTerm( '' );
|
|
|
|
}
|
2023-07-04 12:08:50 +00:00
|
|
|
};
|
|
|
|
|
2023-07-26 14:58:22 +00:00
|
|
|
return (
|
2024-09-18 12:14:30 +00:00
|
|
|
<SearchControl
|
|
|
|
label={ searchPlaceholder }
|
|
|
|
placeholder={ searchPlaceholder }
|
|
|
|
value={ searchTerm }
|
|
|
|
onChange={ setSearchTerm }
|
|
|
|
onKeyUp={ handleKeyUp }
|
|
|
|
className="woocommerce-marketplace__search"
|
|
|
|
/>
|
2023-07-26 14:58:22 +00:00
|
|
|
);
|
2023-07-04 12:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Search;
|