woocommerce/plugins/woocommerce-admin/client/marketing/overview-multichannel/DiscoverTools/useRecommendedPlugins.ts

39 lines
1018 B
TypeScript

/**
* External dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { STORE_KEY } from '~/marketing/data/constants';
import { RecommendedPlugin } from '~/marketing/types';
const selector = 'getRecommendedPlugins';
const category = 'marketing';
export const useRecommendedPlugins = () => {
const { invalidateResolution, installAndActivateRecommendedPlugin } =
useDispatch( STORE_KEY );
const installAndActivate = ( plugin: string ) => {
installAndActivateRecommendedPlugin( plugin, category );
invalidateResolution( selector, [ category ] );
};
return useSelect( ( select ) => {
const { getRecommendedPlugins, hasFinishedResolution } =
select( STORE_KEY );
const plugins =
getRecommendedPlugins< RecommendedPlugin[] >( category );
const isLoading = ! hasFinishedResolution( selector, [ category ] );
return {
isInitializing: ! plugins.length && isLoading,
isLoading,
plugins,
installAndActivate,
};
}, [] );
};