2022-09-14 07:43:30 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-11-24 13:46:08 +00:00
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
2023-03-08 15:42:02 +00:00
|
|
|
import { differenceWith } from 'lodash';
|
2022-09-14 07:43:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { STORE_KEY } from '~/marketing/data/constants';
|
2023-03-08 15:42:02 +00:00
|
|
|
import { useRecommendedChannels } from '~/marketing/hooks';
|
2022-12-09 16:33:10 +00:00
|
|
|
import { RecommendedPlugin } from '~/marketing/types';
|
2022-09-14 07:43:30 +00:00
|
|
|
|
2022-11-24 13:46:08 +00:00
|
|
|
const selector = 'getRecommendedPlugins';
|
2022-09-14 07:43:30 +00:00
|
|
|
const category = 'marketing';
|
|
|
|
|
|
|
|
export const useRecommendedPlugins = () => {
|
2023-03-08 15:42:02 +00:00
|
|
|
const { data: dataRecommendedChannels } = useRecommendedChannels();
|
2022-11-24 13:46:08 +00:00
|
|
|
const { invalidateResolution, installAndActivateRecommendedPlugin } =
|
|
|
|
useDispatch( STORE_KEY );
|
|
|
|
|
|
|
|
const installAndActivate = ( plugin: string ) => {
|
|
|
|
installAndActivateRecommendedPlugin( plugin, category );
|
|
|
|
invalidateResolution( selector, [ category ] );
|
|
|
|
};
|
|
|
|
|
2023-03-08 15:42:02 +00:00
|
|
|
const { isLoading, plugins } = useSelect( ( select ) => {
|
2022-11-24 13:46:08 +00:00
|
|
|
const { getRecommendedPlugins, hasFinishedResolution } =
|
|
|
|
select( STORE_KEY );
|
2022-09-14 07:43:30 +00:00
|
|
|
|
2022-11-24 13:46:08 +00:00
|
|
|
return {
|
2023-03-08 15:42:02 +00:00
|
|
|
isLoading: ! hasFinishedResolution( selector, [ category ] ),
|
|
|
|
plugins: getRecommendedPlugins< RecommendedPlugin[] >( category ),
|
2022-11-24 13:46:08 +00:00
|
|
|
};
|
|
|
|
}, [] );
|
2023-03-08 15:42:02 +00:00
|
|
|
|
|
|
|
const recommendedPluginsWithoutChannels = differenceWith(
|
|
|
|
plugins,
|
|
|
|
dataRecommendedChannels || [],
|
|
|
|
( a, b ) => a.product === b.product
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
isInitializing: ! recommendedPluginsWithoutChannels.length && isLoading,
|
|
|
|
isLoading,
|
|
|
|
plugins: recommendedPluginsWithoutChannels,
|
|
|
|
installAndActivate,
|
|
|
|
};
|
2022-09-14 07:43:30 +00:00
|
|
|
};
|