| 1 |
import { RecommendationsGrid } from '@recommendations/components/RecommendationsGrid'; |
| 2 |
import { |
| 3 |
selectIsError, |
| 4 |
selectIsLoading, |
| 5 |
selectRecommendations, |
| 6 |
} from '@recommendations/selectors/plugin-search'; |
| 7 |
import { usePluginSearchStore } from '@recommendations/state/plugin-search'; |
| 8 |
import { recordActivity } from '@recommendations/utils/record-activity'; |
| 9 |
import { useEffect } from '@wordpress/element'; |
| 10 |
import { __, sprintf } from '@wordpress/i18n'; |
| 11 |
import { safeDecodeURIComponent } from '@wordpress/url'; |
| 12 |
|
| 13 |
const showPartnerBranding = |
| 14 |
window.extRecommendationsData?.showPartnerBranding && |
| 15 |
window.extSharedData?.partnerLogo; |
| 16 |
|
| 17 |
export const PluginSearchBanner = () => { |
| 18 |
const query = usePluginSearchStore((state) => state.query); |
| 19 |
const isLoading = usePluginSearchStore(selectIsLoading); |
| 20 |
const isError = usePluginSearchStore(selectIsError); |
| 21 |
const recommendationsLimit = usePluginSearchStore( |
| 22 |
(state) => state.recommendationsLimit, |
| 23 |
); |
| 24 |
const recommendations = usePluginSearchStore(selectRecommendations); |
| 25 |
const fetchInstalledPlugins = usePluginSearchStore( |
| 26 |
(state) => state.fetchInstalledPlugins, |
| 27 |
); |
| 28 |
|
| 29 |
useEffect(() => { |
| 30 |
if (!query || isLoading) return; |
| 31 |
recordActivity({ slot: 'plugin-search', event: 'search' }); |
| 32 |
}, [query, isLoading]); |
| 33 |
|
| 34 |
useEffect(() => { |
| 35 |
if (!query) return; |
| 36 |
fetchInstalledPlugins(true); |
| 37 |
}, [query, fetchInstalledPlugins]); |
| 38 |
|
| 39 |
if (!query || !recommendations?.length || isLoading || isError) { |
| 40 |
return null; |
| 41 |
} |
| 42 |
|
| 43 |
return ( |
| 44 |
<div |
| 45 |
className="my-8 flex w-full flex-col overflow-hidden rounded-sm border border-gray-400 bg-white" |
| 46 |
data-test="extendify-recommendations-banner" |
| 47 |
> |
| 48 |
<div |
| 49 |
className={`flex h-14 ${showPartnerBranding ? 'bg-banner-main' : ''} px-6 py-4`} |
| 50 |
> |
| 51 |
{showPartnerBranding ? ( |
| 52 |
<> |
| 53 |
<img |
| 54 |
alt={window.extSharedData?.partnerName || 'Partner Logo'} |
| 55 |
className="mr-3 h-full" |
| 56 |
src={window.extSharedData?.partnerLogo} |
| 57 |
/> |
| 58 |
<div className="mr-3 border-l border-banner-text opacity-80" /> |
| 59 |
</> |
| 60 |
) : null} |
| 61 |
<h2 |
| 62 |
className={`m-0 flex h-full items-center ${showPartnerBranding ? 'text-banner-text' : ''} `} |
| 63 |
> |
| 64 |
{sprintf( |
| 65 |
// translators: %s: The search query term |
| 66 |
__('Recommended Solutions for: %s', 'extendify-local'), |
| 67 |
safeDecodeURIComponent(query), |
| 68 |
)} |
| 69 |
</h2> |
| 70 |
</div> |
| 71 |
<RecommendationsGrid |
| 72 |
recommendations={recommendations.slice(0, recommendationsLimit)} |
| 73 |
/> |
| 74 |
</div> |
| 75 |
); |
| 76 |
}; |
| 77 |
|